Python: Double KeyboardInterrupt in Windows?
I'm running a console-based app in Python 3.1.2. I want the app to trap a Ctrl-C at the prompt and handle it according to context. I'm getting the KeyboardInterrupt as expected, but unexpectedly, I'm sometimes seeing it again when I go to write a warning message. The tracebacks are below. Any thoughts from you smart people?
Traceback (most recent call last): File "E:\Dropbox\git\vocabulary\v.py", line 58, in main command, args = c.getcommand() File "E:\Dropbox\git\vocabulary\console.py", line 81, in getcommand command, *args = input(prompt).split() KeyboardInterrupt During 开发者_如何学Gohandling of the above exception, another exception occurred: Traceback (most recent call last): File "E:\Dropbox\git\vocabulary\v.py", line 125, in main() File "E:\Dropbox\git\vocabulary\v.py", line 71, in main print("\nUse 'quit' to exit the application.") File "E:\Dropbox\git\vocabulary\utilities.py", line 191, in write self.stream.write(data) KeyboardInterrupt
I can't reproduce this.
def foo():
try:
x = 0
while True:
x += 1
except KeyboardInterrupt:
print(x)
while True:
foo()
Works just fine and traps CTRL-C as expected.
精彩评论