Skip to content

Commit b619b09

Browse files
bpo-31241: Fix AST node position for list and generator comprehensions. (GH-10633)
The lineno and col_offset attributes of AST nodes for list comprehensions, generator expressions and tuples are now point to the opening parenthesis or square brace. For tuples without parenthesis they point to the position of the first item.
1 parent d1cbc6f commit b619b09

File tree

5 files changed

+278
-230
lines changed

5 files changed

+278
-230
lines changed

Lib/test/test_ast.py

+40-11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def to_tuple(t):
5555
"del v",
5656
# Assign
5757
"v = 1",
58+
"a,b = c",
59+
"(a,b) = c",
60+
"[a,b] = c",
5861
# AugAssign
5962
"v += 1",
6063
# For
@@ -90,9 +93,8 @@ def to_tuple(t):
9093
"for v in v:continue",
9194
# for statements with naked tuples (see http://bugs.python.org/issue6704)
9295
"for a,b in c: pass",
93-
"[(a,b) for a,b in c]",
94-
"((a,b) for a,b in c)",
95-
"((a,b) for (a,b) in c)",
96+
"for (a,b) in c: pass",
97+
"for [a,b] in c: pass",
9698
# Multiline generator expression (test for .lineno & .col_offset)
9799
"""(
98100
(
@@ -130,6 +132,8 @@ def to_tuple(t):
130132
"@deco1\n@deco2()\nasync def f(): pass",
131133
# Decorated ClassDef
132134
"@deco1\n@deco2()\nclass C: pass",
135+
# Decorator with generator argument
136+
"@deco(a for a in b)\ndef f(): pass",
133137
]
134138

135139
# These are compiled through "single"
@@ -168,12 +172,24 @@ def to_tuple(t):
168172
"[a for b in c if d]",
169173
# GeneratorExp
170174
"(a for b in c if d)",
175+
# Comprehensions with multiple for targets
176+
"[(a,b) for a,b in c]",
177+
"[(a,b) for (a,b) in c]",
178+
"[(a,b) for [a,b] in c]",
179+
"{(a,b) for a,b in c}",
180+
"{(a,b) for (a,b) in c}",
181+
"{(a,b) for [a,b] in c}",
182+
"((a,b) for a,b in c)",
183+
"((a,b) for (a,b) in c)",
184+
"((a,b) for [a,b] in c)",
171185
# Yield - yield expressions can't work outside a function
172186
#
173187
# Compare
174188
"1 < 2 < 3",
175189
# Call
176190
"f(1,2,c=3,*d,**e)",
191+
# Call with a generator argument
192+
"f(a for a in b)",
177193
# Num
178194
"10",
179195
# Str
@@ -1266,6 +1282,9 @@ def main():
12661282
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Constant', (1, 15), 1))], [], None)]),
12671283
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
12681284
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Constant', (1, 4), 1))]),
1285+
('Module', [('Assign', (1, 0), [('Tuple', (1, 0), [('Name', (1, 0), 'a', ('Store',)), ('Name', (1, 2), 'b', ('Store',))], ('Store',))], ('Name', (1, 6), 'c', ('Load',)))]),
1286+
('Module', [('Assign', (1, 0), [('Tuple', (1, 0), [('Name', (1, 1), 'a', ('Store',)), ('Name', (1, 3), 'b', ('Store',))], ('Store',))], ('Name', (1, 8), 'c', ('Load',)))]),
1287+
('Module', [('Assign', (1, 0), [('List', (1, 0), [('Name', (1, 1), 'a', ('Store',)), ('Name', (1, 3), 'b', ('Store',))], ('Store',))], ('Name', (1, 8), 'c', ('Load',)))]),
12691288
('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Constant', (1, 5), 1))]),
12701289
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
12711290
('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
@@ -1284,10 +1303,9 @@ def main():
12841303
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
12851304
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
12861305
('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
1287-
('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)]))]),
1288-
('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)]))]),
1289-
('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 12), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)]))]),
1290-
('Module', [('Expr', (1, 0), ('GeneratorExp', (2, 4), ('Tuple', (3, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [], 0)]))]),
1306+
('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 5), 'a', ('Store',)), ('Name', (1, 7), 'b', ('Store',))], ('Store',)), ('Name', (1, 13), 'c', ('Load',)), [('Pass', (1, 16))], [])]),
1307+
('Module', [('For', (1, 0), ('List', (1, 4), [('Name', (1, 5), 'a', ('Store',)), ('Name', (1, 7), 'b', ('Store',))], ('Store',)), ('Name', (1, 13), 'c', ('Load',)), [('Pass', (1, 16))], [])]),
1308+
('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 0), ('Tuple', (2, 4), [('Name', (3, 4), 'Aa', ('Load',)), ('Name', (5, 7), 'Bb', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (8, 4), [('Name', (8, 4), 'Aa', ('Store',)), ('Name', (10, 4), 'Bb', ('Store',))], ('Store',)), ('Name', (10, 10), 'Cc', ('Load',)), [], 0)]))]),
12911309
('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Name', (1, 11), 'w', ('Store',)), ('Name', (1, 16), 'x', ('Load',)), [], 0), ('comprehension', ('Name', (1, 22), 'm', ('Store',)), ('Name', (1, 27), 'p', ('Load',)), [('Name', (1, 32), 'g', ('Load',))], 0)]))]),
12921310
('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [], 0)]))]),
12931311
('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))], 0)]))]),
@@ -1297,10 +1315,11 @@ def main():
12971315
('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 1), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Constant', (2, 20), 1))])], [], None)]),
12981316
('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Constant', (1, 10), 2)], [('Dict', (1, 3), [('Constant', (1, 4), 1)], [('Constant', (1, 6), 2)]), ('Constant', (1, 12), 3)]))]),
12991317
('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Constant', (1, 3), 1), ('Constant', (1, 6), 2)]), ('Load',)), ('Constant', (1, 10), 3)]))]),
1300-
('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]),
1318+
('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 1), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]),
13011319
('Module', [('FunctionDef', (3, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (3, 9))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])], None)]),
13021320
('Module', [('AsyncFunctionDef', (3, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (3, 15))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])], None)]),
13031321
('Module', [('ClassDef', (3, 0), 'C', [], [], [('Pass', (3, 9))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])])]),
1322+
('Module', [('FunctionDef', (2, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (2, 9))], [('Call', (1, 1), ('Name', (1, 1), 'deco', ('Load',)), [('GeneratorExp', (1, 5), ('Name', (1, 6), 'a', ('Load',)), [('comprehension', ('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 17), 'b', ('Load',)), [], 0)])], [])], None)]),
13041323
]
13051324
single_results = [
13061325
('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Constant', (1, 0), 1), ('Add',), ('Constant', (1, 2), 2)))]),
@@ -1315,10 +1334,20 @@ def main():
13151334
('Expression', ('Dict', (1, 0), [], [])),
13161335
('Expression', ('Set', (1, 0), [('Constant', (1, 1), None)])),
13171336
('Expression', ('Dict', (1, 0), [('Constant', (2, 6), 1)], [('Constant', (4, 10), 2)])),
1318-
('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
1319-
('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
1337+
('Expression', ('ListComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
1338+
('Expression', ('GeneratorExp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))], 0)])),
1339+
('Expression', ('ListComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)])),
1340+
('Expression', ('ListComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])),
1341+
('Expression', ('ListComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])),
1342+
('Expression', ('SetComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)])),
1343+
('Expression', ('SetComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])),
1344+
('Expression', ('SetComp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])),
1345+
('Expression', ('GeneratorExp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [], 0)])),
1346+
('Expression', ('GeneratorExp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])),
1347+
('Expression', ('GeneratorExp', (1, 0), ('Tuple', (1, 1), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('List', (1, 11), [('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 14), 'b', ('Store',))], ('Store',)), ('Name', (1, 20), 'c', ('Load',)), [], 0)])),
13201348
('Expression', ('Compare', (1, 0), ('Constant', (1, 0), 1), [('Lt',), ('Lt',)], [('Constant', (1, 4), 2), ('Constant', (1, 8), 3)])),
13211349
('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Constant', (1, 2), 1), ('Constant', (1, 4), 2), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Constant', (1, 8), 3)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])),
1350+
('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('GeneratorExp', (1, 1), ('Name', (1, 2), 'a', ('Load',)), [('comprehension', ('Name', (1, 8), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Load',)), [], 0)])], [])),
13221351
('Expression', ('Constant', (1, 0), 10)),
13231352
('Expression', ('Constant', (1, 0), 'string')),
13241353
('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
@@ -1327,7 +1356,7 @@ def main():
13271356
('Expression', ('List', (1, 0), [('Constant', (1, 1), 1), ('Constant', (1, 3), 2), ('Constant', (1, 5), 3)], ('Load',))),
13281357
('Expression', ('List', (1, 0), [], ('Load',))),
13291358
('Expression', ('Tuple', (1, 0), [('Constant', (1, 0), 1), ('Constant', (1, 2), 2), ('Constant', (1, 4), 3)], ('Load',))),
1330-
('Expression', ('Tuple', (1, 1), [('Constant', (1, 1), 1), ('Constant', (1, 3), 2), ('Constant', (1, 5), 3)], ('Load',))),
1359+
('Expression', ('Tuple', (1, 0), [('Constant', (1, 1), 1), ('Constant', (1, 3), 2), ('Constant', (1, 5), 3)], ('Load',))),
13311360
('Expression', ('Tuple', (1, 0), [], ('Load',))),
13321361
('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Constant', (1, 12), 1), ('Constant', (1, 14), 2), None), ('Load',))], [])),
13331362
]

Lib/test/test_exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def check(src, lineno, offset):
204204
check('x = 0o9', 1, 6)
205205

206206
# Errors thrown by symtable.c
207-
check('x = [(yield i) for i in range(3)]', 1, 6)
207+
check('x = [(yield i) for i in range(3)]', 1, 5)
208208
check('def f():\n from _ import *', 1, 1)
209209
check('def f(x, x):\n pass', 1, 1)
210210
check('def f(x):\n nonlocal x', 2, 3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The *lineno* and *col_offset* attributes of AST nodes for list comprehensions,
2+
generator expressions and tuples are now point to the opening parenthesis or
3+
square brace. For tuples without parenthesis they point to the position of
4+
the first item.

Python/ast.c

+32-17
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
577577
static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
578578

579579
/* Note different signature for ast_for_call */
580-
static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool);
580+
static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
581+
const node *);
581582

582583
static PyObject *parsenumber(struct compiling *, const char *);
583584
static expr_ty parsestrplus(struct compiling *, const node *n);
@@ -931,6 +932,16 @@ forbidden_name(struct compiling *c, identifier name, const node *n,
931932
return 0;
932933
}
933934

935+
static expr_ty
936+
copy_location(expr_ty e, const node *n)
937+
{
938+
if (e) {
939+
e->lineno = LINENO(n);
940+
e->col_offset = n->n_col_offset;
941+
}
942+
return e;
943+
}
944+
934945
/* Set the context ctx for expr_ty e, recursively traversing e.
935946
936947
Only sets context for expr kinds that "can appear in assignment context"
@@ -1519,7 +1530,7 @@ ast_for_decorator(struct compiling *c, const node *n)
15191530
name_expr = NULL;
15201531
}
15211532
else {
1522-
d = ast_for_call(c, CHILD(n, 3), name_expr, true);
1533+
d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2));
15231534
if (!d)
15241535
return NULL;
15251536
name_expr = NULL;
@@ -2129,10 +2140,16 @@ ast_for_atom(struct compiling *c, const node *n)
21292140
return ast_for_expr(c, ch);
21302141

21312142
/* testlist_comp: test ( comp_for | (',' test)* [','] ) */
2132-
if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
2133-
return ast_for_genexp(c, ch);
2143+
if (NCH(ch) == 1) {
2144+
return ast_for_testlist(c, ch);
2145+
}
21342146

2135-
return ast_for_testlist(c, ch);
2147+
if (TYPE(CHILD(ch, 1)) == comp_for) {
2148+
return copy_location(ast_for_genexp(c, ch), n);
2149+
}
2150+
else {
2151+
return copy_location(ast_for_testlist(c, ch), n);
2152+
}
21362153
case LSQB: /* list (or list comprehension) */
21372154
ch = CHILD(n, 1);
21382155

@@ -2147,8 +2164,9 @@ ast_for_atom(struct compiling *c, const node *n)
21472164

21482165
return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
21492166
}
2150-
else
2151-
return ast_for_listcomp(c, ch);
2167+
else {
2168+
return copy_location(ast_for_listcomp(c, ch), n);
2169+
}
21522170
case LBRACE: {
21532171
/* dictorsetmaker: ( ((test ':' test | '**' test)
21542172
* (comp_for | (',' (test ':' test | '**' test))* [','])) |
@@ -2187,11 +2205,7 @@ ast_for_atom(struct compiling *c, const node *n)
21872205
/* It's a dictionary display. */
21882206
res = ast_for_dictdisplay(c, ch);
21892207
}
2190-
if (res) {
2191-
res->lineno = LINENO(n);
2192-
res->col_offset = n->n_col_offset;
2193-
}
2194-
return res;
2208+
return copy_location(res, n);
21952209
}
21962210
}
21972211
default:
@@ -2330,7 +2344,7 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
23302344
return Call(left_expr, NULL, NULL, LINENO(n),
23312345
n->n_col_offset, c->c_arena);
23322346
else
2333-
return ast_for_call(c, CHILD(n, 1), left_expr, true);
2347+
return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0));
23342348
}
23352349
else if (TYPE(CHILD(n, 0)) == DOT) {
23362350
PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
@@ -2667,7 +2681,8 @@ ast_for_expr(struct compiling *c, const node *n)
26672681
}
26682682

26692683
static expr_ty
2670-
ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
2684+
ast_for_call(struct compiling *c, const node *n, expr_ty func,
2685+
const node *maybegenbeg)
26712686
{
26722687
/*
26732688
arglist: argument (',' argument)* [',']
@@ -2690,7 +2705,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
26902705
nargs++;
26912706
else if (TYPE(CHILD(ch, 1)) == comp_for) {
26922707
nargs++;
2693-
if (!allowgen) {
2708+
if (!maybegenbeg) {
26942709
ast_error(c, ch, "invalid syntax");
26952710
return NULL;
26962711
}
@@ -2775,7 +2790,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
27752790
}
27762791
else if (TYPE(CHILD(ch, 1)) == comp_for) {
27772792
/* the lone generator expression */
2778-
e = ast_for_genexp(c, ch);
2793+
e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
27792794
if (!e)
27802795
return NULL;
27812796
asdl_seq_SET(args, nargs++, e);
@@ -3935,7 +3950,7 @@ ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
39353950
if (!dummy_name)
39363951
return NULL;
39373952
dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3938-
call = ast_for_call(c, CHILD(n, 3), dummy, false);
3953+
call = ast_for_call(c, CHILD(n, 3), dummy, NULL);
39393954
if (!call)
39403955
return NULL;
39413956
}

0 commit comments

Comments
 (0)