Bytecode transformers for CPython inspired by the
codetransformer is a library that provides utilities for working with
CPython bytecode at runtime. Among other things, it provides:
Codetype for representing and manipulating Python bytecode.
subclassesfor each opcode used by the CPython interpreter.
CodeTransformertype providing a pattern-based API for describing transformations on
Codeobjects. Example transformers can be found in
- An experimental
decompilerfor determining the AST tree that would generate a code object.
The existence of
codetransformer is motivated by the desire to override
parts of the python language that cannot be easily hooked via more standard
means. Examples of program transformations made possible using code
- Overriding the
- Overloading Python’s data structure literals.
- Optimizing functions by freezing globals as constants.
- Exception handlers that match on exception instances.