Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,18 @@ def g():
"""
self._check_in_scopes(code, {"x": 1, "y": 1}, scopes=["module", "function"])

def test_freevar_through_scope_containing_comprehension_with_cell(self):
code = """
x = 1
def f():
[lambda: x for x in [1]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[lambda: x for x in [1]]
[lambda: x for x in [2]]

As discussed:

  • Use a different value (the test ran correctly but used the wrong 1)
  • Should change it to execute the lambda so we ensure the value of x inside the lambda is right

def g():
return x
return g()
y = f()
"""
self._check_in_scopes(code, {"x": 1, "y": 1}, scopes=["function"])

__test__ = {'doctests' : doctests}

def load_tests(loader, tests, pattern):
Expand Down
28 changes: 15 additions & 13 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
return 0;
}
SET_SCOPE(scopes, k, scope);
if (scope == LOCAL) {
if (scope == LOCAL || scope == CELL) {
if (PySet_Add(inlined_locals, k) < 0) {
return 0;
}
Expand Down Expand Up @@ -906,7 +906,8 @@ analyze_cells(PyObject *scopes, PyObject *free, PyObject *inlined_cells, PyObjec
if (scope == -1 && PyErr_Occurred()) {
goto error;
}
if (scope != LOCAL)

if (scope != LOCAL && scope != CELL)
continue;
int contains = PySet_Contains(free, name);
if (contains < 0) {
Expand All @@ -920,18 +921,19 @@ analyze_cells(PyObject *scopes, PyObject *free, PyObject *inlined_cells, PyObjec
if (!contains) {
continue;
}
}
/* If the name is defined by an inlined comprehension, it must be
* preserved as free in the outer scope. */
contains = PySet_Contains(inlined_locals, name);
if (contains < 0) {
goto error;
}
if (contains) {
if (PyDict_SetItem(scopes, name, v_free) < 0) {
} else {
/* If a free name is defined only by an inlined comprehension, it must be
* preserved as free in the outer scope. */
contains = PySet_Contains(inlined_locals, name);
if (contains < 0) {
goto error;
}
continue;
if (contains) {
if (PyDict_SetItem(scopes, name, v_free) < 0) {
goto error;
}
continue;
}
}

/* Replace LOCAL with CELL for this name, and remove
Expand Down Expand Up @@ -1248,7 +1250,7 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,

// we inline all non-generator-expression comprehensions,
// except those in annotation scopes that are nested in classes
int inline_comp = true &&
int inline_comp =
entry->ste_comprehension &&
!entry->ste_generator &&
!ste->ste_can_see_class_scope;
Expand Down
Loading