@@ -806,6 +806,8 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
806806 PyObject * k , * v ;
807807 Py_ssize_t pos = 0 ;
808808 int remove_dunder_class = 0 ;
809+ int remove_dunder_classdict = 0 ;
810+ int remove_dunder_cond_annotations = 0 ;
809811
810812 while (PyDict_Next (comp -> ste_symbols , & pos , & k , & v )) {
811813 // skip comprehension parameter
@@ -828,15 +830,27 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
828830 if (existing == NULL && PyErr_Occurred ()) {
829831 return 0 ;
830832 }
831- // __class__ is never allowed to be free through a class scope (see
833+ // __class__, __classdict__ and __conditional_annotations__ are
834+ // never allowed to be free through a class scope (see
832835 // drop_class_free)
833836 if (scope == FREE && ste -> ste_type == ClassBlock &&
834- _PyUnicode_EqualToASCIIString (k , "__class__" )) {
837+ (_PyUnicode_EqualToASCIIString (k , "__class__" ) ||
838+ _PyUnicode_EqualToASCIIString (k , "__classdict__" ) ||
839+ _PyUnicode_EqualToASCIIString (k , "__conditional_annotations__" ))) {
835840 scope = GLOBAL_IMPLICIT ;
836841 if (PySet_Discard (comp_free , k ) < 0 ) {
837842 return 0 ;
838843 }
839- remove_dunder_class = 1 ;
844+
845+ if (_PyUnicode_EqualToASCIIString (k , "__class__" )) {
846+ remove_dunder_class = 1 ;
847+ }
848+ else if (_PyUnicode_EqualToASCIIString (k , "__conditional_annotations__" )) {
849+ remove_dunder_cond_annotations = 1 ;
850+ }
851+ else {
852+ remove_dunder_classdict = 1 ;
853+ }
840854 }
841855 if (!existing ) {
842856 // name does not exist in scope, copy from comprehension
@@ -876,6 +890,12 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
876890 if (remove_dunder_class && PyDict_DelItemString (comp -> ste_symbols , "__class__" ) < 0 ) {
877891 return 0 ;
878892 }
893+ if (remove_dunder_classdict && PyDict_DelItemString (comp -> ste_symbols , "__classdict__" ) < 0 ) {
894+ return 0 ;
895+ }
896+ if (remove_dunder_cond_annotations && PyDict_DelItemString (comp -> ste_symbols , "__conditional_annotations__" ) < 0 ) {
897+ return 0 ;
898+ }
879899 return 1 ;
880900}
881901
0 commit comments