Getting a python traceback without an exception

好久不见. 提交于 2019-12-03 11:21:51

traceback.print_stack works nicely for me:

>>> import traceback
>>> def what():
...    traceback.print_stack()
... 
>>> def hey():
...    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.py

def what():
    return 3

def hey():
    return what()

def yo():
    return hey()

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):     return 3
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!