如何设置 sys.excepthook 在 python 中全局调用 pdb?

发布于 2024-07-30 11:18:21 字数 550 浏览 3 评论 0原文

来自 Python 文档:

sys.excepthook(类型、值、回溯)

此函数将给定的回溯和异常打印到 sys.stderr

当引发异常但未捕获时,解释器会使用三个参数调用 sys.excepthook:异常类、异常实例和回溯对象。 在交互式会话中,这发生在控制权返回提示之前; 在 Python 程序中,这发生在程序退出之前。 可以通过将另一个三参数函数分配给 sys.excepthook 来自定义此类顶级异常的处理。

http://docs.python.org/library/sys.html

我该如何全局修改此设置,以便默认操作始终调用 pdb? 有我可以更改的配置文件吗? 我不想包装我的代码来执行此操作。

From Python docs:

sys.excepthook(type, value, traceback)

This function prints out a given traceback and exception to sys.stderr.

When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.

http://docs.python.org/library/sys.html

How do I modify this globally so the default action is to always invoke pdb? Is there a configuration file I can change? I don't want to wrap my code to do this.

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

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

发布评论

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

评论(3

生来就爱笑 2024-08-06 11:18:21

这就是您需要的

http://ynniv.com/blog/2007/11/debugging -python.html

三种方式,第一种简单粗暴(Thomas Heller) - 将以下内容添加到 site-packages/sitecustomize.py:

import pdb, sys, traceback
def info(type, value, tb):
    traceback.print_exception(type, value, tb)
    pdb.pm()
sys.excepthook = info

第二个更复杂,并检查交互模式(奇怪的是跳过交互模式下的调试),来自 cookbook

# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty():
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type, value, tb)
   else:
      import traceback, pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

sys.excepthook = info

第三个​​(除非 stdin 或 stderr 被重定向,否则总是启动调试器) ynniv

# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   if (#hasattr(sys, "ps1") or
       not sys.stderr.isatty() or 
       not sys.stdin.isatty()):
       # stdin or stderr is redirected, just do the normal thing
       original_hook(type, value, tb)
   else:
       # a terminal is attached and stderr is not redirected, debug 
       import traceback, pdb
       traceback.print_exception(type, value, tb)
       print
       pdb.pm()
       #traceback.print_stack()

original_hook = sys.excepthook
if sys.excepthook == sys.__excepthook__:
    # if someone already patched excepthook, let them win
    sys.excepthook = info

Here's what you need

http://ynniv.com/blog/2007/11/debugging-python.html

Three ways, the first is simple but crude (Thomas Heller) - add the following to site-packages/sitecustomize.py:

import pdb, sys, traceback
def info(type, value, tb):
    traceback.print_exception(type, value, tb)
    pdb.pm()
sys.excepthook = info

The second is more sophisticated, and checks for interactive mode (weirdly skipping the debugging in interactive mode), from the cookbook:

# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty():
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type, value, tb)
   else:
      import traceback, pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

sys.excepthook = info

And the third (which always start the debugger unless stdin or stderr are redirected) by ynniv

# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   if (#hasattr(sys, "ps1") or
       not sys.stderr.isatty() or 
       not sys.stdin.isatty()):
       # stdin or stderr is redirected, just do the normal thing
       original_hook(type, value, tb)
   else:
       # a terminal is attached and stderr is not redirected, debug 
       import traceback, pdb
       traceback.print_exception(type, value, tb)
       print
       pdb.pm()
       #traceback.print_stack()

original_hook = sys.excepthook
if sys.excepthook == sys.__excepthook__:
    # if someone already patched excepthook, let them win
    sys.excepthook = info
冷︶言冷语的世界 2024-08-06 11:18:21

另一种选择是使用 ipython,我认为它是任何 python 开发人员的必备工具。 不要从 shell 运行脚本,而是使用 %run 从 ipython 运行脚本。 当出现异常时,可以输入%debug进行调试。 (还有一个选项可以自动调试发生的任何异常,但我忘记了它是什么。)

Another option is to use ipython, which I consider a must-have tool for any python developer anyway. Instead of running your script from the shell, run it from ipython with %run. When an exception occurs, you can type %debug to debug it. (There's also an option to automatically debug any exception that occurs, but I forget what it is.)

剩余の解释 2024-08-06 11:18:21

尝试:

import pdb
import sys

def excepthook(type, value, traceback):
    pdb.post_mortem(traceback)

excepthook.old = sys.excepthook
sys.excepthook = excepthook

def raise_exception():
    raise_exception()

raise_exception()

Try:

import pdb
import sys

def excepthook(type, value, traceback):
    pdb.post_mortem(traceback)

excepthook.old = sys.excepthook
sys.excepthook = excepthook

def raise_exception():
    raise_exception()

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