[Python/C API] How to break a script execution ?

Mark Hammond mhammond at skippinet.com.au
Fri Jan 17 02:19:58 CET 2003

Misiek wrote:
> Hello everybody,
> I have written a MFC application that allows to run Python's scripts with
> using Python/C API. All works perfectly, but I don't know how to break a
> script execution on demand (something like pressing Ctrl+C in Python).
> Suppose I have a script that simply execute an endless loop and I would like
> to stop it execution by clicking a button in my application and then dump to
> my application an iteration count. Here is an example script:
> def main():
>     try:
>         i = 0
>         while 1:
>             i = i + 1
>     except KeyboardInterrupt:
>         print "Iteration: ", i
> main()
> Is there any possibility to break a script execution from my application and
> catch the exception? How to do it? I would be grateful for any
> suggestions...

Pythonwin is just an MFC app that embeds Python <wink>.  I discovered 
that raising an exception from a different C thread generally had the 
desired results.  The only way I could manage a seperate thread was a 
seperate Window - once this was a real top-level window, but now we have 
the taskbar icon.


More information about the Python-list mailing list