PDB:在控制台中的异常 - 完整堆栈跟踪

时间:2010-05-02 18:01:18

标签: python pdb

在pdb控制台上,输入导致异常的语句只会导致单个行堆栈跟踪,例如:

(Pdb) someFunc()
*** TypeError: __init__() takes exactly 2 arguments (1 given)

但是,我想弄清楚错误源自someFunc的确切位置。即在这种情况下,附加了__init__个类。

有没有办法在Pdb中获得完整的堆栈跟踪?

2 个答案:

答案 0 :(得分:7)

最简单的方法是在代码中定义一个函数,调用someFunc()并打印回溯,然后从Pdb调用它。

或者,您可以自己打印回溯。鉴于此源代码:

def foo(a):
    pass

def bar(b):
    foo(b, 2)

def some_func():
    bar(3)

if __name__=='__main__':
    import pdb
    pdb.set_trace()

然后我们可以这样做:

C:\temp>test.py
--Return--
> c:\temp\test.py(12)<module>()->None
-> pdb.set_trace()
(Pdb) import traceback
(Pdb) exec "try: some_func()\nexcept: traceback.print_exc()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\temp\test.py", line 8, in some_func
    bar(3)
  File "C:\temp\test.py", line 5, in bar
    foo(b, 2)
TypeError: foo() takes exactly 1 argument (2 given)
(Pdb)

答案 1 :(得分:1)

pdb支持递归调用的debug语句:

$ nosetests xxxx -x --pdb
-> some code line with error
(Pdb) debug os.sdfafa()  # something that raises exception
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()  # new line that raised excetion
((Pdb))  # number of parentheses indicates debugger recusion depth
((Pdb)) debug os.someothersdfsdf()
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
(((Pdb))) 
相关问题