Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue catching KeyboardInterrupt while reading stdin #55046

Closed
JoshHanson mannequin opened this issue Jan 5, 2011 · 5 comments
Closed

Issue catching KeyboardInterrupt while reading stdin #55046

JoshHanson mannequin opened this issue Jan 5, 2011 · 5 comments
Labels
OS-windows topic-IO type-bug An unexpected behavior, bug, or error

Comments

@JoshHanson
Copy link
Mannequin

JoshHanson mannequin commented Jan 5, 2011

BPO 10837
Nosy @tjguk, @ssbr, @iritkatriel

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-11-30.13:39:56.408>
created_at = <Date 2011-01-05.20:02:04.675>
labels = ['type-bug', 'expert-IO', 'OS-windows']
title = 'Issue catching KeyboardInterrupt while reading stdin'
updated_at = <Date 2020-11-30.13:39:56.407>
user = 'https://bugs.python.org/JoshHanson'

bugs.python.org fields:

activity = <Date 2020-11-30.13:39:56.407>
actor = 'iritkatriel'
assignee = 'none'
closed = True
closed_date = <Date 2020-11-30.13:39:56.408>
closer = 'iritkatriel'
components = ['Windows', 'IO']
creation = <Date 2011-01-05.20:02:04.675>
creator = 'Josh.Hanson'
dependencies = []
files = []
hgrepos = []
issue_num = 10837
keywords = []
message_count = 5.0
messages = ['125463', '131629', '131630', '222888', '382148']
nosy_count = 4.0
nosy_names = ['tim.golden', 'Devin Jeanpierre', 'Josh.Hanson', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10837'
versions = ['Python 2.7']

@JoshHanson
Copy link
Mannequin Author

JoshHanson mannequin commented Jan 5, 2011

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.

@JoshHanson JoshHanson mannequin added OS-windows topic-IO type-bug An unexpected behavior, bug, or error labels Jan 5, 2011
@ssbr
Copy link
Mannequin

ssbr mannequin commented Mar 21, 2011

I can confirm this behavior on 2.7. On 3.2 for me it prints "done.", but not "Interrupted!"

@ssbr
Copy link
Mannequin

ssbr mannequin commented Mar 21, 2011

Sorry, forgot to mention my system. 64-bit Windows 7.

@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Jul 12, 2014

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.

@iritkatriel
Copy link
Member

Python 2-only issue.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows topic-IO type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant