Mark wrote:
Peter Hansen wrote:
"local" trace function to take over once I execute the function I'm
interested in tracing? I'd like to use the "event" argument to take note
of calls that are 1 level deep (ie in my foo function, any calls made) and
turn off tracing of these until they return back to the level of foo.
Peter Hansen wrote:
Sorry, I couldn't find a solution that was larger than six lines:
... if event == 'line':
... print '%s: %s' % (frame.f_code.co_filename, frame.f_lineno)
... return trace
...
This is exactly what I needed. Now, can you explain to me how I hook in adef trace(frame, event, arg):
... print '%s: %s' % (frame.f_code.co_filename, frame.f_lineno)
... return trace
...
import sys
sys.settrace(trace)
sys.settrace(trace)
"local" trace function to take over once I execute the function I'm
interested in tracing? I'd like to use the "event" argument to take note
of calls that are 1 level deep (ie in my foo function, any calls made) and
turn off tracing of these until they return back to the level of foo.
"method" as an object with a __call__ method so you can track state as
well as receive the tracing calls.
As for local tracing... the above function actually does that already.
As the docs note, the global function (installed by sys.settrace)
returns a reference to a local trace function when event is "call".
I'm just using the same function for both purposes as a cheat. :)
I presume I could implement this with a "if event == 'call':" condition, but
I can't seem to get a global trace function to hook up to a local trace
function ... it does one or two things right, then goes crazy.
Post sample code if you're having trouble... but I recommend playingI can't seem to get a global trace function to hook up to a local trace
function ... it does one or two things right, then goes crazy.
a lot at the interactive prompt before doing so. You learn lots more
that way.
-Peter