The Default Trace

# python standard library
from trace import Trace

from test_class import TestExec

The exec Statement

To use Trace you can pass its run method either a string or an object that the exec statement accepts.

t = TestExec()
exec('t.run_this()')

Getting that to work had me stumped for a bit. Since the documentation says code object I thought you would pass in the TestExec instance, but it has some kind of magic going on that allows you to import or define code and then run it using strings.

The Default Trace

With that background out of the way we can see what the default behavior is.

trace = Trace()
try:
    trace.run('t.run_this_and_that()')
    print 'END_DEFAULT'
except NameError:
    print NameError
 --- modulename: Pweave, funcname: <module>
<string>(1):   --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)
<type 'exceptions.NameError'>

Okay, it turns out that Pweave raises a NameError if you run a trace inside of it so I have to add it afterwards, but if you run the tangled python code the trace will dump to stdout:

1,2,3
 --- modulename: explore_trace, funcname: <module>
<string>(1):   --- modulename: explore_trace, funcname: run_this_and_that
explore_trace.py(22):         self.run_this()
 --- modulename: explore_trace, funcname: run_this
explore_trace.py(11):         x = 1
explore_trace.py(12):         y = 2
explore_trace.py(13):         print "{0},{1},{2}".format(x,y,x+y)
1,2,3
explore_trace.py(14):         return
explore_trace.py(23):         self.run_that()
 --- modulename: explore_trace, funcname: run_that
explore_trace.py(17):         self.x = 1
explore_trace.py(18):         self.y = 2
explore_trace.py(19):         return
explore_trace.py(24):         return
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)