sys.exit() doesn't exit

Randall Hopper aa8vb at yahoo.com
Wed Oct 20 08:42:20 EDT 1999


Guido van Rossum:
 |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 
...
 |> 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.

Thanks.  That was the advice I needed.

The wrapped C code doesn't know about Python so propagating exceptions
through it really isn't practical.  I guess the best I can do is propagate
them across it.

Fortunately, in this particular C wrapper, there are only two C entry
points inside which a Python callback might be invoked, so I can use
static-wrapper-variable-hackery to record when exceptions occur, and return
NULL in the appropriate spots to the controlling Python.  It's not pretty,
but it works.

With Python using return values to signal exceptions, I guess this is this
is the best I can do.

Thanks,

Randall

-- 
Randall Hopper
aa8vb at yahoo.com




More information about the Python-list mailing list