octave.cxx: fix exception raising for newer Octave versions

- Since (at least) Octave 5.1.0, the Octave error() function now raises a C++
  exception, which if uncaught immediately exits a SWIG wrapper function,
  bypassing any cleanup code that may appear after a "fail:" label.
- This patch adds a "try { ... } catch(...) { }" block around the contents of
  SWIG wrapper functions to first execute the cleanup code before rethrowing any
  exception raised.
- It is backward compatible with earlier versions of Octave where error() does
  not raise an exception, which will still branch to the "fail:" block to
  execute cleanup code if an error is encountered.
This commit is contained in:
Karl Wette 2020-05-02 03:35:05 +10:00
commit d9c94848ec
2 changed files with 32 additions and 1 deletions

View file

@ -567,6 +567,10 @@ public:
Wrapper *f = NewWrapper();
Octave_begin_function(n, f->def, iname, overname, !overloaded);
// Start default try block to execute
// cleanup code if exception is thrown
Printf(f->code, "try {\n");
emit_parameter_variables(l, f);
emit_attach_parmmaps(l, f);
Setattr(n, "wrap:parms", l);
@ -754,9 +758,20 @@ public:
}
Printf(f->code, "return _out;\n");
Printf(f->code, "fail:\n"); // we should free locals etc if this happens
// Execute cleanup code if branched to fail: label
Printf(f->code, "fail:\n");
Printv(f->code, cleanup, NIL);
Printf(f->code, "return octave_value_list();\n");
// Execute cleanup code if exception was thrown
Printf(f->code, "}\n");
Printf(f->code, "catch(...) {\n");
Printv(f->code, cleanup, NIL);
Printf(f->code, "throw;\n");
Printf(f->code, "}\n");
// End wrapper function
Printf(f->code, "}\n");
/* Substitute the cleanup code */