comparison 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
comparison
equal deleted inserted replaced
31131:6cfc7af8eacc 31132:0f4d16af143b
34 ## @var{scriptfile} is not an absolute filename it is searched for in the 34 ## @var{scriptfile} is not an absolute filename it is searched for in the
35 ## current directory and then in the Octave loadpath. 35 ## current directory and then in the Octave loadpath.
36 ## @seealso{system, perl} 36 ## @seealso{system, perl}
37 ## @end deftypefn 37 ## @end deftypefn
38 38
39 function [output, status] = python (scriptfile = "-c ''", varargin) 39 function [output, status] = python (scriptfile, varargin)
40 40
41 persistent pyexec = get_python_executable (); 41 persistent pyexec = get_python_executable ();
42 42
43 if (ischar (scriptfile) 43 if (nargin < 1)
44 && ( (nargin == 1 && ! isempty (scriptfile)) 44 print_usage ();
45 || (nargin != 1 && iscellstr (varargin)))) 45 endif
46 if (! strcmp (scriptfile(1:2), "-c")) 46
47 if (! ischar (scriptfile) || isempty (scriptfile))
48 error ("python: SCRIPTFILE must be a non-empty string");
49 endif
50 if (nargin > 1 && ! iscellstr (varargin))
51 error ("python: ARGUMENTS must be strings");
52 endif
53
54 if (numel (scriptfile) < 2 || ! strcmp (scriptfile(1:2), "-c"))
47 ## Attempt to find file in loadpath. No effect for absolute filenames. 55 ## Attempt to find file in loadpath. No effect for absolute filenames.
48 scriptfile = file_in_loadpath (scriptfile); 56 scriptfile = file_in_loadpath (scriptfile);
49 endif 57 endif
50 58
51 [status, output] = system ([pyexec " " scriptfile, ... 59 [status, output] = system ([pyexec " " scriptfile, ...
52 sprintf(" %s", varargin{:})]); 60 sprintf(" %s", varargin{:})]);
53 else
54 error ("python: invalid arguments");
55 endif
56 61
57 endfunction 62 endfunction
58 63
59 function pyexec = get_python_executable () 64 function pyexec = get_python_executable ()
60 65
70 endif 75 endif
71 76
72 endfunction 77 endfunction
73 78
74 79
75 %!error <invalid arguments> python (123) 80 ## Test input validation
81 %!error <Invalid call> python ()
82 %!error <SCRIPTFILE must be a non-empty string> python (123)
83 %!error <SCRIPTFILE must be a non-empty string> python ("")
84 %!error <ARGUMENTS must be strings> python ("pythonfile", 123)