无法覆盖 sys.excepthook
我尝试按照 食谱。
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅这个问题并确保您的
sitecustomize.py
中没有任何内容阻止交互模式下的调试。See this SO question and make sure there isn't something in your
sitecustomize.py
that prevents debugging in interactive mode.扩展 Chris 的答案,您可以使用另一个函数(如装饰器)将您自己的功能添加到 jupyters showbacktrace 中:
expanding on Chris answer, you can use another function like a decorator to add your own functionality to jupyters showbacktrace:
sys.excepthook
在 ipython 中不起作用。 我认为挂钩异常的推荐方法是使用 set_custom_exc 方法,如下所示:请参阅 文档 了解更多详细信息。
sys.excepthook
won't work in ipython. I think the recommended way of hooking to exceptions is to use theset_custom_exc
method, like this:See docs for more details.
您使用的 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 justipython
, and it will automatically invoke pdb upon uncaught exceptions, just as you are trying to do with your excepthook.在你写这篇文章五年后,IPython 仍然以这种方式工作,所以我想一个解决方案可能对人们在谷歌上搜索这个问题有用。
每次执行一行代码时,IPython 都会替换 sys. excepthook ,因此您对 sys. excepthook 的覆盖不起作用。 此外,IPython 甚至不调用 sys. excepthook ,它会捕获所有异常并在事情发展到这一步之前自行处理它们。
要在 IPython 运行时覆盖异常处理程序,您可以对其 shell 的
showtraceback
方法进行 Monkeypatch。 例如,以下是我如何重写以提供看起来像普通 Python 回溯的内容(因为我不喜欢 IPython 的冗长):这在普通终端控制台和 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 callsys.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):This works in both the normal terminal console and the Qt console.