Injecting code into a function
Kay Schluehr
kay.schluehr at gmx.net
Tue Apr 26 02:54:07 EDT 2005
George Sakkis wrote:
> > Oh, I overlooked this. Then the solution becomes simple:
> >
> > sys._getframe().f_trace
> >
> > Test:
> >
> > >>> an = Analyzer()
> > >>> sys.settrace(an.trace_returns)
> > >>> sys._getframe().f_trace
> > <bound method Analyzer.trace_returns of <__main__.Analyzer instance
> at
> > 0x010015D0>>
>
> Does this work for you non-interactively ? I tried running it from a
> script or importing it from a module but it returns None. Very
> strange...
>
> George
You are right. The expression was context-dependent :-/
I had not yet the time to analyze the output of the following function
but it returns stable values:
def gettracefunc():
import sys
i = 0
while 1:
try:
f_trace = sys._getframe(i).f_trace
if f_trace:
return f_trace
i+=1
except ValueError:
break
Ciao,
Kay
More information about the Python-list
mailing list