You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored #72353
_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
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.
changed the title [-]WindowsConsoleIO misbehavior when Ctrl+C is ignored[/-][+][io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored[/+]on Aug 8, 2022
Activity
eryksun commentedon 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> EOFErrorAlso, 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 incorrectValeriyaSinevich commentedon 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 commentedon Jul 20, 2018
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.
[-]WindowsConsoleIO misbehavior when Ctrl+C is ignored[/-][+][io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored[/+]gh-72353: Continue reading from the console after ignored Ctrl+C (GH-…
auwallpatil commentedon Aug 29, 2026
[3.14] gh-72353: Continue reading from the console after ignored Ctrl…
[3.13] gh-72353: Continue reading from the console after ignored Ctrl…
pythongh-72353: Continue reading from the console after ignored Ctrl+C (
[3.15] gh-72353: Continue reading from the console after ignored Ctrl…