bugGNU Octave - Bugs: bug #48949, mexCallMATLABWithTrap not...

 
 

bug #48949: mexCallMATLABWithTrap not implemented

Submitted by:  Guillaume <gyom>
Submitted on:  Tue 30 Aug 2016 10:42:32 AM UTC  
 
Category:  Libraries Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Feature Request
Status:  Fixed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  Closed
Release:  dev Operating System:  Any

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)

Thu 08 Sep 2016 11:14:33 PM UTC, comment #6: 

I also made the default value of the TrapFlag to be 0 to match Matlab (http://hg.savannah.gnu.org/hgweb/octave/rev/3a8af9d517de).  Previously Octave was behaving as if TrapFlag was set.

Rik <rik5>
Project Administrator
Thu 08 Sep 2016 11:03:29 PM UTC, comment #5: 

I added mexCallMATLABWithTrap to the MEX API in this cset (http://hg.savannah.gnu.org/hgweb/octave/rev/ad5439817753).  I mostly followed your suggestion, but the new code saves and restores the TrapFlag in case it is not 1 when entering the function.  I also used Octave coding conventions for the alignment of the code and for the error id and message.

Rik <rik5>
Project Administrator
Thu 08 Sep 2016 10:04:58 PM UTC, comment #4: 

Re-titling bug report for remaining issue.

Rik <rik5>
Project Administrator
Thu 08 Sep 2016 10:04:34 PM UTC, comment #3: 

I made the change you suggested to get the correct status from mexCallMATLAB in this cset (http://hg.savannah.gnu.org/hgweb/octave/rev/2fb86778f78d).

Rik <rik5>
Project Administrator
Mon 05 Sep 2016 04:32:21 PM UTC, comment #2: 

I actually conflated two issues in this bug report: one bug with mexCallMATLAB and a feature request with mexCallMATLABWithTrap.

  • bug report concerning mexCallMATLAB:

The integer value returned by mexCallMATLAB is always 1 even if an error has been caught. This can be fixed with the following (this is what mexEvalString does):

diff -r dc4e2203cd15 libinterp/corefcn/mex.cc
--- a/libinterp/corefcn/mex.cc  Mon Aug 29 14:32:22 2016 -0700
+++ b/libinterp/corefcn/mex.cc  Tue Aug 30 14:30:37 2016 +0100
@@ -3072,7 +3072,11 @@
   catch (const octave::execution_exception&)
     {
       if (mex_context->trap_feval_error)
-        recover_from_exception ();
+        {
+          recover_from_exception ();
+
+          execution_error = true;
+        }
       else
         {
           args.resize (0);

  • feature request for mexCallMATLABWithTrap:

Given that the try/catch ME currently returns a structure, the following could be used until a complete MException can be returned:

mxArray *mexCallMATLABWithTrap(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], const char *functionName) {
    mxArray *mx = NULL;
    const char **fields = (const char *[]){"identifier", "message", "case", "stack"};
    mexSetTrapFlag(1);
    if (mexCallMATLAB(nlhs, plhs, nrhs, prhs, functionName)) {
        mx = mxCreateStructMatrix(1, 1, 4, fields);
        mxSetFieldByNumber(mx, 0, 0, mxCreateString("MATLAB:error"));
        mxSetFieldByNumber(mx, 0, 1, mxCreateString(functionName));
        mxSetFieldByNumber(mx, 0, 2, mxCreateCellMatrix(0, 0));
        mxSetFieldByNumber(mx, 0, 3, mxCreateStructMatrix(0, 1, 0, NULL));
        return mx;
    }
    else {
        return NULL;
    }
}

Guillaume <gyom>
Tue 30 Aug 2016 01:37:27 PM UTC, comment #1: 

I've just made the following tiny change:

diff -r dc4e2203cd15 libinterp/corefcn/mex.cc
--- a/libinterp/corefcn/mex.cc  Mon Aug 29 14:32:22 2016 -0700
+++ b/libinterp/corefcn/mex.cc  Tue Aug 30 14:30:37 2016 +0100
@@ -3072,7 +3072,11 @@
   catch (const octave::execution_exception&)
     {
       if (mex_context->trap_feval_error)
-        recover_from_exception ();
+        {
+          recover_from_exception ();
+
+          execution_error = true;
+        }
       else
         {
           args.resize (0);

and it now returns a non zero value when an error is caught. The error message is still being displayed though:

octave:1> test_mexCallMATLAB('e')
error: fileread: cannot open file
status = 1.

Maybe the same approach than the one used by @oheim in patch #8033 can be used here.

Guillaume <gyom>
Tue 30 Aug 2016 10:42:32 AM UTC, original submission:  

It seems that mexCallMATLABWithTrap is not available from the Octave API:
  http://www.mathworks.com/help/matlab/apiref/mexcallmatlabwithtrap.html

I tried instead to use mexCallMATLAB with mexSetTrapFlag, even if its use is deprecated in MATLAB, as it seems to be implemented in Octave:
  http://www.mathworks.com/help/matlab/apiref/mexsettrapflag.html

There seems to be an issue with the integer value returned by mexCallMATLAB though:

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    int sts;

    mexSetTrapFlag(1); /* or 0 */
    sts = mexCallMATLAB(0, NULL, 1, (mxArray **)prhs, "fileread");
    mexPrintf("status = %d.\n", sts);
}

With mexSetTrapFlag(0), Octave gives:

octave:1> test_mexCallMATLAB('e')
error: fileread: cannot open file
error: called from
    fileread at line 37 column 5

With mexSetTrapFlag(0), MATLAB gives:

>> test_mexCallMATLAB('e')
Error using fileread (line 49)
Could not open file e. No such file or directory.

With mexSetTrapFlag(1), Octave gives:

octave:1> test_mexCallMATLAB('e')
error: fileread: cannot open file
status = 0.

With mexSetTrapFlag(1), MATLAB gives:

>> test_mexCallMATLAB('e')
Error using fileread (line 49)
Could not open file e. No such file or directory.

status = 1.

So it seems that when the Trap Flag is set to true, mexCallMATLAB is not set to a non-zero value when an error is caught.

From what I see, it seems that having an implementation of mexCallMATLABWithTrap might not be too difficult - the main issue being that MException is not implemented: what are the plans regarding this and should it be implemented in M-code or C++?

Guillaume <gyom>

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by gyom (Submitted the item)
  •  

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

    Only project members can vote.

     

     

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-09-08 rik5 StatusNone => Fixed
        Open/ClosedOpen => Closed
    2016-09-08 rik5 SummarymexCallMATLABWithTrap not implemented and problem with mexCallMATLAB/mexSetTrapFlag => mexCallMATLABWithTrap not implemented

    Back to the top


    Powered by Savane 3.5