Minimal debug/rep functionality
peter at engcorp.com
Thu Sep 19 19:40:37 EDT 2002
> Peter Hansen wrote:
>>Sorry, I couldn't find a solution that was larger than six lines:
>> >>> def trace(frame, event, arg):
>>... if event == 'line':
>>... print '%s: %s' % (frame.f_code.co_filename, frame.f_lineno)
>>... return trace
>> >>> import sys
>> >>> sys.settrace(trace)
> This is exactly what I needed. Now, can you explain to me how I hook in a
> "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.
I guess you'd need a global to track the level, or implement the trace
"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 playing
a lot at the interactive prompt before doing so. You learn lots more
More information about the Python-list