Skip to content Skip to sidebar Skip to footer

Getting A Python Traceback Without An Exception

Suppose you have these modules: module1.py import module2 def a(): module1.b() def c(): print 'Hi guys!' module2.py import module1 def b(): module1.c() I want a fu

Solution 1:

traceback.print_stack works nicely for me:

>>>import traceback>>>defwhat():...   traceback.print_stack()...>>>defhey():...   what()...>>>hey()
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in hey
  File "<stdin>", line 2, in what

UPDATE:

You've made it clear you really don't want a traceback. You want tracing information. Here's a way you can get some trace info:

#tracetest.pydef what():
    return 3

def hey():
    returnwhat()

def yo():
    returnhey()

import trace
tracer = trace.Trace()
tracer.run("yo()")
r = tracer.results()
r.write_results()

and running the above:

$ python tracetest.py
 --- modulename: tracetest, funcname: <module>
<string>(1):   --- modulename: tracetest, funcname: yo
tracetest.py(8):     return hey()
 --- modulename: tracetest, funcname: hey
tracetest.py(5):     return what()
 --- modulename: tracetest, funcname: what
tracetest.py(2):     return3

Post a Comment for "Getting A Python Traceback Without An Exception"