Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 421
Bentley4 Avatar asked Jun 14 '12 16:06

Bentley4


People also ask

How do I fix Python traceback error?

The traceback error also shows the type of error and information about that error. The above case is IndexError: list index out of range . You can fix it using the valid index number to retrieve an item from a list.

Is traceback an error in Python?

In Python, A traceback is a report containing the function calls made in your code at a specific point i.e when you get an error it is recommended that you should trace it backward(traceback). Whenever the code gets an exception, the traceback will give the information about what went wrong in the code.


1 Answers

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
like image 196
jterrace Avatar answered Sep 23 '22 00:09

jterrace