PEP 329 and bytecode hacking

Phillip J. Eby pje at telecommunity.com
Wed Apr 21 18:51:15 EDT 2004


At 03:07 PM 4/21/04 -0700, Robert Brewer wrote:
>Phillip J. Eby wrote (on python-dev):
> > ...builtins can potentially be optimized away
> > altogether (e.g. 'while True:') or converted to fast
> > LOAD_CONST, or perhaps even a new CALL_BUILTIN opcode...
>
>Raymond's cookbook recipe inspired me to write a generic Visitor for
>inspecting and rewriting bytecode. Maybe it'll help someone out there
>with prototyping bytecode hacks.

For more sophisticated bytecode modifications, you'll want to keep track of 
things like source line number, the nth instruction and its operand if any 
(so you can scan *backwards*), the list of locations of a given opcode, 
etc.  This Pyrex module:

http://cvs.eby-sarna.com/PEAK/src/peak/util/_Code.pyx?rev=1.2&content-type=text/vnd.viewcvs-markup

provides high-speed dynamic access to bytecode metadata, and is suitable 
for use in runtime bytecode hacks, since it does not require Python 
function call overhead for each instruction.  E.g. instead of defing a 
'visit_OPNAME' method that then gets called numerous times, you simply do 
something like:

     from peak.util._Code import codeIndex

     idx = codeIndex(someCodeObject)

     for op in (DELETE_NAME, DELETE_GLOBAL):
         for i in idx.opcodeLocations(op):
             warn_explicit(
                 "Deletion of global during initialization",
                 ModuleInheritanceWarning,
                 someCodeObject.co_filename,
                 idx.byteLine(idx.offset(i)),
             )

which demos the use of the byteLine (line number for a given offset in the 
code) and offset (offset of the nth instruction) methods.





More information about the Python-list mailing list