SWIG: Python bug reports from C ++ code

I use a library that points out in its API documents to define a class inherited from some specific library class. The library itself is written in C ++, and Python bindings are created using SWIG. The problem is that when I run my Python code, no matter what exception Python throws, I get the error "call ended after calling the instance" Swig :: DirectorMethodException ".

I would like this exception to be thrown by Python code that will be displayed at runtime of my program. Esp, in cases where I get a 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 no luck. I still get the same message, “ending the call after calling the instance of“ Swig :: DirectorMethodException ””, regardless of what I put in the module.i file.

Can someone please give me instructions on how to do this, so that Python exceptions are reported like them?

+6
c ++ python exception swig
source share
2 answers

Report exception thrown by Python in the program console.

This is a helpful solution from Madhusudan.CS. See His comment on ginbot's answer. I put 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); } } 
+2
source share

I don’t know how far your code base is going, so this may be of little use, but I was lucky with boost :: python than SWIG . Then you can do this: boost :: python Export custom exceptions

+1
source share

All Articles