Inconsistent raw_input behavior after Ctrl-C
mkhitrov at gmail.com
Wed Oct 21 01:22:49 CEST 2009
On Tue, Oct 20, 2009 at 6:09 PM, Maxim Khitrov <mkhitrov at 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
> 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
> - 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.
Workaround for raw_input raising EOFError and KeyboardInterrupt on Ctrl-C.
except EOFError as exc:
# If KeyboardInterrupt is not raised in 50ms, it's a real EOF event.
raw_input1 = raw_input
__builtin__.raw_input = raw_input2
except BaseException as exc:
More information about the Python-list