classification
Title: KeyboardInterrupt handling and traceback broken on Windows 10
Type: behavior Stage:
Components: Interpreter Core, Windows Versions: Python 3.4, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: martin.panter, mdf, paul.moore, r.david.murray, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2015-10-11 12:32 by mdf, last changed 2015-10-13 04:40 by martin.panter.

Messages (5)
msg252800 - (view) Author: Miikka Salminen (mdf) Date: 2015-10-11 12:32
Pressing Ctrl+C to raise a KeyboardInterrupt while waiting for user input in an input() call yields an incomplete traceback.

The behaviour appears in a Python REPL session started by issuing "python" without quotes in a Windows cmd session:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> input("Question: ")
Question: Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
>>>

When executing input_test.py (a single line Python source code file containing just an input("Question: ") call) from the cmd prompt by issuing "python input_test.py" without quotes, every other time the traceback is incomplete and every other time the Ctrl+C keypress yields, erroneously, an EOFError instead of a KeyboardInterrupt:

C:\x>python input_test.py
Question: Traceback (most recent call last):
  File "input_test.py", line 1, in <module>
    input("Question: ")
EOFError
^C
C:\x>python input_test.py
Question: Traceback (most recent call last):
  File "input_test.py", line 1, in <module>

C:\x>
msg252801 - (view) Author: Miikka Salminen (mdf) Date: 2015-10-11 12:40
Just tried the same in an IDLE session, but it seems to display the KeyboardInterrupt correctly:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> input("Question: ")
Question: 
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    input("Question: ")
  File "C:\y\Python\Python35\lib\idlelib\PyShell.py", line 1385, in readline
    line = self._line_buffer or self.shell.readline()
KeyboardInterrupt
>>> 

Running the file input_test.py I meantioned in my original report doesn't seem to diplay anything when Ctrl+C is pressed:

============= RESTART: C:\x\input_test.py =============
Question: 
>>>
msg252805 - (view) Author: Miikka Salminen (mdf) Date: 2015-10-11 14:14
Did some more testing. KeyboardInterrupt traceback displays properly when pressing Ctrl+C during an infinite loop:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> while True:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>>

Also when running a file in a similar way as I did in the original bug report:

C:\x>python infinite_loop.py
Traceback (most recent call last):
  File "infinite_loop.py", line 2, in <module>
    pass
KeyboardInterrupt

C:\x>

Could this mean that the problem is more related to reading stdin than the actual KeyboardInterrupt?
msg252816 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-10-11 15:36
Yes, this is probably a duplicate of issue 18597.
msg252912 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-10-13 04:40
I am no Windows expert, but I read in Issue 18597 that Ctrl+C is treated as EOF, except that a SIGINT is also sent in the background. This would explain why you see an EOFError. The partial traceback may also be from the same EOFError. Or both cases could be truncated reports of KeyboardInterrupt chained onto EOFError.

I understand the code path for input() may bypass the usual stdin.read() and readline() methods. Issue 18597 is about readline(), so it may not be an exact duplicate.

See also Issue 17619, which hints that KeyboardInterrupt handling for input() was worked on and fixed in 3.3. So why is it broken again in 3.4? A regression test would be nice, if at all possible. Does Windows have an equivalent of pseudo terminals?

Possibly also related is Issue 13673, about functions (like input) raising an exception (like EOFError), when SIGINT has been received. It mentions the interpreter completely failing to report the entire traceback, or truncating the traceback. It might be worth trying out the patch there. It might also be interesting to check sys.last_value in the interactive case.
History
Date User Action Args
2015-10-13 04:40:06martin.pantersetnosy: + martin.panter
messages: + msg252912
2015-10-11 15:36:06r.david.murraysetnosy: + r.david.murray
messages: + msg252816
2015-10-11 14:14:49mdfsetmessages: + msg252805
2015-10-11 12:40:36mdfsetmessages: + msg252801
2015-10-11 12:32:24mdfcreate