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

[io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored #72353

Open
eryksun opened this issue Sep 15, 2016 · 3 comments
Open

[io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored #72353

eryksun opened this issue Sep 15, 2016 · 3 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows topic-IO type-bug An unexpected behavior, bug, or error

Comments

@eryksun
Copy link
Contributor

eryksun commented Sep 15, 2016

BPO 28166
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @ZackerySpytz, @ValeriyaSinevich
PRs
  • bpo-28166: Continue to read after Ctrl+C if SIGINT was ignored #8344
  • gh-72353: WindowsConsoleIO misbehavior when Ctrl+C is ignored #17976
  • 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-09-15.08:10:25.219>
    labels = ['interpreter-core', 'type-bug', '3.8', '3.9', 'expert-IO', '3.10', 'OS-windows']
    title = 'WindowsConsoleIO misbehavior when Ctrl+C is ignored'
    updated_at = <Date 2021-03-20.07:45:47.180>
    user = 'https://github.com/eryksun'

    bugs.python.org fields:

    activity = <Date 2021-03-20.07:45:47.180>
    actor = 'eryksun'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core', 'Windows', 'IO']
    creation = <Date 2016-09-15.08:10:25.219>
    creator = 'eryksun'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 28166
    keywords = ['patch']
    message_count = 3.0
    messages = ['276530', '321979', '322003']
    nosy_count = 7.0
    nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'ZackerySpytz', 'valer']
    pr_nums = ['8344', '17976']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28166'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @eryksun
    Copy link
    Contributor Author

    eryksun commented Sep 15, 2016

    _PyOS_WindowsConsoleReadline should continue to read when interrupted by Ctrl+C if SIGINT is ignored or the handler doesn't raise an exception. Currently it breaks out of the read loop, which looks like an EOF:

        >>> import signal
        >>> signal.signal(signal.SIGINT, signal.SIG_IGN)
        <built-in function default_int_handler>
        >>> input()
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        EOFError 

    Also, in this case Ctrl+C quits the REPL.

    Similarly sys.stdin.buffer.raw should retry a read. Instead a read(1) returns an empty string and readall() raises an exception:

        >>> sys.stdin.buffer.raw.read(1)
        b''
    
        >>> sys.stdin.buffer.raw.read()
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        OSError: [WinError 87] The parameter is incorrect

    @eryksun eryksun added stdlib Python modules in the Lib dir 3.7 (EOL) end of life OS-windows topic-IO type-bug An unexpected behavior, bug, or error labels Sep 15, 2016
    @ValeriyaSinevich
    Copy link
    Mannequin

    ValeriyaSinevich mannequin commented Jul 20, 2018

    I posted a PR but the problem with it is that all the input before the ignored Ctrl+C is lost.

    @eryksun
    Copy link
    Contributor Author

    eryksun commented Jul 20, 2018

    all the input before the ignored Ctrl+C is lost

    The console host process doesn't know that Python is ignoring Ctrl+C, so it cancels the read and flushes the input buffer. For now, we have to accept this as a limitation of relying on the high-level console API (i.e. ReadConsoleW with the default settings), which implements command-line editing, aliases and history completely in the console host process. Restarting the read with a flushed input buffer is better than raising EOFError or exiting the REPL.

    In the future, Python's console readline implementation could hook deeper into the operation by temporarily disabling the console's ENABLE_PROCESSED_INPUT mode and using the pInputControl parameter to handle ^C and ^H (backspace). This would be fairly involved but still an imperfect solution because ^C will replace whichever character is under the cursor (fine if the cursor is at the end of the line, but otherwise pretty clunky). The only way to have complete control is to use the low-level ReadConsoleInput function. pyreadline implements this via ctypes.

    @ZackerySpytz ZackerySpytz mannequin added 3.8 only security fixes 3.9 only security fixes labels Jan 13, 2020
    @eryksun eryksun added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes and removed stdlib Python modules in the Lib dir 3.7 (EOL) end of life labels Mar 20, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @eryksun eryksun changed the title WindowsConsoleIO misbehavior when Ctrl+C is ignored [io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored Aug 8, 2022
    @erlend-aasland erlend-aasland added 3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes and removed 3.10 only security fixes 3.9 only security fixes 3.8 only security fixes labels Jan 5, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-windows topic-IO type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants