diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py index 45644d6c0927827..dfcfdc73f9dc84e 100644 --- a/Lib/test/test_listcomps.py +++ b/Lib/test/test_listcomps.py @@ -114,6 +114,7 @@ def get_output(moddict, name): def _f(): {code} return locals() + import dis; dis.dis(_f) # TODO _out = _f() """).format(code=textwrap.indent(code, " ")) def get_output(moddict, name): @@ -750,6 +751,18 @@ def iter_raises(): self.assertEqual(f.line[f.colno - indent : f.end_colno - indent], expected) + def test_freevar_through_scope_containing_comprehension(self): + code = """ + x = 1 + def f(): + [x for x in [1]] + 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): diff --git a/Python/symtable.c b/Python/symtable.c index 8bc9db6d7d68116..98ea7c59b4c66fa 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -1220,7 +1220,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 = + int inline_comp = true && entry->ste_comprehension && !entry->ste_generator && !ste->ste_can_see_class_scope;