Any idea of stopping the execution of PyRun_File()

Gabriel Genellina gagsl-py2 at
Tue Jun 9 19:41:25 EDT 2009

En Mon, 08 Jun 2009 22:15:22 -0300, BigHand <heweiwei at> escribió:

>    I have an embedded python application. which is  a MFC app with
> Python interpreter embedded.
>    In the App, I have a separate thread to execute a Python script
> (using the PyRun_File), but if the user want to stop the executing
> script, how should I do?
> A possible way is terminate the thread of executing the scripts by
> TerminateThread(). but TerminateThread() is unsafe and not
> recommended.
> guys, could you guide me on this?

You could use PyThreadState_SetAsyncExc (to "inject" an exception), or  
perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a  
KeyboardInterrupt). This should work fine for well-behaved scripts, but if  
it ignores all exceptions like this:
	try: ...
	except: pass
you'll have to look at ceval.c how to break out of the running loop.

(this is yet another argument against indiscriminately using a bare except  

Gabriel Genellina

More information about the Python-list mailing list