On Tue, Oct 20, 2009 at 6:09 PM, Maxim Khitrov mkhit...@gmail.com wrote:
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.
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
Replying to my own post... The code below seems to fix the problem,
though it is obviously a hack I would rather do without. I have no
idea if 50ms is enough to wait for a KeyboardInterrupt, but so far
I've not encountered any inconsistent behavior.
import __builtin__
import time
def raw_input2(prompt=''):
Workaround for raw_input raising EOFError and KeyboardInterrupt on
Ctrl-C.
try:
return raw_input1(prompt)
except EOFError as exc:
# If KeyboardInterrupt is not raised in 50ms, it's a real EOF
event.
time.sleep(0.05)
raise
raw_input1 = raw_input
__builtin__.raw_input = raw_input2
try:
raw_input()
except BaseException as exc:
print type(exc)
--
http://mail.python.org/mailman/listinfo/python-list