Chuck Rhode <[email protected]> added the comment:
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.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue6978>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com