使用 Ctrl-C 删除 Python 中的回溯

发布于 2024-11-29 14:55:06 字数 99 浏览 0 评论 0原文

有没有办法在您按 Ctrl+c 时阻止回溯出现, 即在Python脚本中引发KeyboardInterrupt

Is there a way to keep tracebacks from coming up when you hit Ctrl+c,
i.e. raise KeyboardInterrupt in a Python script?

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

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

发布评论

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

评论(9

一杆小烟枪 2024-12-06 14:55:06

试试这个:

import signal
import sys
signal.signal(signal.SIGINT, lambda x, y: sys.exit(0))

这样您就不需要将所有内容都包装在异常处理程序中。

Try this:

import signal
import sys
signal.signal(signal.SIGINT, lambda x, y: sys.exit(0))

This way you don't need to wrap everything in an exception handler.

横笛休吹塞上声 2024-12-06 14:55:06
import sys
try:
    # your code
except KeyboardInterrupt:
    sys.exit(0) # or 1, or whatever

这是最简单的方法,假设您在按下 Ctrl+c 时仍想退出。

如果你想在不尝试/例外的情况下捕获它,你可以使用 像这样的配方使用signal模块,除了它似乎在 Windows 上对我不起作用..

import sys
try:
    # your code
except KeyboardInterrupt:
    sys.exit(0) # or 1, or whatever

Is the simplest way, assuming you still want to exit when you get a Ctrl+c.

If you want to trap it without a try/except, you can use a recipe like this using the signal module, except it doesn't seem to work for me on Windows..

屌丝范 2024-12-06 14:55:06

根据 正确处理 SIGINT/SIGQUIT,唯一的方法如果您捕获到 SIGINT,正确退出就是随后用 SIGINT 信号杀死自己(请参阅标题为“如何成为一个正确的程序”的部分)。尝试伪造正确的退出代码是不正确的。有关详细信息,另请检查 为什么“执行 exit 130 与死于 SIGINT 不同”? 在 Unix Stack Exchange 上。

似乎这里所有退出零的答案实际上都表明程序行为不当。最好不要隐藏您被中断的情况,以便调用者(通常是 shell)知道该怎么做。

在 Python 中,stderr 上的回溯打印输出来自默认的 sys。 excepthook 行为。因此,为了抑制回溯垃圾邮件,我选择通过替换键盘中断上的 except 钩子来直接解决问题,然后通过重新引发原始异常来保留正确的退出代码:

import sys, time

def _no_traceback_excepthook(exc_type, exc_val, traceback):
    pass

def main():
    try:
        # whatever your program does here...
        print("hello world..")
        time.sleep(42)
    except KeyboardInterrupt:
        # whatever cleanup code you need here...
        print("bye")
        if sys.excepthook is sys.__excepthook__:
            sys.excepthook = _no_traceback_excepthook
        raise

if __name__ == "__main__":
    main()

结果如下:

$ python3 /tmp/example.py
hello world..
^Cbye

$ echo $?
130

如果您没有任何要执行的清理操作,并且您想要做的就是禁止在中断时打印回溯,那么默认安装它可能会更简单:

import sys

def _no_traceback_excepthook(exc_type, exc_val, traceback):
    if isinstance(exc_val, KeyboardInterrupt):
        return
    sys.__excepthook__(exc_type, exc_val, traceback)

sys.excepthook = _no_traceback_excepthook

According to Proper handling of SIGINT/SIGQUIT, the only way to exit correctly if you're catching a SIGINT is to subsequently kill yourself with a SIGINT signal (see the section titled "How to be a proper program"). It is incorrect to attempt to fake the proper exit code. For more info about that, check also Why is "Doing an exit 130 is not the same as dying of SIGINT"? over on Unix Stack Exchange.

It seems all the answers here which are exiting zero actually demonstrate programs that misbehave. It's preferable not to hide that you were interrupted, so that the caller (usually a shell) knows what to do.

In Python, the traceback printout on stderr comes from the default sys.excepthook behaviour. So, to suppress the traceback spam, I've opted to tackle the problem directly at the cause by replacing the except hook on keyboard interrupt, and then preserve the correct exit code by re-raising the original exception:

import sys, time

def _no_traceback_excepthook(exc_type, exc_val, traceback):
    pass

def main():
    try:
        # whatever your program does here...
        print("hello world..")
        time.sleep(42)
    except KeyboardInterrupt:
        # whatever cleanup code you need here...
        print("bye")
        if sys.excepthook is sys.__excepthook__:
            sys.excepthook = _no_traceback_excepthook
        raise

if __name__ == "__main__":
    main()

The result is like this:

$ python3 /tmp/example.py
hello world..
^Cbye

$ echo $?
130

If you don't have any cleanup actions to execute, and all you wanted to do is suppress printing the traceback on interrupt, it may be simpler just to install that by default:

import sys

def _no_traceback_excepthook(exc_type, exc_val, traceback):
    if isinstance(exc_val, KeyboardInterrupt):
        return
    sys.__excepthook__(exc_type, exc_val, traceback)

sys.excepthook = _no_traceback_excepthook
十六岁半 2024-12-06 14:55:06

捕获 KeyboardInterrupt

try:
    # do something
except KeyboardInterrupt:
    pass

Catch the KeyboardInterrupt:

try:
    # do something
except KeyboardInterrupt:
    pass
唯憾梦倾城 2024-12-06 14:55:06

用 try/ except 块捕获它:

while True:
   try:
      print "This will go on forever"
   except KeyboardInterrupt:
      pass

Catch it with a try/except block:

while True:
   try:
      print "This will go on forever"
   except KeyboardInterrupt:
      pass
莫言歌 2024-12-06 14:55:06
try:
    your_stuff()
except KeyboardInterrupt:
    print("no traceback")
try:
    your_stuff()
except KeyboardInterrupt:
    print("no traceback")
森林迷了鹿 2024-12-06 14:55:06

另请注意,默认情况下,解释器退出时状态码为 128 + 您平台上的 SIGINT 值(在大多数系统上为 2)。

    import sys, signal

    try:
        # code...
    except KeyboardInterrupt: # Suppress tracebacks on SIGINT
        sys.exit(128 + signal.SIGINT) # http://tldp.org/LDP/abs/html/exitcodes.html

Also note that by default the interpreter exits with the status code 128 + the value of SIGINT on your platform (which is 2 on most systems).

    import sys, signal

    try:
        # code...
    except KeyboardInterrupt: # Suppress tracebacks on SIGINT
        sys.exit(128 + signal.SIGINT) # http://tldp.org/LDP/abs/html/exitcodes.html
悟红尘 2024-12-06 14:55:06

使用上下文管理器抑制异常:

from contextlib import suppress

def output_forever():
    while True:
        print('endless script output. Press ctrl + C to exit')


if __name__ == '__main__':
    with suppress(KeyboardInterrupt):
        output_forever()

suppress exception using context manager:

from contextlib import suppress

def output_forever():
    while True:
        print('endless script output. Press ctrl + C to exit')


if __name__ == '__main__':
    with suppress(KeyboardInterrupt):
        output_forever()
深爱成瘾 2024-12-06 14:55:06
import sys
try:
    print("HELLO")
    english = input("Enter your main launguage: ")
    print("GOODBYE")
except KeyboardInterrupt:
    print("GET LOST")
import sys
try:
    print("HELLO")
    english = input("Enter your main launguage: ")
    print("GOODBYE")
except KeyboardInterrupt:
    print("GET LOST")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文