Python:Windows 中的双键盘中断?

发布于 2024-10-08 13:30:07 字数 813 浏览 5 评论 0原文

我正在 Python 3.1.2 中运行基于控制台的应用程序。我希望应用程序在提示符下捕获 Ctrl-C 并根据上下文进行处理。我按预期收到了 KeyboardInterrupt,但出乎意料的是,当我去写警告消息时,有时我会再次看到它。追溯如下。各位聪明人有什么想法吗?


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 handling 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'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 handling 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

☆獨立☆ 2024-10-15 13:30:07

我无法重现这个。

def foo():
    try:
        x = 0
        while True:
            x += 1        
    except KeyboardInterrupt:
        print(x)
while True:
    foo()

工作正常并按预期捕获 CTRL-C。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文