假设我有一个引发意外异常的函数,所以我将它包装在ipdb中:
def boom(x,y):
try:
x / y
except Exception as e:
import ipdb; ipdb.set_trace()
def main():
x = 2
y = 0
boom(x,y)
if __name__ == '__main__':
main()
我可以向上移动堆栈以找出x和y具有的值:
$python crash.py
> /tmp/crash.py(6)boom()
5 except Exception as e:
----> 6 import ipdb; ipdb.set_trace()
7
ipdb> u
> /tmp/crash.py(11)main()
10 y = 0
---> 11 boom(x,y)
12
ipdb> p y
0
但是,在调试时,我想把调试器放在最顶层:
def boom(x,y):
x / y
def main():
x = 2
y = 0
boom(x,y)
if __name__ == '__main__':
try:
main()
except Exception as e:
import ipdb; ipdb.set_trace()
$python crash.py
> /tmp/crash.py(14)
异常发生时,异常对象显然仍然具有对堆栈的引用.我可以在这里访问x和y,即使堆栈已经解开了吗?
最佳答案
事实证明,可以从traceback对象中提取变量.
原文链接:https://www.f2er.com/python/438540.html要手动提取值:
ipdb> !import sys
ipdb> !tb = sys.exc_info()[2]
ipdb> p tb.tb_next.tb_frame.f_locals
{'y': 0,'x': 2}
更好的是,您可以使用异常在该堆栈上显式执行事后调试:
import sys
def boom(x,y)
if __name__ == '__main__':
try:
main()
except Exception as e:
# Most debuggers allow you to just do .post_mortem()
# but see https://github.com/gotcha/ipdb/pull/94
tb = sys.exc_info()[2]
import ipdb; ipdb.post_mortem(tb)
这让我们直接看到了令人讨厌的代码:
> /tmp/crash.py(4)boom()
3 def boom(x,y):
----> 4 x / y
5
ipdb> p x
2