Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDB: exception when in console - full stack trace

Tags:

python

pdb

When at the pdb console, entering a statement which causes an exception results in just a single line stack trace, e.g.

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

However I'd like to figure out where exactly in someFunc the error originates. i.e. in this case, which class __init__ is attached to.

Is there a way to get a full stack trace in Pdb?

like image 568
EoghanM Avatar asked May 02 '10 18:05

EoghanM


1 Answers

The easiest way would be to define a function in your code that calls someFunc() and prints the traceback then call that from Pdb.

Alternatively you can print the traceback for yourself. Given this source code:

def foo(a):
    pass

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

def some_func():
    bar(3)

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

Then we can do this:

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)
like image 64
Duncan Avatar answered Oct 11 '22 08:10

Duncan