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 function func(a())
that produces a similar output to this: (=a traceback ?)
/usr/local/lib/python2.7/dist-packages/test/module1.py
3 def a():
4 module1.b()
1 import module1
/usr/local/lib/python2.7/dist-packages/test/module2.py
3 def b():
4 module1.c()
1 import module2
/usr/local/lib/python2.7/dist-packages/test/module1.py
6 def c():
7 print "Hi guys!"
It might be possible with the standard modules traceback
and/or cgitb
and/or inspect
but I am having a hard time figuring out these modules from the documentation.
I thought it was possible doing traceback.print_stack(a())
but it just kept on loading forever for some reason. I tried other functions in those modules but without success.
UPDATE 3
@jterrace
python trapy_module.py :
import trace
def trapy(arg):
tracer = trace.Trace()
tracer.run(arg)
r = tracer.results()
r.write_results()
if __name__ == '__main__':
import random
trapy('random.random()')
Now when I do:
python trapy_module.py
I get:
--- modulename: trapy, funcname: <module>
<string>(1):
Replacing import random
with import pyglet
and random.random()
with pyglet.app.run()
just keeps running without outputting anything.
module2.b()
or alternativelyfrom module2 import b
. – Sarena