# HG changeset patch # User Rik # Date 1657390313 25200 # Node ID 0f4d16af143b68aa45f7acac4a0c537863052c23 # Parent 6cfc7af8eacc6310cfd38f767ec8c472dc6b501a perl.m, python.m: Redo input validation. * perl.m, python.m: Redo input validation to check for correct number of arguments as well as type. Issue more informative error messages when validation fails. Add more BIST tests for input validation. diff -r 6cfc7af8eacc -r 0f4d16af143b scripts/miscellaneous/perl.m --- a/scripts/miscellaneous/perl.m Sat Jul 09 10:22:57 2022 -0700 +++ b/scripts/miscellaneous/perl.m Sat Jul 09 11:11:53 2022 -0700 @@ -36,27 +36,32 @@ ## @seealso{system, python} ## @end deftypefn -function [output, status] = perl (scriptfile = "-e ''", varargin) +function [output, status] = perl (scriptfile, varargin) - ## VARARGIN is initialized to {}(1x0) if no additional arguments are - ## supplied, so there is no need to check for it, or provide an - ## initial value in the argument list of the function definition. + if (nargin < 1) + print_usage (); + endif - if (ischar (scriptfile) - && ( (nargin == 1 && ! isempty (scriptfile)) - || (nargin != 1 && iscellstr (varargin)))) - if (! strcmp (scriptfile(1:2), "-e")) - ## Attempt to find file in loadpath. No effect for absolute filenames. - scriptfile = file_in_loadpath (scriptfile); - endif + if (! ischar (scriptfile) || isempty (scriptfile)) + error ("perl: SCRIPTFILE must be a non-empty string"); + endif + if (nargin > 1 && ! iscellstr (varargin)) + error ("perl: ARGUMENTS must be strings"); + endif - [status, output] = system (["perl " scriptfile ... - sprintf(" %s", varargin{:})]); - else - error ("perl: invalid arguments"); + if (numel (scriptfile) < 2 || ! strcmp (scriptfile(1:2), "-e")) + ## Attempt to find file in loadpath. No effect for absolute filenames. + scriptfile = file_in_loadpath (scriptfile); endif + [status, output] = system (["perl " scriptfile ... + sprintf(" %s", varargin{:})]); + endfunction -%!error perl (123) +## Test input validation +%!error perl () +%!error perl (123) +%!error perl ("") +%!error perl ("perlfile", 123) diff -r 6cfc7af8eacc -r 0f4d16af143b scripts/miscellaneous/python.m --- a/scripts/miscellaneous/python.m Sat Jul 09 10:22:57 2022 -0700 +++ b/scripts/miscellaneous/python.m Sat Jul 09 11:11:53 2022 -0700 @@ -36,23 +36,28 @@ ## @seealso{system, perl} ## @end deftypefn -function [output, status] = python (scriptfile = "-c ''", varargin) +function [output, status] = python (scriptfile, varargin) persistent pyexec = get_python_executable (); - if (ischar (scriptfile) - && ( (nargin == 1 && ! isempty (scriptfile)) - || (nargin != 1 && iscellstr (varargin)))) - if (! strcmp (scriptfile(1:2), "-c")) + if (nargin < 1) + print_usage (); + endif + + if (! ischar (scriptfile) || isempty (scriptfile)) + error ("python: SCRIPTFILE must be a non-empty string"); + endif + if (nargin > 1 && ! iscellstr (varargin)) + error ("python: ARGUMENTS must be strings"); + endif + + if (numel (scriptfile) < 2 || ! strcmp (scriptfile(1:2), "-c")) ## Attempt to find file in loadpath. No effect for absolute filenames. scriptfile = file_in_loadpath (scriptfile); endif - [status, output] = system ([pyexec " " scriptfile, ... - sprintf(" %s", varargin{:})]); - else - error ("python: invalid arguments"); - endif + [status, output] = system ([pyexec " " scriptfile, ... + sprintf(" %s", varargin{:})]); endfunction @@ -72,4 +77,8 @@ endfunction -%!error python (123) +## Test input validation +%!error python () +%!error python (123) +%!error python ("") +%!error python ("pythonfile", 123)