title: Getting a python traceback without an exception
body: <p>Suppose you have these modules:</p>
  
  <h2>module1.py</h2>
  
  <pre><code>import module2
  
  def a():
      module1.b()
  
  def c():
      print "Hi guys!"
  </code></pre>
  
  <h2>module2.py</h2>
  
  <pre><code>import module1
  
  def b():
      module1.c()
  </code></pre>
  
  <p>I want a function <code>func(a())</code> that produces a similar output to this: (=a traceback ?)</p>
  
  <pre><code> /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!"
  </code></pre>
  
  <p>It might be possible with the standard modules <code>traceback</code> and/or <code>cgitb</code> and/or <code>inspect</code> but I am having a hard time figuring out these modules from the documentation.</p>
  
  <p>I thought it was possible doing <code>traceback.print_stack(a())</code> but it just kept on loading forever for some reason. I tried other functions in those modules but without success.</p>
  
  <h2>UPDATE 3</h2>
  
  <p>@jterrace</p>
  
  <p>python trapy_module.py :</p>
  
  <pre><code>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()')
  </code></pre>
  
  <p>Now when I do:
  <code>python trapy_module.py</code> I get:</p>
  
  <pre><code>--- modulename: trapy, funcname: &lt;module&gt;
  &lt;string&gt;(1): 
  </code></pre>
  
  <p>Replacing <code>import random</code> with <code>import pyglet</code> and <code>random.random()</code> with <code>pyglet.app.run()</code> just keeps running without outputting anything.</p>