You are not logged in Log in Join
You are here: Home » Members » 4AM Productions (Evan Simpson) » PythonMethodWiki » CodeBlock

Log in
Name

Password

 
 
FrontPage » GeneralDescription »

CodeBlock

CodeBlock is defined in zbytecodehacks/VSExec.py.

CodeBlock is a base class for bytecode munging of code strings. Derived classes should override forbid and Mungers. The class constructor takes a function name, a parameter string, and a function body string. It combines the name, parameters, and body into a function (indented one space), compiles it, and then examines its bytecode.

Any bytecode whose opcode is a key in forbid with a true value may not appear in the compiled code.

Each object in Mungers is called with the code window object (at start) and the variable name information for the function. Mungers can use this opportunity to examine the code and variables, and perform setup operations. The value returned from the call is discarded if false, otherwise it is assumed to be a collection of bytecode-specific functions.

The code window object is passed over the bytecodes, maintaining a stack of "responsible" opcodes. At any given position, the size of the stack should be the same as the size of the run-time stack would be when that part of the code is executing, and the top of the stack would have been put there by the "responsible" opcode.

At each position, the mungers are examined to see if they have a function corresponding to the current opcode. If so, the function is called with the code window object. The function can then use the code window object to examine or change the bytecode.

Once all processing is done, and if there have not been any errors, the compiled, edited function is converted into a pickleable tuple, which is stored as attribute t of the resulting instance. In order for this to work, mungers must not place unpickleable objects into the list of constants.

In order for unpickling to be robust, they should not place any function objects there either. If a function must be provided for use in the bytecode, it should be loaded from the global dictionary, preferably with an "illegal" name.