Skip to content

Commit 67f50ba

Browse files
committed
fix case where iter var is free in outer scope
1 parent 463c740 commit 67f50ba

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Lib/test/test_listcomps.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@
191191
>>> test_func([1])
192192
[1]
193193
194+
>>> def test_func():
195+
... x = 1
196+
... def g():
197+
... [x for x in range(3)]
198+
... return x
199+
... g()
200+
... return x
201+
>>> test_func()
202+
1
203+
194204
"""
195205

196206

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5237,7 +5237,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
52375237
assert(PyLong_Check(outv));
52385238
long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK;
52395239
int outer_global = (outsc == GLOBAL_IMPLICIT || outsc == GLOBAL_EXPLICIT);
5240-
if (outer_global || (outsc == CELL && scope == LOCAL)) {
5240+
if (outer_global || ((outsc == CELL || outsc == FREE) && scope == LOCAL)) {
52415241
// If a name is global in the outer scope but local in the
52425242
// comprehension scope, we need to keep it global in outer scope
52435243
// but ensure the comprehension writes to the local, not the

0 commit comments

Comments
 (0)