[PyQt] Raise exception from event handler

Jack Cosgrove jackcosgrove at gmail.com
Sat May 28 22:56:21 BST 2011

I am making a shell for PyQt. I have an odd program architecture.

The Qt event loop is initiated by my shell, which is written in C++. The
shell executes a block of Python code using PyRun_String and its sister

The behavior I want is to end this execution by raising a
KeyboardInterrupt when a user presses a certain button during execution.

I can catch the button press in the event handler, but raising a
KeyboardInterrupt from this location consumes the event somewhere along
the way so that execution does not stop. I know Qt does not support
raising exceptions from signals, slots, and event handlers, and I am at
a loss as to go about raising this exception and terminating the Python
interpreter execution upon a button press.

The exception is reported by printing out all errors, so it is consumed
somewhere by the Python interpreter but not in a place that ends

I can stop execution if I hard-code raising the exception into the
Python code being executed, i.e. not in an event handler.

Any ideas how to stop execution of the PyRun_* functions when something
is triggered in a PyQt event handler?

More information about the PyQt mailing list