Stop embedded Python prompt from C++
<p>I'm running Python embedded in a C++ application. The program consists of a Qt GUI and a work QThread where computations happen. The user can chose to run a Python script from a file or to start a Python prompt. Both run in the QThread. The program exits when the python script is done or when we exit the python prompt. However, I want to handle the case when the user requests to quit from the GUI. </p>

<p>If running a Python script I can achieve this by a call to <code>PyErr_SetInterrupt</code>(see <a href="http://stackoverflow.com/questions/1420957/stopping-embedded-python">Stopping embedded Python</a>). The Python interpreter stops, and we can quit normally. However, I haven't found a good way to force the Python prompt to quit.</p>

<p>I've tried feeding characters to <code>stdin</code> (with <code>ungetc</code>) in the hopes that the Python prompt receives them but without success. I do this from an overloaded <code>PyOS_InputHook</code> that I use to catch Qt events while the Python prompt is running.</p>

<p>Any ideas how to properly force the Python prompt to exit?</p>

<p>Can you not set up a slot receiver on your <code>QThread</code> subclass that will call <code>PyErr_SetInterrupt</code> in the proper context for you?</p>

<p>You might achieve cleaner separation-of-concerns if, instead of using QThreads, you run your embedded Python interpreter in a separate process â which you can then <code>kill</code> it as you see fit, or at least you can if youâre on a POSIX-y platform; I take it this type of operation is way different on e.g. Windows.</p>

python c++ python-c-api embedded-language