Injecting code into a function

Kay Schluehr kay.schluehr at gmx.net
Mon Apr 25 18:50:28 EDT 2005


George Sakkis wrote:

> Thanks, that's the closest to what I wanted. A minor point I didn't
> quite get from the documentation is how to set a local trace instead
of
> a global (sys) trace.

You never do. "Local" in this context only means that those local trace
functions are called inside the one global trace-function and return
the global trace function again.

In Lonnies example there are no local-trace functions at all.

> Also, there's no sys.gettrace() to return the
> current tracer; is there a way around this ?

The default tracer is None i.e. no debugging. The programmer has to
control his tracer which might not be to hard:


class Analyzer:
    def trace_returns(self, frame, event, arg):
        if event == 'return':
            self.func_locals = frame.f_locals
        return self.trace_returns

    def analyzeLocals(self, func,*args,**kw):
        sys.settrace(self.trace_returns)
        func(*args,**kw)
        sys.settrace(None)

Ciao,
Kay




More information about the Python-list mailing list