FAQ

Mark wrote:
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
that way.

-Peter

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 9 of 18 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedSep 19, '02 at 2:54a
activeSep 22, '02 at 11:51p
posts18
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase