This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ChuckRhode
Recipients ChuckRhode, amaury.forgeotdarc, benjamin.peterson, kees, meador.inge
Date 2010-02-02.04:23:48
SpamBayes Score 5.1350155e-05
Marked as misclassified No
Message-id <1265084630.47.0.965397655808.issue6978@psf.upfronthosting.co.za>
In-reply-to
Content
Here are four ways to generate things called in the documentation Abstract Syntax Trees (ASTs).  Obviously they are not all the same kind of object:

#!/usr/local/bin/python2.6

import sys
import compiler
import parser
import ast

STATEMENT = 'd[1,] = 2'

print 'level %s' % sys.version
print compiler.parse(STATEMENT)
print parser.suite(STATEMENT).tolist()
print ast.dump(
    compile(STATEMENT, '<string>', 'exec', ast.PyCF_ONLY_AST),
    annotate_fields=False,
    include_attributes=False,
    )
print ast.dump(
    ast.parse(STATEMENT),
    annotate_fields=False,
    include_attributes=False,
    )

# Fin

Here are the results:

>>> level 2.6.2 (r262:71600, Jun 29 2009, 08:08:18) 
[GCC 4.3.2]
>>> Module(None, Stmt([Assign([Subscript(Name('d'), 'OP_ASSIGN', [Const(1)])], Const(2))]))
>>> [257, [267, [268, [269, [270, [327, [304, [305, [306, [307, [308, [310, [311, [312, [313, [314, [315, [316, [317, [318, [1, 'd']], [322, [9, '['], [323, [324, [304, [305, [306, [307, [308, [310, [311, [312, [313, [314, [315, [316, [317, [318, [2, '1']]]]]]]]]]]]]]]], [12, ',']], [10, ']']]]]]]]]]]]]]]]], [22, '='], [327, [304, [305, [306, [307, [308, [310, [311, [312, [313, [314, [315, [316, [317, [318, [2, '2']]]]]]]]]]]]]]]]]], [4, '']]], [0, '']]
>>> Module([Assign([Subscript(Name('d', Load()), Index(Tuple([Num(1)], Load())), Store())], Num(2))])
>>> Module([Assign([Subscript(Name('d', Load()), Index(Tuple([Num(1)], Load())), Store())], Num(2))])

To me the *compiler* module has vestigial utility.  It would be nice if it returned correct results.
History
Date User Action Args
2010-02-02 04:23:50ChuckRhodesetrecipients: + ChuckRhode, amaury.forgeotdarc, benjamin.peterson, kees, meador.inge
2010-02-02 04:23:50ChuckRhodesetmessageid: <1265084630.47.0.965397655808.issue6978@psf.upfronthosting.co.za>
2010-02-02 04:23:48ChuckRhodelinkissue6978 messages
2010-02-02 04:23:48ChuckRhodecreate