Skip to content

Commit 82c93ea

Browse files
[3.12] gh-119666: fix multiple class-scope comprehensions referencing __class__ (GH-120295) (#120300)
1 parent b134f47 commit 82c93ea

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

Lib/test/test_listcomps.py

+25
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ def test_references___class__(self):
168168
"""
169169
self._check_in_scopes(code, raises=NameError)
170170

171+
def test_references___class___defined(self):
172+
code = """
173+
__class__ = 2
174+
res = [__class__ for x in [1]]
175+
"""
176+
self._check_in_scopes(
177+
code, outputs={"res": [2]}, scopes=["module", "function"])
178+
self._check_in_scopes(code, raises=NameError, scopes=["class"])
179+
180+
def test_references___class___enclosing(self):
181+
code = """
182+
__class__ = 2
183+
class C:
184+
res = [__class__ for x in [1]]
185+
res = C.res
186+
"""
187+
self._check_in_scopes(code, raises=NameError)
188+
189+
def test_super_and_class_cell_in_sibling_comps(self):
190+
code = """
191+
[super for _ in [1]]
192+
[__class__ for _ in [1]]
193+
"""
194+
self._check_in_scopes(code, raises=NameError)
195+
171196
def test_inner_cell_shadows_outer(self):
172197
code = """
173198
items = [(lambda: i) for i in range(5)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a compiler crash in the case where two comprehensions in class scope both reference ``__class__``.

Python/symtable.c

+10-13
Original file line numberDiff line numberDiff line change
@@ -675,22 +675,19 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
675675
if (existing == NULL && PyErr_Occurred()) {
676676
return 0;
677677
}
678+
// __class__ is never allowed to be free through a class scope (see
679+
// drop_class_free)
680+
if (scope == FREE && ste->ste_type == ClassBlock &&
681+
_PyUnicode_EqualToASCIIString(k, "__class__")) {
682+
scope = GLOBAL_IMPLICIT;
683+
if (PySet_Discard(comp_free, k) < 0) {
684+
return 0;
685+
}
686+
remove_dunder_class = 1;
687+
}
678688
if (!existing) {
679689
// name does not exist in scope, copy from comprehension
680690
assert(scope != FREE || PySet_Contains(comp_free, k) == 1);
681-
if (scope == FREE && ste->ste_type == ClassBlock &&
682-
_PyUnicode_EqualToASCIIString(k, "__class__")) {
683-
// if __class__ is unbound in the enclosing class scope and free
684-
// in the comprehension scope, it needs special handling; just
685-
// letting it be marked as free in class scope will break due to
686-
// drop_class_free
687-
scope = GLOBAL_IMPLICIT;
688-
only_flags &= ~DEF_FREE;
689-
if (PySet_Discard(comp_free, k) < 0) {
690-
return 0;
691-
}
692-
remove_dunder_class = 1;
693-
}
694691
PyObject *v_flags = PyLong_FromLong(only_flags);
695692
if (v_flags == NULL) {
696693
return 0;

0 commit comments

Comments
 (0)