[New-bugs-announce] [issue10837] Issue catching KeyboardInterrupt while reading stdin

Josh Hanson report at bugs.python.org
Wed Jan 5 21:02:05 CET 2011

New submission from Josh Hanson <surely.you.jest at gmail.com>:

Example code:
    except KeyboardInterrupt:
        print "Interrupted!"
        print "Some other exception?"
        print "cleaning up..."
        print "done."

Test: run the code and hit ctrl-c while the read is blocking.
Expected behavior: program should print:
    cleaning up...

Actual behavior: On linux, behaves as expected. On windows, prints:
    cleaning up... 
    Traceback (most recent call last):
      File "filename.py", line 119, in <module>
        print 'cleaning up...'

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:
        print "Done reading."
    ... [etc.]

Then this is the output:
    Done reading. Interrupted!
    cleaning up...

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.

components: IO, Windows
messages: 125463
nosy: Josh.Hanson
priority: normal
severity: normal
status: open
title: Issue catching KeyboardInterrupt while reading stdin
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list