怎么看完整的追溯?

时间:2012-12-16 17:32:12

标签: python stack-trace traceback

我在函数中添加了一个assert(0)来理解函数调用这个函数的顺序。

例如:

def my_func():
    ...lines of code...
    assert(0)
    ...more lines of code...

日志只显示:

Traceback (most recent call last):
  File "/var/www/folder/file.py", line 273, in my_func
    assert(0)
AssertionError

我希望看到完整的通话追踪 - 示例:first_func - > second_func - > my_func,并将

我尝试了追溯库,但它向我展示了相同的堆栈跟踪。 请告诉我这里缺少的东西。

1 个答案:

答案 0 :(得分:3)

使用traceback模块。即。

>>> import traceback
>>> def my_func():
...     my_other_func()
... 
>>> def my_other_func():
...     my_third()
... 
>>> def my_third():
...     print "Stack"
...     traceback.print_stack()
...     print "Extracted"
...     print repr(traceback.extract_stack())
... 
>>> 
>>> my_func()
Stack
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in my_func
  File "<stdin>", line 2, in my_other_func
  File "<stdin>", line 3, in my_third
Extracted
[('<stdin>', 1, '<module>', None), 
 ('<stdin>', 2, 'my_func', None), 
 ('<stdin>', 2, 'my_other_func', None), 
 ('<stdin>', 5, 'my_third', None)]
>>> 
相关问题