Injecting code into a function

George Sakkis gsakkis at rutgers.edu
Tue Apr 26 02:31:29 EDT 2005


> I'm not clear on what your real goal is, but if you just want a
snapshot
> of what locals() is just before exiting func, that could be done with
> a byte-code-hacking decorator with usage looking something like
>
>     #func defined before this
>     func_locals = {}
>     @getlocals(func, func_locals)
>     def probefunc(): pass
>
> which would make a probefunc function that would be identical to func
> except that before exiting, it would do func_locals.update(locals()).
> (you might want func_locals to be a list and do
func_locals.append(locals())
> in case func is recursive and you are interested in the all the
locals).

That's all good, at least if I knew how to poke with bytecodes ;-)
What's a good starting point to look at ?

By the way, the original problem was yet another property packager,
which I posted as recipe at the cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698.

George




More information about the Python-list mailing list