Issue 6978: compiler.transformer dict key bug d[1,] = 1 (original) (raw)
compiler.parse("d[1] = 1") should have a single tuple as subs
compiler.parse("d[1] = 1") Module(None, Stmt([Assign([Subscript(Name('d'), 'OP_ASSIGN', [Const(1)])], Const(1))])) compiler.parse("d[1,] = 2") Module(None, Stmt([Assign([Subscript(Name('d'), 'OP_ASSIGN', [Const(1)])], Const(2))])) compiler.parse("d[1,2] = 3") Module(None, Stmt([Assign([Subscript(Name('d'), 'OP_ASSIGN', [Const(1), Const(2)])], Const(3))])) compiler.parse("d[(1,)] = 2") Module(None, Stmt([Assign([Subscript(Name('d'), 'OP_ASSIGN', [Tuple([Const(1)])])], Const(2))]))
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, '', '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.