Skip to content

Commit c0ba828

Browse files
committed
better col_offsets for "for" statements with tuple unpacking #6704
Patch from Frank Wierzbicki.
1 parent 8cabfa3 commit c0ba828

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

Lib/test/test_ast.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def to_tuple(t):
6464
"break",
6565
# Continue
6666
"continue",
67+
# for statements with naked tuples (see http://bugs.python.org/issue6704)
68+
"for a,b in c: pass",
69+
"[(a,b) for a,b in c]",
70+
"((a,b) for a,b in c)",
6771
]
6872

6973
# These are compiled through "single"
@@ -301,7 +305,7 @@ def main():
301305
print kind+"_results = ["
302306
for s in statements:
303307
print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
304-
print "]"
308+
print "]"
305309
print "main()"
306310
raise SystemExit
307311
test_main()
@@ -330,6 +334,9 @@ def main():
330334
('Module', [('Pass', (1, 0))]),
331335
('Module', [('Break', (1, 0))]),
332336
('Module', [('Continue', (1, 0))]),
337+
('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))], [])]),
338+
('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',)), [])]))]),
339+
('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',)), [])]))]),
333340
]
334341
single_results = [
335342
('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ Collin Winter
794794
Dik Winter
795795
Blake Winton
796796
Jean-Claude Wippler
797+
Frank Wierzbicki
797798
Lars Wirzenius
798799
Chris Withers
799800
Stefan Witzel

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #6704: Improve the col_offset in AST for "for" statements with
16+
a target of tuple unpacking.
17+
1518
- Issue #6707: dir() on an uninitialized module caused a crash.
1619

1720
- Issue #6540: Fixed crash for bytearray.translate() with invalid parameters.

Python/ast.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ ast_for_listcomp(struct compiling *c, const node *n)
10471047
list_if: 'if' test [list_iter]
10481048
testlist_safe: test [(',' test)+ [',']]
10491049
*/
1050-
expr_ty elt;
1050+
expr_ty elt, first;
10511051
asdl_seq *listcomps;
10521052
int i, n_fors;
10531053
node *ch;
@@ -1087,11 +1087,11 @@ ast_for_listcomp(struct compiling *c, const node *n)
10871087
/* Check the # of children rather than the length of t, since
10881088
[x for x, in ... ] has 1 element in t, but still requires a Tuple.
10891089
*/
1090+
first = (expr_ty)asdl_seq_GET(t, 0);
10901091
if (NCH(for_ch) == 1)
1091-
lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1092-
c->c_arena);
1092+
lc = comprehension(first, expression, NULL, c->c_arena);
10931093
else
1094-
lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1094+
lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
10951095
c->c_arena),
10961096
expression, NULL, c->c_arena);
10971097
if (!lc)
@@ -1226,7 +1226,7 @@ ast_for_genexp(struct compiling *c, const node *n)
12261226
for (i = 0; i < n_fors; i++) {
12271227
comprehension_ty ge;
12281228
asdl_seq *t;
1229-
expr_ty expression;
1229+
expr_ty expression, first;
12301230
node *for_ch;
12311231

12321232
REQ(ch, gen_for);
@@ -1241,11 +1241,11 @@ ast_for_genexp(struct compiling *c, const node *n)
12411241

12421242
/* Check the # of children rather than the length of t, since
12431243
(x for x, in ...) has 1 element in t, but still requires a Tuple. */
1244+
first = (expr_ty)asdl_seq_GET(t, 0);
12441245
if (NCH(for_ch) == 1)
1245-
ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1246-
NULL, c->c_arena);
1246+
ge = comprehension(first, expression, NULL, c->c_arena);
12471247
else
1248-
ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1248+
ge = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
12491249
c->c_arena),
12501250
expression, NULL, c->c_arena);
12511251

@@ -2844,7 +2844,7 @@ ast_for_for_stmt(struct compiling *c, const node *n)
28442844
{
28452845
asdl_seq *_target, *seq = NULL, *suite_seq;
28462846
expr_ty expression;
2847-
expr_ty target;
2847+
expr_ty target, first;
28482848
const node *node_target;
28492849
/* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
28502850
REQ(n, for_stmt);
@@ -2861,10 +2861,11 @@ ast_for_for_stmt(struct compiling *c, const node *n)
28612861
return NULL;
28622862
/* Check the # of children rather than the length of _target, since
28632863
for x, in ... has 1 element in _target, but still requires a Tuple. */
2864+
first = (expr_ty)asdl_seq_GET(_target, 0);
28642865
if (NCH(node_target) == 1)
2865-
target = (expr_ty)asdl_seq_GET(_target, 0);
2866+
target = first;
28662867
else
2867-
target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
2868+
target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
28682869

28692870
expression = ast_for_testlist(c, CHILD(n, 3));
28702871
if (!expression)

0 commit comments

Comments
 (0)