summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-10 04:37:28 (GMT)
committerGitHub <noreply@github.com>2024-06-10 04:37:28 (GMT)
commit03cd44759f8e5faaa154628e196860255d87bddd (patch)
tree1722f2ad13b2a1305dd6e2abd78571d4b27e7f4a /Python
parentc15f94d6fbc960790db34c94d49716658ccf6348 (diff)
downloadcpython-03cd44759f8e5faaa154628e196860255d87bddd.zip
cpython-03cd44759f8e5faaa154628e196860255d87bddd.tar.gz
cpython-03cd44759f8e5faaa154628e196860255d87bddd.tar.bz2
[3.13] gh-119666: fix multiple class-scope comprehensions referencing __class__ (GH-120295) (#120299)
Diffstat (limited to 'Python')
-rw-r--r--Python/symtable.c23
1 files changed, 10 insertions, 13 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index d8240cd..35c1e4a 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -780,22 +780,19 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
if (existing == NULL && PyErr_Occurred()) {
return 0;
}
+ // __class__ is never allowed to be free through a class scope (see
+ // drop_class_free)
+ if (scope == FREE && ste->ste_type == ClassBlock &&
+ _PyUnicode_EqualToASCIIString(k, "__class__")) {
+ scope = GLOBAL_IMPLICIT;
+ if (PySet_Discard(comp_free, k) < 0) {
+ return 0;
+ }
+ remove_dunder_class = 1;
+ }
if (!existing) {
// name does not exist in scope, copy from comprehension
assert(scope != FREE || PySet_Contains(comp_free, k) == 1);
- if (scope == FREE && ste->ste_type == ClassBlock &&
- _PyUnicode_EqualToASCIIString(k, "__class__")) {
- // if __class__ is unbound in the enclosing class scope and free
- // in the comprehension scope, it needs special handling; just
- // letting it be marked as free in class scope will break due to
- // drop_class_free
- scope = GLOBAL_IMPLICIT;
- only_flags &= ~DEF_FREE;
- if (PySet_Discard(comp_free, k) < 0) {
- return 0;
- }
- remove_dunder_class = 1;
- }
PyObject *v_flags = PyLong_FromLong(only_flags);
if (v_flags == NULL) {
return 0;