Mercurial > octave
changeset 28563:5a07c798eb08
avoid function call input or output argument number mismatch
Don't call functions with more inputs or outputs than they are defined
to accept. For example, always define graphics callback functions to
accept at least two arguments. To avoid creating unused variable
names, they may be defined as ignored (~).
* importdata.m: Call fileparts with three outputs, not four.
* inputParser.m: Define default validation function with ignored input.
* odemergeopts.m: Accept additional SOLVER argument.
* annotation.m, legend.m, movfun.m, bug-55321.tst: Define callback
functions with two inputs.
* annotation.m (addbasemenu): Also accept varargin.
* graphics.cc: Fix tests.
* pkg/private/install.m: Don't pass extra global_install argument to
getarchdir.
* sparse/private/__alltohandles__.m: Define function handles with two
inputs where needed.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 11 Jul 2020 10:15:57 -0400 |
parents | b0b80efecea1 |
children | 1dd765e54265 |
files | libinterp/corefcn/graphics.cc scripts/image/imformats.m scripts/io/importdata.m scripts/miscellaneous/inputParser.m scripts/ode/ode15s.m scripts/ode/ode23.m scripts/ode/ode23s.m scripts/ode/ode45.m scripts/ode/private/odemergeopts.m scripts/pkg/private/install.m scripts/plot/appearance/annotation.m scripts/plot/appearance/legend.m scripts/signal/movfun.m scripts/sparse/private/__alltohandles__.m test/bug-55321.tst |
diffstat | 15 files changed, 78 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/graphics.cc Sat Jul 11 09:34:44 2020 -0400 +++ b/libinterp/corefcn/graphics.cc Sat Jul 11 10:15:57 2020 -0400 @@ -3612,7 +3612,7 @@ %! hf = figure ("handlevisibility", "off", "visible", "off"); %! hax = axes ("parent", hf, "handlevisibility", "off"); %! unwind_protect -%! fcn = @(h) setappdata (h, "testdata", gcbo ()); +%! fcn = @(h, ~) setappdata (h, "testdata", gcbo ()); %! addlistener (hf, "color", fcn); %! addlistener (hax, "color", fcn); %! set (hf, "color", "b"); @@ -12474,7 +12474,7 @@ /* ## Test interruptible/busyaction properties -%!function cb (h) +%!function cb (h, ~) %! setappdata (gcbf (), "cb_exec", [getappdata(gcbf (), "cb_exec") h]); %! drawnow (); %! setappdata (gcbf (), "cb_exec", [getappdata(gcbf (), "cb_exec") h]);
--- a/scripts/image/imformats.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/image/imformats.m Sat Jul 11 10:15:57 2020 -0400 @@ -372,7 +372,7 @@ %! unwind_protect %! fmt = imformats ("jpg"); # take jpg as template %! fmt.ext = "new_fmt"; -%! fmt.read = @() true (); +%! fmt.read = @(~) true (); %! imformats ("add", fmt); %! assert (imread (fname), true); %! unwind_protect_cleanup @@ -391,7 +391,7 @@ %! unwind_protect %! fmt = imformats ("jpg"); # take jpg as template %! fmt.ext = "new_fmt1"; -%! fmt.read = @() true(); +%! fmt.read = @(~) true(); %! fmt(2) = fmt(1); %! fmt(2).ext = "new_fmt2"; %! imformats ("add", fmt);
--- a/scripts/io/importdata.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/io/importdata.m Sat Jul 11 10:15:57 2020 -0400 @@ -98,7 +98,7 @@ ## Check file format ## Get the extension from the filename. - [~, ~, ext, ~] = fileparts (fname); + [~, ~, ext] = fileparts (fname); ext = lower (ext); switch (ext)
--- a/scripts/miscellaneous/inputParser.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/miscellaneous/inputParser.m Sat Jul 11 10:15:57 2020 -0400 @@ -190,7 +190,7 @@ properties (Access = protected, Constant = true) ## Default validator, always returns scalar true. - def_val = @() true; + def_val = @(~) true; endproperties methods
--- a/scripts/ode/ode15s.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/ode/ode15s.m Sat Jul 11 10:15:57 2020 -0400 @@ -433,7 +433,7 @@ %! refrob = [100, 0.617234887614937, 0.000006153591397, 0.382758958793666]; %!endfunction %! -%!function [val, isterminal, direction] = feve (t, y) +%!function [val, isterminal, direction] = feve (t, y, ~) %! isterminal = [0, 1]; %! if (t < 1e1) %! val = [-1, -2];
--- a/scripts/ode/ode23.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/ode/ode23.m Sat Jul 11 10:15:57 2020 -0400 @@ -326,19 +326,19 @@ ## We are using the Van der Pol equation for all tests. ## Further tests also define a reference solution (computed at high accuracy) -%!function ydot = fpol (t, y) # The Van der Pol ODE +%!function ydot = fpol (t, y, varargin) # The Van der Pol ODE %! ydot = [y(2); (1 - y(1)^2) * y(2) - y(1)]; %!endfunction %!function ref = fref () # The computed reference sol %! ref = [0.32331666704577, -1.83297456798624]; %!endfunction %!function [val, trm, dir] = feve (t, y, varargin) -%! val = fpol (t, y, varargin); # We use the derivatives +%! val = fpol (t, y, varargin{:}); # We use the derivatives %! trm = zeros (2,1); # that's why component 2 %! dir = ones (2,1); # does not seem to be exact %!endfunction %!function [val, trm, dir] = fevn (t, y, varargin) -%! val = fpol (t, y, varargin); # We use the derivatives +%! val = fpol (t, y, varargin{:}); # We use the derivatives %! trm = ones (2,1); # that's why component 2 %! dir = ones (2,1); # does not seem to be exact %!endfunction
--- a/scripts/ode/ode23s.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/ode/ode23s.m Sat Jul 11 10:15:57 2020 -0400 @@ -356,7 +356,7 @@ ## We are using the "Van der Pol" implementation for all tests that are done ## for this function. For further tests we also define a reference solution ## (computed at high accuracy). -%!function ydot = fpol (t, y) # The Van der Pol ODE +%!function ydot = fpol (t, y, varargin) # The Van der Pol ODE %! ydot = [y(2); 10 * (1 - y(1)^2) * y(2) - y(1)]; %!endfunction %!function ydot = jac (t, y) # The Van der Pol ODE @@ -366,12 +366,12 @@ %! ref = [1.8610687248524305 -0.0753216319179125]; %!endfunction %!function [val, trm, dir] = feve (t, y, varargin) -%! val = fpol (t, y, varargin); # We use the derivatives +%! val = fpol (t, y, varargin{:}); # We use the derivatives %! trm = zeros (2,1); # that's why component 2 %! dir = ones (2,1); # does not seem to be exact %!endfunction %!function [val, trm, dir] = fevn (t, y, varargin) -%! val = fpol (t, y, varargin); # We use the derivatives +%! val = fpol (t, y, varargin{:}); # We use the derivatives %! trm = ones (2,1); # that's why component 2 %! dir = ones (2,1); # does not seem to be exact %!endfunction
--- a/scripts/ode/ode45.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/ode/ode45.m Sat Jul 11 10:15:57 2020 -0400 @@ -326,19 +326,19 @@ ## We are using the Van der Pol equation for all tests. ## Further tests also define a reference solution (computed at high accuracy) -%!function ydot = fpol (t, y) # The Van der Pol ODE +%!function ydot = fpol (t, y, varargin) # The Van der Pol ODE %! ydot = [y(2); (1 - y(1)^2) * y(2) - y(1)]; %!endfunction %!function ref = fref () # The computed reference solution %! ref = [0.32331666704577, -1.83297456798624]; %!endfunction %!function [val, trm, dir] = feve (t, y, varargin) -%! val = fpol (t, y, varargin); # We use the derivatives +%! val = fpol (t, y, varargin{:}); # We use the derivatives %! trm = zeros (2,1); # that's why component 2 %! dir = ones (2,1); # does not seem to be exact %!endfunction %!function [val, trm, dir] = fevn (t, y, varargin) -%! val = fpol (t, y, varargin); # We use the derivatives +%! val = fpol (t, y, varargin{:}); # We use the derivatives %! trm = ones (2,1); # that's why component 2 %! dir = ones (2,1); # does not seem to be exact %!endfunction
--- a/scripts/ode/private/odemergeopts.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/ode/private/odemergeopts.m Sat Jul 11 10:15:57 2020 -0400 @@ -23,8 +23,12 @@ ## ######################################################################## +## FIXME: there are some calls to odemergeopts with a "solver" argument +## but we don't use that here. Should the calls be fixed or should we +## do something with the solver argument here? + function options = odemergeopts (caller, useroptions, options, classes, - attributes); + attributes, solver); for [value, key] = options
--- a/scripts/pkg/private/install.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/pkg/private/install.m Sat Jul 11 10:15:57 2020 -0400 @@ -718,8 +718,8 @@ ## part in the main directory. archdir = fullfile (getarchprefix (desc, global_install), [desc.name "-" desc.version], getarch ()); - if (isfolder (getarchdir (desc, global_install))) - archpkg = fullfile (getarchdir (desc, global_install), nm); + if (isfolder (getarchdir (desc))) + archpkg = fullfile (getarchdir (desc), nm); archfid = fopen (archpkg, "at"); else archpkg = instpkg;
--- a/scripts/plot/appearance/annotation.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/plot/appearance/annotation.m Sat Jul 11 10:15:57 2020 -0400 @@ -277,7 +277,7 @@ listener = {@update_figsize_points, hax}; addlistener (hf, "position", listener); - delfcn = @() dellistener (hf, "position", listener); + delfcn = @(~, ~) dellistener (hf, "position", listener); set (hax, "deletefcn", delfcn); endfunction @@ -329,7 +329,7 @@ addlistener (hax, "figsize_points", listener); - delfcn = @() dellistener (hax, "figsize_points", listener); + delfcn = @(~, ~) dellistener (hax, "figsize_points", listener); set (h, "deletefcn", delfcn); addlistener (h, "units", {@update_position, h}); @@ -832,7 +832,10 @@ proptable(1:3:end), proptable(2:3:end), proptable(3:3:end)); endfunction -function addbasemenu (hm, hpar, pname, vals, mainlabel = "") +## FIXME: there are some calls to addbasemenu with option-like arguments +## but we don't do anything with varargin here. What is the right thing +## to do? +function addbasemenu (hm, hpar, pname, vals, mainlabel = "", varargin) if (isempty (mainlabel)) mainlabel = pname;
--- a/scripts/plot/appearance/legend.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/plot/appearance/legend.m Sat Jul 11 10:15:57 2020 -0400 @@ -245,17 +245,17 @@ hf = ancestor (hax, "figure"); add_safe_listener (hl, hf, "colormap", ... - @() set (hl, "colormap", get (hax, "colormap"))); + @(~, ~) set (hl, "colormap", get (hax, "colormap"))); add_safe_listener (hl, hax, "position", {@maybe_update_layout_cb, hl}); add_safe_listener (hl, hax, "tightinset", ... - @(h) update_layout_cb (get (h, "__legend_handle__"))); + @(h, ~) update_layout_cb (get (h, "__legend_handle__"))); add_safe_listener (hl, hax, "clim", ... - @(hax) set (hl, "clim", get (hax, "clim"))); + @(hax, ~) set (hl, "clim", get (hax, "clim"))); add_safe_listener (hl, hax, "colormap", ... - @(hax) set (hl, "colormap", get (hax, "colormap"))); + @(hax, ~) set (hl, "colormap", get (hax, "colormap"))); add_safe_listener (hl, hax, "fontsize", ... - @(hax) set (hl, "fontsize", 0.9*get (hax, "fontsize"))); + @(hax, ~) set (hl, "fontsize", 0.9*get (hax, "fontsize"))); add_safe_listener (hl, hax, "children", {@legend_autoupdate_cb, hl}); ## Listeners to legend properties @@ -281,8 +281,8 @@ addlistener (hl, "string", @update_string_cb); addlistener (hl, "textcolor", ... - @(h) set (findobj (h, "type", "text"), ... - "color", get (hl, "textcolor"))); + @(h, ~) set (findobj (h, "type", "text"), ... + "color", get (hl, "textcolor"))); addlistener (hl, "visible", @update_visible_cb); @@ -340,14 +340,14 @@ endfunction -function update_edgecolor_cb (hl) +function update_edgecolor_cb (hl, ~) ecolor = get (hl, "edgecolor"); set (hl, "xcolor", ecolor, "ycolor", ecolor); endfunction -function update_position_cb (hl) +function update_position_cb (hl, ~) updating = getappdata (hl, "__updating_layout__"); if (isempty (updating) || ! updating) @@ -356,7 +356,7 @@ endfunction -function update_string_cb (hl) +function update_string_cb (hl, ~) ## Check that the number of legend item and the number of labels match ## before calling update_layout_cb. @@ -390,7 +390,7 @@ endfunction -function update_visible_cb (hl) +function update_visible_cb (hl, ~) location = get (hl, "location"); if (strcmp (location(end:-1:end-3), "edis")) @@ -416,7 +416,7 @@ endfunction -function delete_legend_cb (hl) +function delete_legend_cb (hl, ~) reset_cb ([], [], hl, false); @@ -461,7 +461,7 @@ addproperty ("numcolumnsmode", hl, "radio", "{auto}|manual"); - addlistener (hl, "numcolumns", @(h) set (h, "numcolumnsmode", "manual")); + addlistener (hl, "numcolumns", @(h, ~) set (h, "numcolumnsmode", "manual")); addproperty ("autoupdate", hl, "radio", "{on}|off"); @@ -505,7 +505,7 @@ endfunction -function update_numchild_cb (hl) +function update_numchild_cb (hl, ~) if (strcmp (get (hl, "autoupdate"), "on")) @@ -992,12 +992,12 @@ safe_property_link (hplt(1), hicon, lprops); safe_property_link (hplt(end), hmarker, mprops); addlistener (hicon, "ydata", ... - @(h) set (hmarker, "ydata", get (h, "markerydata"))); + @(h, ~) set (hmarker, "ydata", get (h, "markerydata"))); addlistener (hicon, "xdata", ... - @(h) set (hmarker, "xdata", get (h, "markerxdata"))); + @(h, ~) set (hmarker, "xdata", get (h, "markerxdata"))); addlistener (hmarker, "markersize", @update_marker_cb); add_safe_listener (hl, hplt(1), "beingdeleted", - @() delete ([hicon hmarker])) + @(~, ~) delete ([hicon hmarker])) if (! strcmp (typ, "__errplot__")) setappdata (hicon, "__creator__", typ); else @@ -1035,11 +1035,11 @@ safe_property_link (hplt(1), hicon, pprops); safe_property_link (hplt(end), htmp, pprops); addlistener (hicon, "ydata", ... - @(h) set (htmp, "ydata", get (h, "innerydata"))); + @(h, ~) set (htmp, "ydata", get (h, "innerydata"))); addlistener (hicon, "xdata", ... - @(h) set (htmp, "xdata", get (h, "innerxdata"))); + @(h, ~) set (htmp, "xdata", get (h, "innerxdata"))); add_safe_listener (hl, hplt(1), "beingdeleted", - @() delete ([hicon htmp])) + @(~, ~) delete ([hicon htmp])) setappdata (hicon, "__creator__", typ); @@ -1055,9 +1055,9 @@ function safe_property_link (h1, h2, props) for ii = 1:numel (props) prop = props{ii}; - lsn = {h1, prop, @(h) set (h2, prop, get (h, prop))}; + lsn = {h1, prop, @(h, ~) set (h2, prop, get (h, prop))}; addlistener (lsn{:}); - addlistener (h2, "beingdeleted", @() dellistener (lsn{:})); + addlistener (h2, "beingdeleted", @(~, ~) dellistener (lsn{:})); endfor endfunction @@ -1079,7 +1079,7 @@ endfunction -function update_marker_cb (h) +function update_marker_cb (h, ~) if (get (h, "markersize") > 8) set (h, "markersize", 8);
--- a/scripts/signal/movfun.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/signal/movfun.m Sat Jul 11 10:15:57 2020 -0400 @@ -328,7 +328,7 @@ ## Apply replacement value boundary conditions ## Window is padded at beginning and end with user-specified value. -function y = replaceval_bc (fcn, x, idxp, win, wlen) +function y = replaceval_bc (fcn, x, idxp, win, wlen, ~) persistent substitute; @@ -359,7 +359,7 @@ ## Apply "same" boundary conditions ## 'y' values outside window are replaced by value of 'x' at the window ## boundary. -function y = same_bc (fcn, x, idxp, win) +function y = same_bc (fcn, x, idxp, win, ~, ~) idx = idxp + win; idx(idx < 1) = 1; N = length (x); @@ -370,7 +370,7 @@ ## Apply "periodic" boundary conditions ## Window wraps around. Window values outside data array are replaced with ## data from the other end of the array. -function y = periodic_bc (fcn, x, idxp, win) +function y = periodic_bc (fcn, x, idxp, win, ~, ~) N = length (x); idx = idxp + win; tf = idx < 1;
--- a/scripts/sparse/private/__alltohandles__.m Sat Jul 11 09:34:44 2020 -0400 +++ b/scripts/sparse/private/__alltohandles__.m Sat Jul 11 10:15:57 2020 -0400 @@ -75,7 +75,16 @@ ## Check M1 and sets its type if (isempty (M1)) # M1 empty, set to identity function - M1fun = @(x) x; + switch solver_name + case {"pcg", "gmres", "bicgstab", "cgs", "tfqmr"} + ## methods which do not require the transpose + M1fun = @(x) x; + case {"bicg"} + ## methods which do require the transpose + M1fun = @(x, ~) x; + otherwise + error (["__alltohandles__: unknown method: ", solver_name]); + endswitch else # M1 not empty if (is_function_handle (M1)) M1fun = M1; @@ -89,7 +98,16 @@ endif if (isempty (M2)) # M2 empty, then I set is to the identity function - M2fun = @(x) x; + switch solver_name + case {"pcg", "gmres", "bicgstab", "cgs", "tfqmr"} + ## methods which do not require the transpose + M2fun = @(x) x; + case {"bicg"} + ## methods which do require the transpose + M2fun = @(x, ~) x; + otherwise + error (["__alltohandles__: unknown method: ", solver_name]); + endswitch else # M2 not empty if (is_function_handle (M2)) M2fun = M2; @@ -104,7 +122,7 @@ switch solver_name case {"pcg", "gmres", "bicgstab", "cgs", "tfqmr"} - # methods which do not require the transpose + ## methods which do not require the transpose if (A_is_numeric) Afun = @(x) A * x; endif @@ -115,7 +133,7 @@ M2fun = @(x) M2 \ x; endif case {"bicg"} - # methods which do require the transpose + ## methods which do require the transpose if (A_is_numeric) Afun = @(x, trans) A_sub (A, x, trans); endif
--- a/test/bug-55321.tst Sat Jul 11 09:34:44 2020 -0400 +++ b/test/bug-55321.tst Sat Jul 11 10:15:57 2020 -0400 @@ -23,7 +23,7 @@ ## ######################################################################## -%!function cb_children (hg) +%!function cb_children (hg, ~) %! hl = get (hg, "children"); %! color = get (hl, "color"); %! set (hl, "userdata", isequal (color, [1 0 0]));