Triggering a KeyboardInterrupt in a PyObject_CallObject
Hi list. I have a situation where I make a PyObject_CallObject call on one thread, and then I watch for keyboard input on a separate thread. When the user hits CTRL-C, this second thread will see it and needs to make it so that the callable being run on the first thread gets "stopped" with a KeyboardInterrupt exception I tried calling Py_AddPendingCall() on the second thread, and passing it a pointer to a function foo.
My first attempt was to make foo call PyErr_SetInterrupt() and PyErr_CheckSignals().
Since this did not work, I rewrote foo to explicitly set the KeyboardInterrupt exception with PyErr_SetString() and returning -1 This worked in the sense that I saw the KeyboardInterrupt come out on screen as a traceback, but it did not stop the function from running.
Am I doing something wrong? Is there even a way to raise an exception "inside" a PyObject_CallObject without the callee doing anything? It would be much preferable that the called python object did not have to do anything in order to be interruptible, but I would be willing to compromise on some simple "make me interruptible" pattern, provided it really is simple.
Thanks for any help.
Enrico Granata ✉ egranata@.com ✆ (408) 972-7683
On Oct 10, 2012, at 11:24 AM, Enrico Granata egranata@apple.com wrote:
Am I doing something wrong? Is there even a way to raise an exception "inside" a PyObject_CallObject without the callee doing anything? It would be much preferable that the called python object did not have to do anything in order to be interruptible,
Maybe this is what you're looking for:
http://docs.python.org/c-api/init.html?highlight=async#PyThreadState_SetAsyn...
Tho, I'm not sure if it's a good idea for your particular case…
participants (2)
-
Enrico Granata
-
James William Pye