Skip to content

Commit

Permalink
fix case where iter var is free in outer scope
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Feb 28, 2023
1 parent 463c740 commit 67f50ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@
>>> test_func([1])
[1]
>>> def test_func():
... x = 1
... def g():
... [x for x in range(3)]
... return x
... g()
... return x
>>> test_func()
1
"""


Expand Down
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5237,7 +5237,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
assert(PyLong_Check(outv));
long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK;
int outer_global = (outsc == GLOBAL_IMPLICIT || outsc == GLOBAL_EXPLICIT);
if (outer_global || (outsc == CELL && scope == LOCAL)) {
if (outer_global || ((outsc == CELL || outsc == FREE) && scope == LOCAL)) {
// If a name is global in the outer scope but local in the
// comprehension scope, we need to keep it global in outer scope
// but ensure the comprehension writes to the local, not the
Expand Down

0 comments on commit 67f50ba

Please sign in to comment.