当捕获信号时,我该如何迫使PDB退出?

发布于 2025-01-21 20:36:26 字数 476 浏览 4 评论 0原文

PDB的退出命令通过提高异常(bdb.bdbquit)来起作用。如果被抓住了这个例外,我将无法找到一种杀死该程序的方法,而没有杀死整个外壳。 CTRL+C通过提高键盘Interrupt异常来起作用,这也可以抓住。

您可以使用此简单脚本来重新创建此问题。

foo = 0
while True:
    try:
        import pdb; pdb.set_trace()
        foo += 1
    except:
        pass

使用退出命令或CTRL+c,该脚本无法从PDB内部停止

我知道这是不良的编程,您绝对不应该使用,而没有异常类型。我之所以问,是因为我在调试时遇到了这个问题,第三方图书馆将我困在循环中。

PDB's quit command works by raising an exception (Bdb.BdbQuit). If that exception gets caught, I cannot figure out a way to kill the program short of killing the entire shell. CTRL+C works by raising a KeyboardInterrupt exception, which can also be caught.

You can recreate this problem with this simple script.

foo = 0
while True:
    try:
        import pdb; pdb.set_trace()
        foo += 1
    except:
        pass

This script cannot be stopped from within PDB with the quit command or CTRL+C.

I'm aware this is bad programming and you should never use an except without an exception type. I ask because I ran into this issue while debugging and a third-party library trapped me in the loop.

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

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

发布评论

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

评论(2

孤芳又自赏 2025-01-28 20:36:26

您可以尝试使用 os.__exit

import os
try:
    print("Exiting")
    os._exit(1)
except:
    print("Caught!")

输出:

Exiting

You can try killing the python process with os._exit.

import os
try:
    print("Exiting")
    os._exit(1)
except:
    print("Caught!")

Output:

Exiting
后知后觉 2025-01-28 20:36:26

为什么不重新调整例外:

import bdb

try: 
   something_i_might_want_to_debug_with_pdb()
except bdb.BdbQuit as exc:
   raise exc
except:
    print("Caught!")

Why not re-raise the exception:

import bdb

try: 
   something_i_might_want_to_debug_with_pdb()
except bdb.BdbQuit as exc:
   raise exc
except:
    print("Caught!")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文