Issue 10837: Issue catching KeyboardInterrupt while reading stdin (original) (raw)
Example code: try: sys.stdin.read() except KeyboardInterrupt: print "Interrupted!" except: print "Some other exception?" finally: print "cleaning up..." print "done."
Test: run the code and hit ctrl-c while the read is blocking. Expected behavior: program should print: Interrupted! cleaning up... done.
Actual behavior: On linux, behaves as expected. On windows, prints: cleaning up... Traceback (most recent call last): File "filename.py", line 119, in print 'cleaning up...' KeyboardInterrupt
As you can see, neither of the "except" blocks was executed, and the "finally" block was erroneously interrupted.
If I add one line inside the try block, as follows: try: sys.stdin.read() print "Done reading." ... [etc.]
Then this is the output: Done reading. Interrupted! cleaning up... done.
Here, the exception handler and finally block were executed as expected. This is still mildly unusual because the "done reading" print statement was reached when it probably shouldn't have been, but much more surprising because a newline was not printed after "Done reading.", and for some reason a space was.
This has been tested and found in 32-bit python versions 2.6.5, 2.6.6, 2.7.1, and 3.1.3 on 64-bit Win7.