From 9f814a4dc1018955be751b7797bfe20fdb706dc2 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 18 May 2023 07:28:42 -0700 Subject: [PATCH] add another test and propagate DEF_COMP_CELL --- Lib/test/test_listcomps.py | 15 +++++++++++++++ Python/symtable.c | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py index 7f06b2b7e0ea34..601f4f1a5b9833 100644 --- a/Lib/test/test_listcomps.py +++ b/Lib/test/test_listcomps.py @@ -391,6 +391,21 @@ def f(): """ self._check_in_scopes( code, {"x": 2}, ns={"b": 2}, scopes=["function", "module"]) + # inside a class, the `a = 1` assignment is not visible + self._check_in_scopes(code, raises=NameError, scopes=["class"]) + + def test_cell_in_nested_comprehension(self): + code = """ + a = 1 + def f(): + [[lambda: b for b in c] + [b] for c in [[a]]] + return b + x = f() + """ + self._check_in_scopes( + code, {"x": 2}, ns={"b": 2}, scopes=["function", "module"]) + # inside a class, the `a = 1` assignment is not visible + self._check_in_scopes(code, raises=NameError, scopes=["class"]) def test_name_error_in_class_scope(self): code = """ diff --git a/Python/symtable.c b/Python/symtable.c index 217529f26852e4..bd523f0cdd7409 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -645,7 +645,7 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp, } int scope = (comp_flags >> SCOPE_OFFSET) & SCOPE_MASK; int only_flags = comp_flags & ((1 << SCOPE_OFFSET) - 1); - if (scope == CELL) { + if (scope == CELL || only_flags & DEF_COMP_CELL) { if (PySet_Add(inlined_cells, k) < 0) { return 0; }