This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Issue catching KeyboardInterrupt while reading stdin
Type: behavior Stage: resolved
Components: IO, Windows Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Devin Jeanpierre, Josh.Hanson, iritkatriel, tim.golden
Priority: normal Keywords:

Created on 2011-01-05 20:02 by Josh.Hanson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg125463 - (view) Author: Josh Hanson (Josh.Hanson) Date: 2011-01-05 20:02
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.
msg131629 - (view) Author: Devin Jeanpierre (Devin Jeanpierre) * Date: 2011-03-21 10:47
I can confirm this behavior on 2.7. On 3.2 for me it prints "done.", but not "Interrupted!"
msg131630 - (view) Author: Devin Jeanpierre (Devin Jeanpierre) * Date: 2011-03-21 10:52
Sorry, forgot to mention my system. 64-bit Windows 7.
msg222888 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-12 22:05
I can confirm that this works fine with 3.4.1 and 3.5.0a0.  I don't run 2.7 any more so I don't know if this is fixed in later versions.
msg382148 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-30 13:39
Python 2-only issue.
History
Date User Action Args
2022-04-11 14:57:10adminsetgithub: 55046
2020-11-30 13:39:56iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg382148

resolution: out of date
stage: resolved
2019-04-26 19:06:49BreamoreBoysetnosy: - BreamoreBoy
2014-07-12 22:13:49brian.curtinsetnosy: - brian.curtin
2014-07-12 22:05:07BreamoreBoysetnosy: + BreamoreBoy

messages: + msg222888
versions: - Python 2.6, Python 3.1
2011-03-21 10:52:12Devin Jeanpierresetnosy: tim.golden, Devin Jeanpierre, brian.curtin, Josh.Hanson
messages: + msg131630
2011-03-21 10:47:04Devin Jeanpierresetnosy: + Devin Jeanpierre
messages: + msg131629
2011-01-08 06:04:09ned.deilysetnosy: + tim.golden, brian.curtin
2011-01-05 20:02:04Josh.Hansoncreate