本文介绍了不能覆盖 sys.excepthook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试自定义 sys.excepthook 的行为,如 食谱.

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

在 ipython 中:

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() 没有被调用.似乎 sys.excepthook = info 在我的 python 2.5 安装中不起作用.

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

推荐答案

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

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.

这篇关于不能覆盖 sys.excepthook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 03:52