Aborting Python from C code

Chris Angelico rosuav at gmail.com
Fri Apr 29 03:48:52 CEST 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