Editing a function in-memory and in-place

Terry Reedy tjreedy at udel.edu
Thu Apr 27 01:41:17 EDT 2006


"Ian Bicking" <ianb at colorstudy.com> wrote in message 
news:1146115733.082702.106380 at t31g2000cwb.googlegroups.com...
>
> The func_code attributes of functions is writable, but I don't know how
> to create the proper code object.  Just compiling a new body isn't good
> enough.

Did you directly compile the body or compile a function and then extract 
the code object?  In any case, if you need to patch the code object and 
cannot do so directly due to read-only fields, this may help:

>>> import new
>>> dir(new)
['__builtins__', '__doc__', '__file__', '__name__', 'classobj', 'code', 
'function', 'instance', 'instancemethod', 'module']
>>> help(new.code)
Help on class code in module __builtin__:

class code(object)
 |  code(argcount, nlocals, stacksize, flags, codestring, constants, names,
 |        varnames, filename, name, firstlineno, lnotab[, freevars[, 
cellvars]])
 |
 |  Create a code object.  Not for the faint of heart.
 |
 |  Methods defined here:
...

Terry Jan Reedy






More information about the Python-list mailing list