[英]Why exc_traceback returns None
sys.exc_info() returns a tuple (type , value, traceback). sys.exc_info()返回一个元组(类型,值,回溯)。
so sys.exc_info()[2]
is our traceback object. 因此
sys.exc_info()[2]
是我们的回溯对象。
Why it does not catch exceptions traceback with this code: 为什么它不使用以下代码捕获异常回溯:
import sys
try:
1/0
except ZeroDivisionError:
print sys.exc_info()[2].tb_frame.f_back
tb_frame and f_back usage has been explained here: Frame Objects tb_frame和f_back用法已在此处说明: 框架对象
You see None
because there is no outer frame . 您会看到“
None
因为没有外部框架 。 You're executing this directly, so the current frame is the last frame. 您直接执行此操作,因此当前帧是最后一帧。 To demonstrate this, I created a
demo.py
: 为了演示这一点,我创建了一个
demo.py
:
import sys
try:
1/0
except ZeroDivisionError:
print sys.exc_info()[2].tb_frame.f_back
which should look familiar, and a trivial caller.py
: 应该看起来很熟悉,还有一个琐碎的
caller.py
:
import demo
Now see the difference: 现在看到区别:
$ python demo.py
None
$ python caller.py
<frame object at 0x10bc34c20>
In the second case, where there is an outer frame (ie caller.py
), you don't see None
. 在第二种情况下,如果有一个外部框架(即
caller.py
),则看不到None
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.