Search code examples
pythonpython-2.7keyboardinterruptwing-ide

Why won't KeyboardInterrupt work in Python when using Wing IDE 101?


I'm trying to do something in a loop (in this example print to 100) and cancel the count at any time by pressing 'Ctrl+C'.

The test code I'm using is below, however this doesn't work (EDIT: it works in a script launched from terminal but does not work when run in the python shell of my IDE - Wing 101). KeyboardInterrupt doesn't catch the 'Ctrl+C' command or any other key.

import time     

i = 1
while (i < 101):
    try:
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
    except KeyboardInterrupt:
        break 

I've read this question here and I'm pretty sure that's not my problem because I've put the 'time.sleep(0.5)' command in to slow the program down. I've also read this question but since it's 5 years old now I assume this bug would have been fixed by now? I know that I can use threading to achieve what I want, but if only for the sake of learning I'd like to know why this method doesn't work.

I'm using python 2.7.6 in Ubuntu 14.04 and I'd appreciate any help or advice in solving this issue. (EDIT: I know the code works in isolation, but I'd still be interested in knowing why it doesn't work in the python shell of Wing 101 IDE)

EDIT 1

I've tried putting the while loop inside the try block, as suggested:

try:
    i = 1
    while (i < 101):        
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
except KeyboardInterrupt:
    break 

But unfortunately that doesn't work either, instead I get the following error:

Traceback (most recent call last):
  File "/home/matt/autosys_repo1/python_test_scripts/test_refresh_line.py", line 13, in <module>
    break
'break' outside loop: <string>, line 13

Solution

  • This is a limitation of Wing IDE 101. The only way to stop a loop like this is to restart the shell.