1

Does Python's C API have any means to directly return to the Python interpreter from an arbitrary point in the C code, and ideally raise an exception? Internally this would likely use some sort of longjmp.

Use case:

This would be used to handle fatal errors (such as assertion failures) raised by a C library that is hooked up to Python. By default, on such fatal errors the library abort()s, which terminates the Python interpreter. I am looking for a last-resort way to handle these errors without terminating Python.

Ideally, one would do a setjmp before calling the C library, however, doing this for literally hundreds of manually written wrapper functions that hook up the library to Python is not feasible at the moment. (The proper solution would be to use an interface generator which makes it easy to insert the setjmp consistently and automatically—however, that is a long-term project and now I am looking for a quick way to make an improvement.)

R's C API provides an error() function that returns to the R interpreter directly and prints an error. Does Python's C API has anything comparable?

EDIT: The existence of this very old (~2001) package suggests that it might be possible. The package is unfortunately no longer available, only its description.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174

0 Answers0