changeset 31132:0f4d16af143b

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.
author Rik <rik@octave.org>
date Sat, 09 Jul 2022 11:11:53 -0700
parents 6cfc7af8eacc
children 1d61c36bb34a
files scripts/miscellaneous/perl.m scripts/miscellaneous/python.m
diffstat 2 files changed, 41 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- 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 <invalid arguments> perl (123)
+## Test input validation
+%!error <Invalid call> perl ()
+%!error <SCRIPTFILE must be a non-empty string> perl (123)
+%!error <SCRIPTFILE must be a non-empty string> perl ("")
+%!error <ARGUMENTS must be strings> perl ("perlfile", 123)
--- 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 <invalid arguments> python (123)
+## Test input validation
+%!error <Invalid call> python ()
+%!error <SCRIPTFILE must be a non-empty string> python (123)
+%!error <SCRIPTFILE must be a non-empty string> python ("")
+%!error <ARGUMENTS must be strings> python ("pythonfile", 123)