无法覆盖 sys.excepthook

发布于 2024-07-30 13:39:56 字数 664 浏览 3 评论 0原文

我尝试按照 食谱

在 ipython 中:

:import pdb, sys, traceback
:def info(type, value, tb):
:    traceback.print_exception(type, value, tb)
:    pdb.pm()
:sys.excepthook = info
:--
>>> x[10] = 5
-------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
NameError: name 'x' is not defined
>>>

pdb.pm() 没有被调用。 看来 sys.excepthook = info 在我的 python 2.5 安装中不起作用。

I try to customize behavior of sys.excepthook as described by the recipe.

in ipython:

:import pdb, sys, traceback
:def info(type, value, tb):
:    traceback.print_exception(type, value, tb)
:    pdb.pm()
:sys.excepthook = info
:--
>>> x[10] = 5
-------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
NameError: name 'x' is not defined
>>>

pdb.pm() is not being called. It seems that sys.excepthook = info doesn't work in my python 2.5 installation.

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

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

发布评论

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

评论(5

海未深 2024-08-06 13:40:45

请参阅这个问题并确保您的 sitecustomize.py 中没有任何内容阻止交互模式下的调试。

See this SO question and make sure there isn't something in your sitecustomize.py that prevents debugging in interactive mode.

守不住的情 2024-08-06 13:40:40

扩展 Chris 的答案,您可以使用另一个函数(如装饰器)将您自己的功能添加到 jupyters showbacktrace 中:

from IPython.core.interactiveshell import InteractiveShell
from functools import wraps
import traceback
import sys

def change_function(func):
    @wraps(func)
    def showtraceback(*args, **kwargs):
        # extract exception type, value and traceback
        etype, evalue, tb = sys.exc_info()
        if issubclass(etype, Exception):
            print('caught an exception')
        else:
            # otherwise run the original hook
            value = func(*args, **kwargs)
            return value
    return showtraceback

InteractiveShell.showtraceback = change_function(InteractiveShell.showtraceback)

raise IOError

expanding on Chris answer, you can use another function like a decorator to add your own functionality to jupyters showbacktrace:

from IPython.core.interactiveshell import InteractiveShell
from functools import wraps
import traceback
import sys

def change_function(func):
    @wraps(func)
    def showtraceback(*args, **kwargs):
        # extract exception type, value and traceback
        etype, evalue, tb = sys.exc_info()
        if issubclass(etype, Exception):
            print('caught an exception')
        else:
            # otherwise run the original hook
            value = func(*args, **kwargs)
            return value
    return showtraceback

InteractiveShell.showtraceback = change_function(InteractiveShell.showtraceback)

raise IOError
一杯敬自由 2024-08-06 13:40:33

sys.excepthook 在 ipython 中不起作用。 我认为挂钩异常的推荐方法是使用 set_custom_exc 方法,如下所示:

from IPython import get_ipython
ipython = get_ipython()


def exception_handler(self, etype, evalue, tb, tb_offset=None):
    print("##### Oh no!!! #####")  # your handling of exception here
    self.showtraceback((etype, evalue, tb), tb_offset=tb_offset)  # standard IPython's printout

    
ipython.set_custom_exc((Exception,), exception_handler)  # register your handler
raise Exception("##### Bad things happened! #####")  # see it in action

请参阅 文档 了解更多详细信息。

sys.excepthook won't work in ipython. I think the recommended way of hooking to exceptions is to use the set_custom_exc method, like this:

from IPython import get_ipython
ipython = get_ipython()


def exception_handler(self, etype, evalue, tb, tb_offset=None):
    print("##### Oh no!!! #####")  # your handling of exception here
    self.showtraceback((etype, evalue, tb), tb_offset=tb_offset)  # standard IPython's printout

    
ipython.set_custom_exc((Exception,), exception_handler)  # register your handler
raise Exception("##### Bad things happened! #####")  # see it in action

See docs for more details.

久而酒知 2024-08-06 13:40:27

您使用的 ipython 代替了普通的 Python 交互式 shell,它本身捕获所有异常并且不使用 sys.excepthook。 将其作为 ipython -pdb 运行,而不仅仅是 ipython,它会在未捕获的异常时自动调用 pdb,就像您尝试使用 excepthook 一样。

ipython, which you're using instead of the normal Python interactive shell, traps all exceptions itself and does NOT use sys.excepthook. Run it as ipython -pdb instead of just ipython, and it will automatically invoke pdb upon uncaught exceptions, just as you are trying to do with your excepthook.

第几種人 2024-08-06 13:40:20

在你写这篇文章五年后,IPython 仍然以这种方式工作,所以我想一个解决方案可能对人们在谷歌上搜索这个问题有用。

每次执行一行代码时,IPython 都会替换 sys. excepthook ,因此您对 sys. excepthook 的覆盖不起作用。 此外,IPython 甚至不调用 sys. excepthook ,它会捕获所有异常并在事情发展到这一步之前自行处理它们。

要在 IPython 运行时覆盖异常处理程序,您可以对其 shell 的 showtraceback 方法进行 Monkeypatch。 例如,以下是我如何重写以提供看起来像普通 Python 回溯的内容(因为我不喜欢 IPython 的冗长):

def showtraceback(self, *args, **kwargs):
    traceback_lines = traceback.format_exception(*sys.exc_info())
    del traceback_lines[1]
    message = ''.join(traceback_lines)
    sys.stderr.write(message)

import sys
import traceback
import IPython
IPython.core.interactiveshell.InteractiveShell.showtraceback = showtraceback

这在普通终端控制台和 Qt 控制台中都有效。

Five years after you wrote this, IPython still works this way, so I guess a solution might be useful to people googling this.

IPython replaces sys.excepthook every time you execute a line of code, so your overriding of sys.excepthook has no effect. Furthermore, IPython doesn't even call sys.excepthook, it catches all exceptions and handles them itself before things get that far.

To override the exception handler whilst IPython is running, you can monkeypatch over their shell's showtraceback method. For example, here's how I override to give what looks like an ordinary Python traceback (because I don't like how verbose IPython's are):

def showtraceback(self, *args, **kwargs):
    traceback_lines = traceback.format_exception(*sys.exc_info())
    del traceback_lines[1]
    message = ''.join(traceback_lines)
    sys.stderr.write(message)

import sys
import traceback
import IPython
IPython.core.interactiveshell.InteractiveShell.showtraceback = showtraceback

This works in both the normal terminal console and the Qt console.

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