Minimal debug/rep functionality

Mark Hobbes2176 at
Thu Sep 19 15:46:55 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)
> -Peter


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 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.


More information about the Python-list mailing list