469,271 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

KeyboardInterrupt not caught

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

Feb 16 '07 #1
6 4139
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
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).
Feb 16 '07 #2
En Fri, 16 Feb 2007 06:58:54 -0300, Daniel Nogradi <no*****@gmail.com>
escribió:
>why is KeyboardInterrupt not caught (xp)?

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.

--
Gabriel Genellina

Feb 16 '07 #3
On Fri, 16 Feb 2007 01:47:43 -0800, ruka_at_ wrote:
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:
>>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
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?

--
Steven.

Feb 16 '07 #4
En Fri, 16 Feb 2007 07:26:09 -0300, Steven D'Aprano
<st***@REMOVE.THIS.cybersource.com.auescribió:
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.

--
Gabriel Genellina

Feb 16 '07 #5
On 16 Feb., 11:44, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:

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

Feb 16 '07 #6
On 16 Feb., 12:16, ruka_...@fastmail.fm wrote:
On 16 Feb., 11:44, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:

I've tried it in cygwin, result:
$ python.exe c:/work/py_src/ctrl_test.py
kbd-interr,SystemExit
normal end
br Rudi

Feb 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve Holden | last post: by
8 posts views Thread by Ivan Nestlerode | last post: by
reply views Thread by PantherSE | last post: by
4 posts views Thread by Tamas Nepusz | last post: by
1 post views Thread by darren kirby | last post: by
4 posts views Thread by Tony Nelson | last post: by
2 posts views Thread by Michael Goerz | last post: by
1 post views Thread by radim.marek | last post: by
7 posts views Thread by Brendon Costa | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.