I am using a library, which specifies in its API docs to define a class inherited from some particular class of of the library. The library itself is written in C++ and the bindings to Python is generated using SWIG. The problem is, when I run my Python code, no matter what exception Python throws, I get the error saying "terminate called after throwing an instance of 'Swig::DirectorMethodException'".
I would like to have this exception raised by the Python code to be reported while executing my program. Esp, those cases where I get ZeroDivisionError.
I tried to hack a bit by following the method described in the SWIG documentation at http://www.swig.org/Doc2.0/Python.html#Python_nn36 but with no luck. I still get the same message "terminate called after throwing an instance of 'Swig::DirectorMethodException'" no matter what I put in the module.i file.
Can some one please give me pointers on how to go about with this problem, so that Python exceptions are reported as they are?
Report exception raised by Python in the console of the program.
This is the useful fix from Madhusudan.C.S. See his comment on ginbot's answer. I am putting it as an answer so that it becomes more visible.
/* MyInterface.i */
%module(directors="1") MyInterface
%feature("director:except") {
if( $error != NULL ) {
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch( &ptype, &pvalue, &ptraceback );
PyErr_Restore( ptype, pvalue, ptraceback );
PyErr_Print();
Py_Exit(1);
}
}
I don't know how far along you are with your code base, so this may be of little use, but I had better luck with boost::python than SWIG. Then you could do this: boost::python Export Custom Exception
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With