Aborting Python from C code

Chris Angelico rosuav at gmail.com
Thu Apr 28 21:48:52 EDT 2011


In our sandboxed Python environment, I would like to be able to
trigger an abort of the currently-running Python script (from a signal
handler or another thread). Currently, I use PyErr_SetInterrupt()
which raises KeyboardInterrupt in the script; this works for the most
part, but can be bypassed with a script like:

while True:
    try:
        while True:
            pass
    except:
        pass

I want to be able to administratively order Python to stop executing.
Is it possible to make KeyboardInterrupt non-catchable from Python?
Alternatively, can I use Py_AddPendingCall to do this? I'd rather not
use setjmp/longjmp if I can help it, as they seem a tad brutal, but
I'm thinking in terms of a call that, in effect, doesn't return.

Any ideas would be appreciated!

Chris Angelico



More information about the Python-list mailing list