Reputation: 22438
I develop an interactive data analysis tool in which C++ high-performance kernels are wrapped via Cython and exposed as Python objects. This works very well in conjunction with IPython. However, a crash in the native code of the extension module (e.g. segmentation fault) will also crash the interpreter so that the entire session is lost. Is it possible to avoid this in any way? Could an error like a segmentation fault be caught as a proper Python exception?
Upvotes: 3
Views: 1268
Reputation: 22438
Let me add that many crashes can be avoided by having C++ standard exceptions be re-raised by Cython. This is done by appending except +
to the Cython class definition. Example:
cdef extern from "../src/Example.h":
cdef cppclass Example:
void method() except +
Upvotes: 0
Reputation: 40340
The faulthandler module (new in 3.3, but available for older versions) can print tracebacks on system-level errors.
Trying to handle the errors from Python code apparently doesn't work. At the C level, you can install a signal handler for SIGSEGV, but from the information I can find, what you can do there is quite limited. The basic message seems to be that you can shut down gracefully, but you can't recover from a segfault.
Upvotes: 1