[Python-ideas] PEP 511: API for code transformers

Victor Stinner victor.stinner at gmail.com
Wed Jan 27 11:44:56 EST 2016


Hi,

2016-01-16 17:56 GMT+01:00 Kevin Conway <kevinjacobconway at gmail.com>:
> I'm a big fan of your motivation to build an optimizer for cPython code.
> What I'm struggling with is understanding why this requires a PEP and
> language modification. There are already several projects that manipulate
> the AST for performance gains such as [1] or even my own ham fisted attempt
> [2].

Oh cool, I didn't know PyCC [2]! I added it to the PEP 511 in the AST
optimizers section of Prior Art.

I wrote astoptimizer [1] and this project uses monkey-patching of the
compile() function, I mentioned this monkey-patching hack in the
rationale of the PEP:
https://www.python.org/dev/peps/pep-0511/#rationale

I would like to avoid monkey-patching because it causes various issues.

The PEP 511 also makes transformations more visible: transformers are
explicitly registered in sys.set_code_transformers() and the .pyc
filename is modified when the code is transformed.

It also adds a new feature: it becomes possible to run transformed
code without having to register the tranformer at runtime. This is
made possible with the addition of the -o command line option.

Victor


More information about the Python-ideas mailing list