17.7. cgitb — 更加详细的 traceback 报告 | 开发者工具 |《python 3 标准库实例教程》| python 技术论坛-江南app体育官方入口
本节目标:了解
cgitb
,cgitb
比traceback
给出的信息更加详细。
cgitb
是标准库中非常有价值的调试工具。它一开始被设计为在 web 应用程序中显示错误和调试信息,后来也更新到可以到普通文本中,不过最终也没重命名它。这也导致了这个包基本没怎么被用过(在需要它的时候)。
标准追踪转储
python 的默认异常处理器会把错误发生位置的保准错误输出流打印出来。基本上也包含了足够的信息让我们理解为什么会引起错误以及随后修复它。
cgitb_basic_traceback.py
def func2(a, divisor):
return a / divisor
def func1(a, b):
c = b - 5
return func2(a, c)
func1(1, 5)
运行之后会有一个比较不容易引发的错误出现(func2()
中)。
$ python3 cgitb_basic_traceback.py
traceback (most recent call last):
file "cgitb_basic_traceback.py", line 18, in
func1(1, 5)
file "cgitb_basic_traceback.py", line 16, in func1
return func2(a, c)
file "cgitb_basic_traceback.py", line 11, in func2
return a / divisor
zerodivisionerror: division by zero
开启更详细的追踪信息显示
基本的追踪信息已经包含了足够的错误信息,cgitb
则显示的更多。cgitb
替换了 sys.excepthook
为一个扩展了追踪的函数。
cgitb_local_vars.py
import cgitb
cgitb.enable(format='text')
运行后的错误报告会非常多。栈的每一帧都被列了出来:
- 原文件的全部路径。
- 栈中每个函数的参数值。
- 错误路径中出错行的多行上下文。
- 引起此错误的变量的值。
能访问到涉及此错误栈的变量可以帮助我们定位并不是因为此行发生错误的情况。
$ python3 cgitb_local_vars.py
zerodivisionerror
python 3.6.4: .../bin/python3
sun mar 18 16:20:19 2018
a problem occurred in a python script. here is the sequence of
function calls leading up to the error, in the order they
occurred.
.../cgitb_local_vars.py in ()
18 def func1(a, b):
19 c = b - 5
20 return func2(a, c)
21
22 func1(1, 5)
func1 =
.../cgitb_local_vars.py in func1(a=1, b=5)
18 def func1(a, b):
19 c = b - 5
20 return func2(a, c)
21
22 func1(1, 5)
global func2 =
a = 1
c = 0
.../cgitb_local_vars.py in func2(a=1, divisor=0)
13
14 def func2(a, divisor):
15 return a / divisor
16
17
a = 1
divisor = 0
zerodivisionerror: division by zero
__cause__ = none
__class__ =
__context__ = none
__delattr__ =
__dict__ = {}
__dir__ =
__doc__ = 'second argument to a division or modulo operation
was zero.'
__eq__ =
__format__ =
__ge__ =
__getattribute__ =
__gt__ =
__hash__ =
__init__ =
__init_subclass__ =
__le__ =
__lt__ =
__ne__ =
__new__ =
__reduce__ =
__reduce_ex__ =
__repr__ =
__setattr__ =
__setstate__ =
__sizeof__ =
__str__ =
__subclasshook__ =
__suppress_context__ = false
__traceback__ =
args = ('division by zero',)
with_traceback =
the above is a description of an error in a python program.
here is
the original traceback:
traceback (most recent call last):
file "cgitb_local_vars.py", line 22, in
func1(1, 5)
file "cgitb_local_vars.py", line 20, in func1
return func2(a, c)
file "cgitb_local_vars.py", line 15, in func2
return a / divisor
zerodivisionerror: division by zero
本例中的错误是 zerodivisionerror
,它给出的分析是此错误是因为func1()
中计算 c
时发生了错误,而不是因为 func2()
使用的值的有问题。
最终的输出还包含完整的异常对象的信息(在某些情况下我们知道它的属性而不是消息
更有利于我们调试)和标准追踪转储格式。
traceback 中的本地变量
处在 cgitb
中的代码,如有错误发生它会检测导致此错误发生的栈中的变量,给我们提供准确的涉及其中的变量信息。
cgitb_with_classes.py
import cgitb
cgitb.enable(format='text', context=12)
class brokenclass:
"""this class has an error.
"""
def __init__(self, a, b):
"""be careful passing arguments in here.
"""
self.a = a
self.b = b
self.c = self.a * self.b
# really
# long
# comment
# goes
# here.
self.d = self.a / self.b
return
o = brokenclass(1, 0)
不过如果某函数或方法包含大量的注释,空格或其他无关代码导致它的上下文很长。默认的5行上下文可能就不够提供足够指示。所以显示在屏幕上之后很可能没有足够的上下文来让我们理解错误的发生。我们可以给 cgitb
指定更多的上下文值来解决这个问题。给 enable()
的 context
指定一个整数即可控制显示出来的上下文代码量。
我们设置为 12
之后就发现 self.a
和 self.b
也牵扯其中。
$ python3 cgitb_with_classes.py
zerodivisionerror
python 3.6.4: .../bin/python3
sun mar 18 16:20:19 2018
a problem occurred in a python script. here is the sequence of
function calls leading up to the error, in the order they
occurred.
.../cgitb_with_classes.py in ()
21 self.a = a
22 self.b = b
23 self.c = self.a * self.b
24 # really
25 # long
26 # comment
27 # goes
28 # here.
29 self.d = self.a / self.b
30 return
31
32 o = brokenclass(1, 0)
o undefined
brokenclass =
.../cgitb_with_classes.py in
__init__(self=<__main__.brokenclass object>, a=1, b=0)
21 self.a = a
22 self.b = b
23 self.c = self.a * self.b
24 # really
25 # long
26 # comment
27 # goes
28 # here.
29 self.d = self.a / self.b
30 return
31
32 o = brokenclass(1, 0)
self = <__main__.brokenclass object>
self.d undefined
self.a = 1
self.b = 0
zerodivisionerror: division by zero
__cause__ = none
__class__ =
__context__ = none
__delattr__ =
__dict__ = {}
__dir__ =
__doc__ = 'second argument to a division or modulo operation
was zero.'
__eq__ =
__format__ =
__ge__ =
__getattribute__ =
__gt__ =
__hash__ =
__init__ =
__init_subclass__ =
__le__ =
__lt__ =
__ne__ =
__new__ =
__reduce__ =
__reduce_ex__ =
__repr__ =
__setattr__ =
__setstate__ =
__sizeof__ =
__str__ =
__subclasshook__ =
__suppress_context__ = false
__traceback__ =
args = ('division by zero',)
with_traceback =
the above is a description of an error in a python program.
here is
the original traceback:
traceback (most recent call last):
file "cgitb_with_classes.py", line 32, in
o = brokenclass(1, 0)
file "cgitb_with_classes.py", line 29, in __init__
self.d = self.a / self.b
zerodivisionerror: division by zero
异常属性
除了每个栈帧中的相关域的变量,cgitb
还会显示出异常对象中所有的属性。自定义异常中的额外属性也会打印到错误报告中。
cgitb_exception_properties.py
import cgitb
cgitb.enable(format='text')
class myexception(exception):
"""
添加一个额外的属性。
"""
def __init__(self, message, bad_value):
self.bad_value = bad_value
exception.__init__(self, message)
return
raise myexception('normal message', bad_value=99)
本例中的 bad_value
属性和标准的 message
和其他参数一起被打印了出来。
$ python3 cgitb_exception_properties.py
myexception
python 3.6.4: .../bin/python3
sun mar 18 16:20:19 2018
a problem occurred in a python script. here is the sequence of
function calls leading up to the error, in the order they
occurred.
.../cgitb_exception_properties.py in ()
19 self.bad_value = bad_value
20 exception.__init__(self, message)
21 return
22
23 raise myexception('normal message', bad_value=99)
myexception =
bad_value undefined
myexception: normal message
__cause__ = none
__class__ =
__context__ = none
__delattr__ =
__dict__ = {'bad_value': 99}
__dir__ =
__doc__ = 'add extra properties to a special exception\n
'
__eq__ =
__format__ =
__ge__ =
__getattribute__ =
__gt__ =
__hash__ =
__init__ =
__init_subclass__ =
__le__ =
__lt__ =
__module__ = '__main__'
__ne__ =
__new__ =
__reduce__ =
__reduce_ex__ =
__repr__ =
__setattr__ =
__setstate__ =
__sizeof__ =
__str__ =
__subclasshook__ =
__suppress_context__ = false
__traceback__ =
__weakref__ = none
args = ('normal message',)
bad_value = 99
with_traceback =
the above is a description of an error in a python program.
here is
the original traceback:
traceback (most recent call last):
file "cgitb_exception_properties.py", line 23, in
raise myexception('normal message', bad_value=99)
myexception: normal message
html 输出
cgitb
原本就是被设计用来处理 web 应用程序中的异常的,没理由不提一下 html 格式输出。之前的例子我们用的全是普通文本输出。要想输出 html 格式,我们可以不指定 format
(或指定为 "html"
)。不过大部分现在的 web 应用程序都使用带有错误报告的框架,所以 它的 html 格式也基本过时了。
记录 traceback
大多数场景中,打印出标准错误的追踪详情是很好的解决方式。不过在生产系统中,把它记录下来更加棒一些。enable()
函数有一个 logdir
参数,可以指定错误的记录目录。指定了目录后,每条异常都会被记录到给定的目录中。
cgitb_log_exception.py
import cgitb
import os
logdir = os.path.join(os.path.dirname(__file__), 'logs')
if not os.path.exists(logdir):
os.makedirs(logdir)
cgitb.enable(
logdir=logdir,
display=false,
format='text',
)
def func(a, divisor):
return a / divisor
func(1, 0)
即使错误被抑制,仍然会有一条描述在哪发生该错误的消息被打印。
$ python3 cgitb_log_exception.py
a problem occurred in a python script. .../logs/tmpq7icvee3.txt contains the description of this error. $ ls logs tmpq7icvee3.txt $ cat logs/*.txt zerodivisionerror python 3.6.4: .../bin/python3 sun mar 18 16:20:19 2018 a problem occurred in a python script. here is the sequence of function calls leading up to the error, in the order they occurred. .../cgitb_log_exception.py in
() 24 25 def func(a, divisor): 26 return a / divisor 27 28 func(1, 0) func = .../cgitb_log_exception.py in func(a=1, divisor=0) 24 25 def func(a, divisor): 26 return a / divisor 27 28 func(1, 0) a = 1 divisor = 0 zerodivisionerror: division by zero __cause__ = none __class__ = __context__ = none __delattr__ = __dict__ = {} __dir__ = __doc__ = 'second argument to a division or modulo operation was zero.' __eq__ = __format__ = __ge__ = __getattribute__ = __gt__ = __hash__ = __init__ = __init_subclass__ = __le__ = __lt__ = __ne__ = __new__ = __reduce__ = __reduce_ex__ = __repr__ = __setattr__ = __setstate__ = __sizeof__ = __str__ = __subclasshook__ = __suppress_context__ = false __traceback__ = args = ('division by zero',) with_traceback = the above is a description of an error in a python program. here is the original traceback: traceback (most recent call last): file "cgitb_log_exception.py", line 28, in func(1, 0) file "cgitb_log_exception.py", line 26, in func return a / divisor zerodivisionerror: division by zero
参阅
- -- 追踪信息的标准库模块
- -- 有更多检查栈信息的函数的模块。
- -- 访问当前异常信息和查看(修改)异常处理器(
excepthook
)。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系江南app体育官方入口。