KeyboardInterrupt not caught

R

ruka_at_

Hi,
why is KeyboardInterrupt not caught (xp)?
import sys
try:
inp = sys.stdin.read()
except (KeyboardInterrupt, SystemExit):
print "kbd-interr,SystemExit"
except EOFError:
print "eof encountered"
except:
print "caught all"
self.showtraceback()
print "normal end"

result after script startet and ^C hit:
ctrl_test.py
normal end
Traceback (most recent call last):
File "C:\work\py_src\ctrl_test.py", line 11, in ?
print "normal end"
KeyboardInterrupt

br Rudi
 
D

Daniel Nogradi

why is KeyboardInterrupt not caught (xp)?
import sys
try:
inp = sys.stdin.read()
except (KeyboardInterrupt, SystemExit):
print "kbd-interr,SystemExit"
except EOFError:
print "eof encountered"
except:
print "caught all"
self.showtraceback()
print "normal end"

result after script startet and ^C hit:
normal end
Traceback (most recent call last):
File "C:\work\py_src\ctrl_test.py", line 11, in ?
print "normal end"
KeyboardInterrupt

Hi, are you sure this is exactly what you run?
The code above works perfectly for me and prints

kbd-interr,SystemExit
normal end

as it should upon pressing Ctrl-C (although I'm on linux not xp but
that shouldn't matter at all).
 
G

Gabriel Genellina

Hi, are you sure this is exactly what you run?
The code above works perfectly for me and prints

kbd-interr,SystemExit
normal end

as it should upon pressing Ctrl-C (although I'm on linux not xp but
that shouldn't matter at all).

I'm using XP and it works as expected too.
 
S

Steven D'Aprano

Hi,
why is KeyboardInterrupt not caught (xp)?
import sys
try:
inp = sys.stdin.read()
except (KeyboardInterrupt, SystemExit):
print "kbd-interr,SystemExit"
except EOFError:
print "eof encountered"

I don't think you ever get an EOFError from stdin. If you type ^D
immediately, stdin.read() returns an empty string.

except:
print "caught all"
self.showtraceback()

I don't imagine you'll get any other exceptions either.

Not that it matters, but what's self?


print "normal end"

result after script startet and ^C hit:
normal end
Traceback (most recent call last):
File "C:\work\py_src\ctrl_test.py", line 11, in ?
print "normal end"
KeyboardInterrupt

It works as expected for me.

I seem to have a vague recollection that the keyboard interrupt under
Windows isn't ^C but something else... ^Z maybe?
 
G

Gabriel Genellina

En Fri, 16 Feb 2007 07:26:09 -0300, Steven D'Aprano
I seem to have a vague recollection that the keyboard interrupt under
Windows isn't ^C but something else... ^Z maybe?

Ctrl-C is the keyboard interrupt, Ctrl-Z means EOF.
 
R

ruka_at_

Thanks to all of you, for the fast answers.
The code I showed you is actually the code running. I tried to catch
eof, cause I read ^C could produce EOF (the self.showtraceback() was
just a stupid cut 'n paste). But not even the except that should catch
all exceptions is triggered.
Thanks again,
br Rudi
 
D

Dennis Lee Bieber

Hi,
why is KeyboardInterrupt not caught (xp)?

Using a variation of your sample

-=-=-=-=-
import time
import sys
inp = None
try:
time.sleep(10) #########
inp = sys.stdin.read()
except (KeyboardInterrupt, SystemExit):
print "kbd-interr,SystemExit"
except EOFError:
print "eof encountered"
except:
print "caught all"
print repr(inp)
print "normal end"
-=-=-=-=-=-=-

results in the following -- the first one is when I hit the <ctrl-c>
immediately after entering the command line; the other is when I waited
the 10+ seconds so the read became active.

E:\UserData\DENNIS~1\MYDOCU~1>python Script1.py
kbd-interr,SystemExit
None
normal end

E:\UserData\DENNIS~1\MYDOCU~1>python Script1.py
''
normal end
^C
E:\UserData\DENNIS~1\MYDOCU~1>python Script1.py
^Z
''
normal end

E:\UserData\DENNIS~1\MYDOCU~1>

Off hand, it appears that a pending read of stdin will absorb all
characters; keyboard interrupt only occurs when the OS itself gets the
<ctrl-c> during some blocking call OTHER THAN a read of stdin -- ie, the
time.sleep()

import sys
try:
inp = sys.stdin.read()
except (KeyboardInterrupt, SystemExit):
print "kbd-interr,SystemExit"
except EOFError:
print "eof encountered"
except:
print "caught all"
self.showtraceback()
print "normal end"

result after script startet and ^C hit:
normal end
Traceback (most recent call last):
File "C:\work\py_src\ctrl_test.py", line 11, in ?
print "normal end"
KeyboardInterrupt

br Rudi
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top