mirror of
https://github.com/python/cpython.git
synced 2026-04-13 23:31:02 +00:00
[3.13] gh-145701: Fix __classdict__ & __conditional_annotations__ in class-scope inlined comprehensions (GH-145702) (#145711)
* gh-145701: Fix `__classdict__` & `__conditional_annotations__` in class-scope inlined comprehensions (GH-145702) (cherry picked from commit63eaaf9599) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> * Add `:oss-fuzz:` macro support Backports part of255e79fa95. --------- Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
parent
4800aa3076
commit
321daaba4e
4 changed files with 39 additions and 3 deletions
|
|
@ -763,6 +763,8 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
|
|||
PyObject *k, *v;
|
||||
Py_ssize_t pos = 0;
|
||||
int remove_dunder_class = 0;
|
||||
int remove_dunder_classdict = 0;
|
||||
int remove_dunder_cond_annotations = 0;
|
||||
|
||||
while (PyDict_Next(comp->ste_symbols, &pos, &k, &v)) {
|
||||
// skip comprehension parameter
|
||||
|
|
@ -782,15 +784,27 @@ 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
|
||||
// __class__, __classdict__ and __conditional_annotations__ are
|
||||
// never allowed to be free through a class scope (see
|
||||
// drop_class_free)
|
||||
if (scope == FREE && ste->ste_type == ClassBlock &&
|
||||
_PyUnicode_EqualToASCIIString(k, "__class__")) {
|
||||
(_PyUnicode_EqualToASCIIString(k, "__class__") ||
|
||||
_PyUnicode_EqualToASCIIString(k, "__classdict__") ||
|
||||
_PyUnicode_EqualToASCIIString(k, "__conditional_annotations__"))) {
|
||||
scope = GLOBAL_IMPLICIT;
|
||||
if (PySet_Discard(comp_free, k) < 0) {
|
||||
return 0;
|
||||
}
|
||||
remove_dunder_class = 1;
|
||||
|
||||
if (_PyUnicode_EqualToASCIIString(k, "__class__")) {
|
||||
remove_dunder_class = 1;
|
||||
}
|
||||
else if (_PyUnicode_EqualToASCIIString(k, "__conditional_annotations__")) {
|
||||
remove_dunder_cond_annotations = 1;
|
||||
}
|
||||
else {
|
||||
remove_dunder_classdict = 1;
|
||||
}
|
||||
}
|
||||
if (!existing) {
|
||||
// name does not exist in scope, copy from comprehension
|
||||
|
|
@ -823,6 +837,12 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
|
|||
if (remove_dunder_class && PyDict_DelItemString(comp->ste_symbols, "__class__") < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (remove_dunder_classdict && PyDict_DelItemString(comp->ste_symbols, "__classdict__") < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (remove_dunder_cond_annotations && PyDict_DelItemString(comp->ste_symbols, "__conditional_annotations__") < 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue