[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:
    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 <module>
        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.

----------
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>
<http://bugs.python.org/issue10837>
_______________________________________


More information about the New-bugs-announce mailing list