Skip to content

bpo-37757: Disallow PEP 572 cases that expose implementation details #15131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prev Previous commit
Next Next commit
Close nested comprehension and lambda expression loopholes
  • Loading branch information
ncoghlan committed Aug 5, 2019
commit c0f2b5bfa7f3b0318493a5d663ac59cf3f705df0
5 changes: 3 additions & 2 deletions Lib/test/test_named_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ def test_named_expression_invalid_rebinding_comprehension_iteration_variable(sel
("Unreachable reuse", "[False or (i:=0) for i in range(5)]"),
("Unreachable nested reuse",
"[(i, j) for i in range(5) for j in range(5) if True or (i:=10)]"),
("Nested comprehension condition", "[i for i in [j for j in range(5) if (j := True)]]"),
("Nested comprehension body", "[i for i in [(j := True) for j in range(5)]]"),
]
for case, code in cases:
with self.subTest(case=case):
Expand All @@ -148,8 +146,11 @@ def test_named_expression_invalid_comprehension_iterator_expression(self):
("Top level", "[i for i in (i := range(5))]"),
("Inside container", "[i for i in (2, 3, i := range(5))]"),
("Different name", "[i for i in (j := range(5))]"),
("Lambda expression", "[i for i in (lambda:(j := range(5)))()]"),
("Inner loop", "[i for i in range(5) for j in (i := range(5))]"),
("Nested comprehension", "[i for i in [j for j in (k := range(5))]]"),
("Nested comprehension condition", "[i for i in [j for j in range(5) if (j := True)]]"),
("Nested comprehension body", "[i for i in [(j := True) for j in range(5)]]"),
]
for case, code in cases:
with self.subTest(case=case):
Expand Down
7 changes: 7 additions & 0 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,13 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
return 0;
}
prev = st->st_cur;
/* bpo-37757: For now, disallow *all* assignment expressions in the
* outermost iterator expression of a comprehension, even those inside
* a nested comprehension or a lambda expression.
*/
if (prev) {
ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
}
/* The entry is owned by the stack. Borrow it for st_cur. */
Py_DECREF(ste);
st->st_cur = ste;
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy