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

KeyboardInterrupt while in input() not catchable on Windows 10 #70718

Open
jecanne mannequin opened this issue Mar 10, 2016 · 9 comments
Open

KeyboardInterrupt while in input() not catchable on Windows 10 #70718

jecanne mannequin opened this issue Mar 10, 2016 · 9 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@jecanne
Copy link
Mannequin

jecanne mannequin commented Mar 10, 2016

BPO 26531
Nosy @pfmoore, @vstinner, @tjguk, @vadmium, @zware, @zooba
Files
  • keyint_poc.py: Proof of Concept
  • ccbug.py
  • 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 = None
    created_at = <Date 2016-03-10.16:18:45.880>
    labels = ['type-bug', 'OS-windows']
    title = 'KeyboardInterrupt while in input() not catchable on Windows 10'
    updated_at = <Date 2016-03-11.16:14:12.441>
    user = 'https://bugs.python.org/jecanne'

    bugs.python.org fields:

    activity = <Date 2016-03-11.16:14:12.441>
    actor = 'jecanne'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Windows']
    creation = <Date 2016-03-10.16:18:45.880>
    creator = 'jecanne'
    dependencies = []
    files = ['42117', '42120']
    hgrepos = []
    issue_num = 26531
    keywords = []
    message_count = 9.0
    messages = ['261508', '261509', '261511', '261512', '261513', '261515', '261520', '261523', '261578']
    nosy_count = 8.0
    nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'martin.panter', 'zach.ware', 'steve.dower', 'tjguk', 'jecanne']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26531'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    @jecanne
    Copy link
    Mannequin Author

    jecanne mannequin commented Mar 10, 2016

    There seems to be an issue capturing keyboard interrupts on Windows 10 when using raw_input() inside of a class method w/python 2.7.x.

    So far I have tested this on:
    2.7.11 x86 on Windows 10 x64 - Does not capture (Traceback)
    2.7.11 x64 on Windows 10 x64 - Does not capture (Traceback)
    2.7.11 x86 on Windows XP - Does capture

    So far I've only tested 2.7.11, so I don't know which other version may or may not be affected. The proof of concept code is attached.

    If there is anything else I can provide to help, please let me know. I'm not accustomed to posting bugs here so I apologize if I've missed something.

    @jecanne jecanne mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Mar 10, 2016
    @tjguk
    Copy link
    Member

    tjguk commented Mar 10, 2016

    I don't have Win10 to test with, but can you confirm that the method is
    necessary for the code to fail? IOW does this fail equally?

    while True:
        try: raw_input()
        except KeyboardInterrupt: print "Interrupt!"

    Also, do I assume that the code simply carries on regardless without
    reaching the exception handler? Or does something else happen?

    @vstinner
    Copy link
    Member

    Please try Python 3, I recall that we fixed bugs on input() on Windows related to CTRL+c.

    @jecanne
    Copy link
    Mannequin Author

    jecanne mannequin commented Mar 10, 2016

    Hi,

    Thanks for the quick response.

    I tried your code, it doesn't capture the keyboard interrupt, so the method doesn't seem to be needed. Also, the code doesn't carry on if interrupt was received, but instead produces the stack traceback. For example:

    while True:
        try:
            data = raw_input()
            print data
        except KeyboardInterrupt:
            print "Interrupt!"
        
    Does not print "data" if a KeyboardInterrupt is received during raw_input.

    Unfortunately, I can't use python 3 as my organization currently uses 2 in their production environment.

    @tjguk
    Copy link
    Member

    tjguk commented Mar 10, 2016

    If I run your code, it does what I expect: pressing Ctrl-C produces
    "Interrupt", doesn't print the data returned from raw_input, and doesn't
    produce a stacktrace.

    I'm not on Win10 and it's entirely possible that this area of the MS CRT
    has been reworked: it's a bit quirky and our use of it is fairly fragile.

    I'm still not clear, though, whether we have a bug or whether it's just
    that your expectation is at odds with the way in which this is intended
    to work.

    If you think there's a bug here, could you run the code you posted in a
    console window, do what actions are needed to make it fail, and then
    cut-and-paste the result here, possibly as an attached file. (Don't post
    a screenshot; just use the cmd window's mark-and-copy functionality to
    select the visible text).

    @zware
    Copy link
    Member

    zware commented Mar 10, 2016

    I can reproduce on Windows 10 with Pythons 2.7.11, 3.4.4, and 3.5.1 with the attached script. Examples:

    E:\>ver

    Microsoft Windows [Version 10.0.10586]

    E:\>py ccbug.py
    2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)]
    Traceback (most recent call last):
      File "ccbug.py", line 12, in <module>
        data = input()
    
    
    E:\>py -3.4 ccbug.py
    3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)]
    Traceback (most recent call last):
      File "ccbug.py", line 12, in <module>
    
    E:\>py -3.5 ccbug.py
    3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]
    Traceback (most recent call last):
      File "ccbug.py", line 12, in <module>

    @zware zware changed the title Keyboard Interrupts not caught when used within a class method on Windows 10 KeyboardInterrupt while in input() not catchable on Windows 10 Mar 10, 2016
    @jecanne
    Copy link
    Mannequin Author

    jecanne mannequin commented Mar 10, 2016

    I can post a script if necessary, but it looks like Zach has already done so. I appreciate you verifying this.

    @vadmium
    Copy link
    Member

    vadmium commented Mar 10, 2016

    It sounds like you guys might be (re-)discovering existing bugs to do with KeyboardInterrupt in input on Windows:

    • bpo-18597 (Py 2 & 3): Strange exception handling reading stdin
    • bpo-10837 (2.7): KeyboardInterrupt deferred reading stdin
    • bpo-25376 (Py 3): Truncated traceback from input()
    • bpo-13673 (Py 2 & 3): General traceback truncated if interrupted

    @jecanne
    Copy link
    Mannequin Author

    jecanne mannequin commented Mar 11, 2016

    Yeah, so this looks like a behavior in Windows in which an EOFError is raised when a Ctrl + C is pressed, followed by a KeyboardInterrupt.

    This post basically explains how to overcome it: http://stackoverflow.com/questions/31127652/cannot-catch-keyboardinterrupt-in-command-prompt-twice

    def bing():
        try:
            input()
        except EOFError:
            print("Caught an EOFError")

    try:
    bing()
    print("After bing")
    except KeyboardInterrupt:
    print("Final KeyboardInterrupt")

    Sorry for necro-ing an old 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 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants