sys.exit() doesn't exit

Guido van Rossum guido at cnri.reston.va.us
Tue Oct 19 17:05:06 EDT 1999


Randall Hopper <aa8vb at yahoo.com> writes:

> I have this situation:
> 
>     A Python script (makes calls to:)
>     |
>     - A C library  (which issues callbacks back to:)
>       |
>       - The Python script (which invokes:)
>         |
>         - sys.exit()
> 
> Calling sys.exit() in the Python callback doesn't terminate the
> application.  Apparently, the SystemExit exception gets lost crossing the C
> library frames in the stack.  What's really going on though?
> 
> If I want to throw an exception in a Python callback like this, is there a
> way for it to unwind the stack across the C library and trigger in the
> controlling Python stack frames?

Most likely the C code isn't properly propagating Python exceptions.
Perhaps it is calling PyErr_Clear().  Perhaps it is not checking for
errors at all.  If the C code checks for a NULL return from the Python
callback and in that case returns NULL itself to the outer Python
script, the exception will be properly passed through.

Alternatively, the C code could call PyErr_Print(), which will call
exit() when it sees SystemExit.

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list