Trouble with KeyboardInterrupt exception

John Griffin-Wiesner johngw at attbi.com
Wed Jan 29 17:51:18 CET 2003


I'm seeing strange behavior when catching EOFError and
KeyboardInterrupt in the same try sequence.  A really
short program that shows my problem is below.

When using it, one time press "ctrl-C" first, and notice
that it works as expected.

Then another time, press "ctrl-D", which works fine, but
after that, a "ctrl-C" does not work right.

I've tested on python 2.2.1, and 2.3.? and it acts the same
on both.  It does not have this problem on python 1.5.2.
(All tests done on linux.)  Swapping the order of the
exceptions has no effect.

I'm pretty new to python so I'm probably doing something
really dumb.  Any help is appreciated.


Here's what I see when running it.  The first time giving
it ctrl-C first, the second time I gave it two ctrl-D's,
then a ctrl-C.

$ test.py
Type here: ctrl-C caught

$ test.py
Type here: EOF character caught
Type here: EOF character caught
Type here: Traceback (most recent call last):
  File "/tmp/test.py", line 8, in ?
    input = raw_input("Type here: ")
KeyboardInterrupt
$ 


--test.py-------------------------------
#!/usr/bin/python2

import sys

input = ""
while not input:
      try:
              input = raw_input("Type here: ")
      except EOFError:
              print "EOF character caught"
      except KeyboardInterrupt:
              sys.exit("ctrl-C caught")
print input

----------------------------------------


-- 
John Griffin-Wiesner
johngw @ attbi.com

   It said, "Windows 95 or better," so I installed Linux.




More information about the Python-list mailing list