2010-05-02 132 views
4

在pdb控制檯上時,輸入導致異常的語句只會導致單個堆棧跟蹤,例如,PDB:處於控制檯時的異常 - 完整堆棧跟蹤

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

但是我想知道錯誤發生在someFunc的確切位置。即在這種情況下,__init__所屬的類別。

有沒有辦法在Pdb中獲得完整的堆棧跟蹤?

回答

6

最簡單的方法是在代碼中定義一個調用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) 
+5

有沒有什麼辦法來自動做到這一點? – 2013-12-04 14:44:12

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)))