bugGNU Octave - Bugs: bug #49793, octave's inputParser only accepts...

 
 

bug #49793: octave's inputParser only accepts validation functions that return true or false

Submitted by:  None
Submitted on:  Wed 07 Dec 2016 01:46:50 PM UTC  
 
Category: Octave FunctionSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Matlab Compatibility
Status: NoneAssigned to: None
Originator Name: Originator Email: -unavailable-
Open/Closed: OpenRelease: 4.2.0
Operating System: GNU/Linux

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

(Jump to the original submission Jump to the original submission)

Fri 20 Oct 2017 12:50:43 PM UTC, comment #10:

I stumbled on the same problem as described in this bug when using the function dirPlus.m from Matlabs FileExchange. There the function validateattributes() was used for argument checking in input parsers. This function has no return value and does throw an error if argument checking fails. In matlab it seems to be valid to do so.

I have created a fix for inputParser in octave to handle this type of functions that does also print a bit more verbose information on what has been checked if the check fails.

See my fix for inputParser.m line ~500 below.

Take care: in the original source "val (in)" was written with a blank. This leads to a different result in this statement. It must be written without blank as "val(in)"!

Georg Wiora <gwiora>
Thu 08 Dec 2016 07:47:15 PM UTC, comment #9:

An anonymous function is similar to a regular one defined as: "function varargout = fcn(x)", and nargout will return -1 for both, just like MATLAB. Hence I was checking "nout~=0" rather than "nout>0".

But you're right, my version won't work unless there was a fixed error ID/message for the too-many-outputs case. Not to mention I assumed it was okay to call the validation twice..

I don't know how hard it is, but I guess a fix for nargout is needed here.

Amro <amro_octave>
Thu 08 Dec 2016 06:27:00 PM UTC, comment #8:

The main point of jwe's idea was that it does depend on fixing nargout so it works for builtins. I wonder about anonymous functions though.

Amro - a couple points about your suggestion. There is no error ID that can be caught in Octave yet for the error of too many output arguments (that can be fixed of course). And this nested approach means that val may be called more than once on the given argument. It's likely that most validation routines will be pure functions, but it's possible to have some side effects from calling the validation function more than once.

Mike Miller <mtmiller>
Project Administrator
Thu 08 Dec 2016 04:19:28 PM UTC, comment #7:

@jwe
if nargout doesn't work with builtins, then calling something like
ip.addRequired('name',@isnumeric);
will always fail in your solution because "nargout" will throw an error...

I think we need to need to test the error in the catch block and attempt calling the validation function another time inside a second try/catch if the error indicated a too-many-output case.

How about this version (it's sort of a hack until "nargout" is fixed):

Amro <amro_octave>
Thu 08 Dec 2016 03:33:40 PM UTC, comment #6:

After thinking about this some more, I agree that the best solution is to do use nargout to check to see whether the function returns at least one output and use a try-catch block. So it would be something like this:

Even though nargout doesn't work properly for all functions yet, this solution will work for user-defined validation functions.

We can also make it a priority for the next release to fix Octave so that nargout will work for built-in functions as well.

John W. Eaton <jwe>
Project Administrator
Thu 08 Dec 2016 03:12:10 PM UTC, comment #5:

nargout doesn't work for built-in functions yet.

That this works:

might be seen as a bug (it is apparently not the same as what Matlab does) so someone might fix it and then your "fix" here won't work.

John W. Eaton <jwe>
Project Administrator
Thu 08 Dec 2016 01:25:52 PM UTC, comment #4:

perhaps a check to "nargout(val)" to differentiate between the two cases of validation functions, coupled with a try/catch block.

Amro <amro_octave>
Thu 08 Dec 2016 09:45:49 AM UTC, comment #3:

As in octave a || b apparently returns the logical value of b if a is undefined (e.g. a is a call to a function which returns nothing), I suggest the following one-line-fix in inputParser.m:

The only drawback I see so far is, that 'failed validation of %s' is only printed if the validation function does not throw an error before. Here a hopefully comprehensive example, using inputParser.m patched as described above:

Anonymous
Wed 07 Dec 2016 03:53:02 PM UTC, comment #2:

It would also be good idea to add a "nargoutchk" check to validateattributes, that way it produces a better error message saying 'Too many output arguments'.

Amro <amro_octave>
Wed 07 Dec 2016 03:46:16 PM UTC, comment #1:

+1 I came across the same error as well.

Amro <amro_octave>
Wed 07 Dec 2016 01:46:50 PM UTC, original submission:

fails with

From Matlab's documentation (cf. https://de.mathworks.com/help/matlab/ref/inputparser.addrequired.html):
"inputParser accepts two types of validation functions: functions that return true or false, and functions that pass or throw an error. Both types of validation functions must accept a single input argument."

octave/4.2.0/m/general/inputParser.m lines 504 to 509:

Octave's function validate_arg which is called from Octave's inputParser's parse function can handle only validation functions that return true or false.
It has to be rewritten, to cope with validation functions that return nothing or throw and error, as validateattributes does, to be MATLAB compatible.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by gwiora (Posted a comment)
  • -unavailable- added by mtmiller (Posted a comment)
  • -unavailable- added by jwe (Posted a comment)
  • -unavailable- added by amro_octave (Posted a comment)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    No Changes Have Been Made to This Item

    Back to the top


    Powered by Savane 3.1-cleanup1