Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e4b0e1

Browse files
committed
Merged revisions 74464 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r74464 | benjamin.peterson | 2009-08-15 17:59:21 -0500 (Sat, 15 Aug 2009) | 4 lines better col_offsets for "for" statements with tuple unpacking #6704 Patch from Frank Wierzbicki. ........
1 parent 8719ad5 commit 2e4b0e1

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

Lib/test/test_ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def to_tuple(t):
6060
"break",
6161
# Continue
6262
"continue",
63+
# for statements with naked tuples (see http://bugs.python.org/issue6704)
64+
"for a,b in c: pass",
65+
"[(a,b) for a,b in c]",
66+
"((a,b) for a,b in c)",
6367
]
6468

6569
# These are compiled through "single"
@@ -321,6 +325,9 @@ def main():
321325
('Module', [('Pass', (1, 0))]),
322326
('Module', [('Break', (1, 0))]),
323327
('Module', [('Continue', (1, 0))]),
328+
('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))], [])]),
329+
('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',)), [])]))]),
330+
('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',)), [])]))]),
324331
]
325332
single_results = [
326333
('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
@@ -805,6 +805,7 @@ Collin Winter
805805
Dik Winter
806806
Blake Winton
807807
Jean-Claude Wippler
808+
Frank Wierzbicki
808809
Lars Wirzenius
809810
Chris Withers
810811
Stefan Witzel

Python/ast.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ ast_for_comprehension(struct compiling *c, const node *n)
11921192
for (i = 0; i < n_fors; i++) {
11931193
comprehension_ty comp;
11941194
asdl_seq *t;
1195-
expr_ty expression;
1195+
expr_ty expression, first;
11961196
node *for_ch;
11971197

11981198
REQ(n, comp_for);
@@ -1207,14 +1207,13 @@ ast_for_comprehension(struct compiling *c, const node *n)
12071207

12081208
/* Check the # of children rather than the length of t, since
12091209
(x for x, in ...) has 1 element in t, but still requires a Tuple. */
1210+
first = (expr_ty)asdl_seq_GET(t, 0);
12101211
if (NCH(for_ch) == 1)
1211-
comp = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1212-
NULL, c->c_arena);
1212+
comp = comprehension(first, expression, NULL, c->c_arena);
12131213
else
1214-
comp = comprehension(Tuple(t, Store, LINENO(n), n->n_col_offset,
1215-
c->c_arena),
1216-
expression, NULL, c->c_arena);
1217-
1214+
comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1215+
c->c_arena),
1216+
expression, NULL, c->c_arena);
12181217
if (!comp)
12191218
return NULL;
12201219

@@ -1294,7 +1293,6 @@ ast_for_dictcomp(struct compiling *c, const node *n)
12941293
key = ast_for_expr(c, CHILD(n, 0));
12951294
if (!key)
12961295
return NULL;
1297-
12981296
value = ast_for_expr(c, CHILD(n, 2));
12991297
if (!value)
13001298
return NULL;
@@ -2802,7 +2800,7 @@ ast_for_for_stmt(struct compiling *c, const node *n)
28022800
{
28032801
asdl_seq *_target, *seq = NULL, *suite_seq;
28042802
expr_ty expression;
2805-
expr_ty target;
2803+
expr_ty target, first;
28062804
const node *node_target;
28072805
/* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
28082806
REQ(n, for_stmt);
@@ -2819,10 +2817,11 @@ ast_for_for_stmt(struct compiling *c, const node *n)
28192817
return NULL;
28202818
/* Check the # of children rather than the length of _target, since
28212819
for x, in ... has 1 element in _target, but still requires a Tuple. */
2820+
first = (expr_ty)asdl_seq_GET(_target, 0);
28222821
if (NCH(node_target) == 1)
2823-
target = (expr_ty)asdl_seq_GET(_target, 0);
2822+
target = first;
28242823
else
2825-
target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
2824+
target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
28262825

28272826
expression = ast_for_testlist(c, CHILD(n, 3));
28282827
if (!expression)

0 commit comments

Comments
 (0)