[Python-ideas] Arbitrary constants in ASTs

Nick Coghlan ncoghlan at gmail.com
Wed Jul 24 17:24:42 CEST 2013


On 24 July 2013 21:23, Victor Stinner <victor.stinner at gmail.com> wrote:
> 2013/7/24 Haoyi Li <haoyi.sg at gmail.com>:
>> How many other people in the world are doing AST manipulations to want this?
>
> I wrote a project called astoptimizer which rewrites AST to implement
> some static optimizations.
> http://bitbucket.org/haypo/astoptimizer/
>
> I need a get_constant() method checking if a node is "a constant". It
> creates a tuple from a ast.Tuple tree for example. Having to
> "encode/decode" (create tuple <=> create ast.Tuple) regulary is not
> efficient. I would also like a first pass in the optimizer which would
> replace ast.Num, ast.Str, ast.NameConstant, ast.Tuple, etc. to a
> generic ast.Constant which contains directly the Python object.
>
> See my get_constant() method:
> https://bitbucket.org/haypo/astoptimizer/src/dfb2c702cb14785320ac34b868d5e1270f910825/astoptimizer/optimizer.py?at=default#cl-289

There are multiple patches on the tracker to improve the AST (most
notably Eugene Toder's work in http://bugs.python.org/issue11549).
Anyone interested in improving this would be well-advised to
familiarise themselves with those patches, with a view to updating it
in response to the comments already received and bring it inline with
the current default branch (also keep in mind that the dis module in
3.4 has new capabilities to make bytecode analysis easier, and there
is a test.bytecode_helper module to make it easier to write regression
tests for the code generator).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list