如何在 Python 中获取完整的异常堆栈跟踪
以下代码片段:
import traceback
def a():
b()
def b():
try:
c()
except:
traceback.print_exc()
def c():
assert False
a()
产生此输出:
Traceback (most recent call last):
File "test.py", line 8, in b
c()
File "test.py", line 13, in c
assert False
AssertionError
如果我想要完整的堆栈跟踪(包括对 a 的调用),我应该使用什么?
如果重要的话,我有Python 2.6.6
编辑:我想要得到的信息与我将 try except 留在外面并让异常传播到顶层时得到的信息相同。例如,此片段:
def a():
b()
def b():
c()
def c():
assert False
a()
产生此输出:
Traceback (most recent call last):
File "test.py", line 10, in <module>
a()
File "test.py", line 2, in a
b()
File "test.py", line 5, in b
c()
File "test.py", line 8, in c
assert False
AssertionError
The following snippet:
import traceback
def a():
b()
def b():
try:
c()
except:
traceback.print_exc()
def c():
assert False
a()
Produces this output:
Traceback (most recent call last):
File "test.py", line 8, in b
c()
File "test.py", line 13, in c
assert False
AssertionError
What should I use if I want the complete stack trace including the call to a?
If it matters I have Python 2.6.6
edit: What I'd like to get is the same information I'd get if I left the try except out and let the exception propagate to the top level. This snippet for example:
def a():
b()
def b():
c()
def c():
assert False
a()
Produces this output:
Traceback (most recent call last):
File "test.py", line 10, in <module>
a()
File "test.py", line 2, in a
b()
File "test.py", line 5, in b
c()
File "test.py", line 8, in c
assert False
AssertionError
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个基于此答案的函数。当不存在异常时它也将工作:
print full_stack()
将打印完整的堆栈跟踪到顶部,包括例如IPython的interactiveshell.py
调用,因为有(据我所知)无法知道谁会捕获异常。无论如何,这可能不值得弄清楚...如果从
except
块内调用print full_stack()
,full_stack
将包含堆栈跟踪一直到加注
。在标准 Python 解释器中,这与您在未捕获异常时收到的消息相同(这就是为什么del stack[-1]
存在,您不关心except 块但关于
try 块)。
Here's a function based on this answer. It will also work when no exception is present:
print full_stack()
will print the full stack trace up to the top, including e.g. IPython'sinteractiveshell.py
calls, since there is (to my knowledge) no way of knowing who would catch exceptions. It's probably not worth figuring out anyway...If
print full_stack()
is called from within anexcept
block,full_stack
will include the stack trace down to theraise
. In the standard Python interpreter, this will be identical to the message you receive when not catching the exception (Which is why thatdel stack[-1]
is there, you don't care about theexcept
block but about thetry
block).我不知道是否有更好的方法,但这就是我所做的:
这是我得到的输出:
I don't know if there is a better way, but here's what I did:
And here's the output I get:
使用
http://docs.python.org/library/traceback.html#traceback.print_stack
Use
http://docs.python.org/library/traceback.html#traceback.print_stack
这是Tobias Kienzler 答案的一个更好的变体。它的工作原理相同,但不能在
except
块中正确调用,而是在更深的地方调用。换句话说,当像
或 这样
调用时,此变体将打印相同的堆栈,这是代码:
Here is a bit better variant of Tobias Kienzler answer. It works same, but can be called not right in
except
block, but somewhere deeper.In other words, this variant will print same stacks, when called like
or
Here is code: