不错的python异常钩子替换
better_exchook的Python项目详细描述
一个更好的python sys.excepthook替代品, 也就是说,它用扩展信息打印堆栈跟踪。 它将为每个帧添加一些有用的信息, 类似于打印相关变量(relevant=在代码行中引用)。 有关详细信息,请参见Python source and comments。
功能
- 堆栈跟踪输出中的多行python语句。
- 每帧的彩色/格式化输出。
- python源代码的语法突出显示。
- 支持DomTerm, 默认情况下,它会折叠每个堆栈帧的所有细节, 从而提供了更全面的概述, 同时在需要的时候提供所有的细节。
用法
importbetter_exchookbetter_exchook.install()# will just do: sys.excepthook = better_exchook
示例
python示例代码:
try:x={1:2,"a":"b"}deff():y="foo"x,42,sys.stdin.__class__,sys.exc_info,y,zf()exceptException:better_exchook.better_exchook(*sys.exc_info())
输出:
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 478, in <module> line: f() locals: f = <local> <function f at 0x107f1de60> File "better_exchook.py", line 477, in f line: x, 42, sys.stdin.__class__, sys.exc_info, y, z locals: x = <global> {'a': 'b', 1: 2} sys = <global> <module 'sys' (built-in)> sys.stdin = <global> <open file '<stdin>', mode 'r' at 0x107d9f0c0> sys.stdin.__class__ = <global> <type 'file'> sys.exc_info = <global> <built-in function exc_info> y = <local> 'foo' z = <not found> NameError: global name 'z' is not defined
python示例代码:
try:f=lambdax:Nonef(x,y)exceptException:better_exchook.better_exchook(*sys.exc_info())
输出:
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 484, in <module> line: f(x, y) locals: f = <local> <function <lambda> at 0x107f1df50> x = <local> {'a': 'b', 1: 2} y = <not found> NameError: name 'y' is not defined
python示例代码:
try:(lambdax:None)(__name__,42)# multilineexceptException:better_exchook.better_exchook(*sys.exc_info())
输出:
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 490, in <module> line: (lambda x: None)(__name__, 42) # multiline locals: x = <local> {'a': 'b', 1: 2} __name__ = <local> '__main__', len = 8 TypeError: <lambda>() takes exactly 1 argument (2 given)
python示例代码:
# use this to overwrite the global exception handlersys.excepthook=better_exchook.better_exchook# and failfinalfail(sys)
输出:
EXCEPTION Traceback (most recent call last): File "better_exchook.py", line 497, in <module> line: finalfail(sys) locals: finalfail = <not found> sys = <local> <module 'sys' (built-in)> NameError: name 'finalfail' is not defined
屏幕截图:
带有DomTerm:
类似项目
- https://github.com/patrys/great-justice
- Nose does something similar for assertion failures。
- ipython有类似的东西(this)。 这样做:from IPython.core import ultratb; sys.excepthook = ultratb.VerboseTB()。
- Ka Ping Yee的“cgitb.py”是Python的一部分, see here, code here。
- 见this 相关的stackoverflow问题。
–阿尔伯特·泽耶,<;http://www.az2000.de>;