Index: Python/ast.c =================================================================== --- Python/ast.c (Revision 51628) +++ Python/ast.c (Arbeitskopie) @@ -983,17 +983,21 @@ comprehension_ty lc; asdl_seq *t; expr_ty expression; + const node *node_target; REQ(ch, list_for); - t = ast_for_exprlist(c, CHILD(ch, 1), Store); + node_target = CHILD(ch, 1); + t = ast_for_exprlist(c, node_target, Store); if (!t) return NULL; expression = ast_for_testlist(c, CHILD(ch, 3)); if (!expression) return NULL; - if (asdl_seq_LEN(t) == 1) + /* Check the # of children rather than the length of t, since + for x, in ... has 1 element in t, but still requires a Tuple. */ + if (NCH(node_target) == 1) lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL, c->c_arena); else @@ -1129,17 +1133,21 @@ comprehension_ty ge; asdl_seq *t; expr_ty expression; + const node *node_target; REQ(ch, gen_for); - t = ast_for_exprlist(c, CHILD(ch, 1), Store); + node_target = CHILD(ch, 1); + t = ast_for_exprlist(c, node_target, Store); if (!t) return NULL; expression = ast_for_expr(c, CHILD(ch, 3)); if (!expression) return NULL; - if (asdl_seq_LEN(t) == 1) + /* Check the # of children rather than the length of t, since + for x, in ... has 1 element in t, but still requires a Tuple. */ + if (NCH(node_target) == 1) ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL, c->c_arena); else Index: Python/import.c =================================================================== --- Python/import.c (Revision 51628) +++ Python/import.c (Arbeitskopie) @@ -64,9 +64,10 @@ Python 2.5b3: 62111 (fix wrong code: x += yield) Python 2.5c1: 62121 (fix wrong lnotab with for loops and storing constants that should have been removed) + Python 2.5c2: 62131 (fix wrong code: [x for x, in ...]) . */ -#define MAGIC (62121 | ((long)'\r'<<16) | ((long)'\n'<<24)) +#define MAGIC (62131 | ((long)'\r'<<16) | ((long)'\n'<<24)) /* Magic word as global; note that _PyImport_Init() can change the value of this global to accommodate for alterations of how the Index: Misc/NEWS =================================================================== --- Misc/NEWS (Revision 51628) +++ Misc/NEWS (Arbeitskopie) @@ -12,6 +12,9 @@ Core and builtins ----------------- +- Bug #1520864 (again): fix wrong code generated for list comprehensions + and generator expressions in the form [x for x, in ...]. + - Patch #1542451: disallow continue anywhere under a finally. Index: Lib/test/test_grammar.py =================================================================== --- Lib/test/test_grammar.py (Revision 51628) +++ Lib/test/test_grammar.py (Arbeitskopie) @@ -825,6 +825,10 @@ verify([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) verify((x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) +# Test 1-tuple unpacking +verify([x for x, in [(1,), (2,), (3,)]], [1, 2, 3]) +verify((x for x, in [(1,), (2,), (3,)]), [1, 2, 3]) + # Test ifelse expressions in various cases def _checkeval(msg, ret): "helper to check that evaluation of expressions is done correctly"