[Tutor] [newbie] output formatting [using traceback to print call stacks]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Nov 5 12:55:43 EST 2003


> PPS. Is there a Python (or maybe Perl (!)) prog that prints the 'call
> stack' of a Python program? I need to analyse other peoples programs,
> that's why I need it.

Hello!


Yes; the 'traceback' module has functions that allow us to see the call
stack:

    http://www.python.org/doc/lib/module-traceback.html


The function traceback.print_stack() should do the trick:

###
>>> def foo():
...     bar()
...
>>> def bar():
...     baz()
...
>>> def baz():
...     traceback.print_stack()
...
>>> foo()
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in foo
  File "<stdin>", line 2, in bar
  File "<stdin>", line 2, in baz
###


Hope this helps!




More information about the Tutor mailing list