Inconsistent raw_input behavior after Ctrl-C

M

Maxim Khitrov

Hello all,

I ran into a rather strange problem when interrupting a raw_input call
with Ctrl-C. This is with python 2.6.3 on Windows 7. When the call is
interrupted, one of two things happen - either a KeyboardInterrupt
exception is raised or raw_input raises EOFError, and
KeyboardInterrupt is raised a line or two later. Here's the example
that I'm testing with:

import sys
import traceback

print '==='
excs = []

try:
try:
raw_input()
except BaseException as exc:
excs.append(sys.exc_info())
print '1', type(exc)
except BaseException as exc:
excs.append(sys.exc_info())
print '2', type(exc)

print '---'

for exc in excs:
traceback.print_exception(*exc)

print '==='

And here are all the two different outputs that I've received at random times:

===
1 <type 'exceptions.KeyboardInterrupt'>
---
Traceback (most recent call last):
File "client.py", line 26, in <module>
raw_input()
KeyboardInterrupt
===

===
1 2 <type 'exceptions.KeyboardInterrupt'>
---
Traceback (most recent call last):
File "client.py", line 26, in <module>
raw_input()
EOFError
Traceback (most recent call last):
File "client.py", line 29, in <module>
print '1', type(exc)
KeyboardInterrupt
===

This makes no sense to me. First, where does raw_input get EOF (Ctrl-Z
or F6) from? Second, why is KeyboardInterrupt raised in the middle of
executing a print instruction and not at raw_input? Third, if the
inner except clause finishes too soon (for example, if I comment out
the print statement), then the KeyboardInterrupt is sometimes raised
at the print '---' line. This makes it difficult to consistently
handle a Ctrl-C event without calling something like sleep after an
EOFError.

I don't recall seeing this problem in Windows XP, but I'm not able to
test on it right now. Is this problem related to Windows 7 in some
way?

- Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top