Python 3 traceback fails when no exception is active

◇◆丶佛笑我妖孽 提交于 2019-12-01 07:28:09

问题


I noticed that in Python2 when I try to dump the exception stack trace, but there's no active exception, it prints None:

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
>>> import traceback
>>> traceback.print_exc()
None

But Python3 fails with some internal problem:

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
>>> import traceback
>>> traceback.print_exc()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/traceback.py", line 259, in print_exc
    print_exception(etype, value, tb, limit, file, chain)
  File "/usr/lib/python3.2/traceback.py", line 155, in print_exception
    for value, tb in values:
  File "/usr/lib/python3.2/traceback.py", line 122, in _iter_chain
    cause = exc.__cause__
AttributeError: 'NoneType' object has no attribute '__cause__'

Is this a bug in Python, or should I always test for some condition before calling print_exc()


回答1:


That is a bug. It might be decided that an error should be raised, but that is definitely the wrong error.



来源:https://stackoverflow.com/questions/9403986/python-3-traceback-fails-when-no-exception-is-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!