diff scripts/miscellaneous/python.m @ 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 22305b923761
line wrap: on
line diff
--- 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)