PEP 329 and bytecode hacking

Robert Brewer fumanchu at amor.org
Wed Apr 21 18:07:44 EDT 2004


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.

Bytecode is scanned in order, and if the Visitor class has a method
'visit_OPNAME', that's called (with the opcode argument, if present).
This is similar in interface style to compiler.Transformer, SGMLParser,
etc. It's nowhere near bulletproof or complete, and needs docs, but it's
a good start: 'codewalk.py' at http://www.aminus.org/rbre/python/ All
comments welcome.

It includes the class LocalizeGlobals, which provides the same
functionality as PEP 329. I hope to make BindEarly stronger so I can use
it for generating SQL from simple lambdas (for an OO-DB mapper I'm
working on). Heck, I'd like to rewrite the dis module with it sometime.
;)


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list