1.5.2 EditableCode

EditableCode objects look very like core Python code objects, but with writable attributes.

EditableCodes are generally constructed using a core Python code object, fiddled with, and then the make_code() method is called to make a new Python code object. Usage tends to look like this:

co=EditableCode(co)
# tweak co
co=co.make_code()

Actually, EditableCodes are usually to be found as members of Function objects, but that's saved for later...

EditableCode ([code])
If code is not supplied, create an emtpy EditableCode.

If code is a tuple or list, pass it to init_tuple.

Otherwise, pass code to init_code.

init_defaults ()
Set all the fields to more or less arbitrarily guessed default values.

init_code (code)
Initialize all fields from (Python core code object) code.

make_code ()
Return a core Python code object to reflect the current state of the EditableCode.

name_index (name)
Adds name to co_names if necessary, and then returns the index of name in said list.

local_index (name)
Adds name to co_varnames if necessary, and then returns the index of name in said list.

rationalize ()
Calls rationalize.rationalize on self to perform some simple optimizations/rationalizations of the bytecode and code object. Many of the bytecodehacks generate rather lousy code, and this method cleans up some of the grosser atrocities. It is automatically invoked in make_code if self.AUTO_RATIONALIZE is true, which is the no longer the default (it makes debugging harder). If you want to turn it on again, use code like this:

from bytecodehacks.code_editor import EditableCode
EditableCode.AUTO_RATIONALIZE = 1

Most of the contents of the rationalize module were written by Corran Webster - thanks very much!

init_tuple (tup)
Initialise the object from the tuple tup.

as_tuple ()
Return a convenient picklable tuple that can be used to initialise an EditableCode.

I've copied the member descriptions form the language reference, and changed them where necessary:

co_argcount
co_argcount is the number of positional arguments (including arguments with default values).
co_flags
The following flag bits are defined for co_flags: bit 2 is set if the function uses the "*arguments" syntax to accept an arbitrary number of positional arguments; bit 3 is set if the function uses the "**keywords" syntax to accept arbitrary keyword arguments; other bits are used internally or reserved for future use.
co_code
A CodeString object.
co_consts
co_consts is a list containing the literals used by the bytecode. If there is a docstring for the bytecode it's stored in co_consts[0]
co_names
co_names is a list containing the names used by the bytecode.
co_varnames
co_varnames is a tuple containing the names of the local variables (starting with the argument names).
co_filename
co_filename is the filename from which the code was compiled.
co_name
co_name gives the function name.
co_firstlineno
co_firstlineno is the first line number of the function.
co_lnotab
co_lnotab is a string encoding the mapping from byte code offsets to line numbers (for details see the source code of the interpreter).
function
The Function the code object is part of.

The alert will notice the absence of co_nlocals and co_stacksize from the above list. That's because these fields are calculated from the others to ensure that they do not become out of date (which could destabilise the interpreter).

Send comments to mwh@python.net