@@ -763,6 +763,8 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
763763 PyObject * k , * v ;
764764 Py_ssize_t pos = 0 ;
765765 int remove_dunder_class = 0 ;
766+ int remove_dunder_classdict = 0 ;
767+ int remove_dunder_cond_annotations = 0 ;
766768
767769 while (PyDict_Next (comp -> ste_symbols , & pos , & k , & v )) {
768770 // skip comprehension parameter
@@ -782,15 +784,27 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
782784 if (existing == NULL && PyErr_Occurred ()) {
783785 return 0 ;
784786 }
785- // __class__ is never allowed to be free through a class scope (see
787+ // __class__, __classdict__ and __conditional_annotations__ are
788+ // never allowed to be free through a class scope (see
786789 // drop_class_free)
787790 if (scope == FREE && ste -> ste_type == ClassBlock &&
788- _PyUnicode_EqualToASCIIString (k , "__class__" )) {
791+ (_PyUnicode_EqualToASCIIString (k , "__class__" ) ||
792+ _PyUnicode_EqualToASCIIString (k , "__classdict__" ) ||
793+ _PyUnicode_EqualToASCIIString (k , "__conditional_annotations__" ))) {
789794 scope = GLOBAL_IMPLICIT ;
790795 if (PySet_Discard (comp_free , k ) < 0 ) {
791796 return 0 ;
792797 }
793- remove_dunder_class = 1 ;
798+
799+ if (_PyUnicode_EqualToASCIIString (k , "__class__" )) {
800+ remove_dunder_class = 1 ;
801+ }
802+ else if (_PyUnicode_EqualToASCIIString (k , "__conditional_annotations__" )) {
803+ remove_dunder_cond_annotations = 1 ;
804+ }
805+ else {
806+ remove_dunder_classdict = 1 ;
807+ }
794808 }
795809 if (!existing ) {
796810 // name does not exist in scope, copy from comprehension
@@ -823,6 +837,12 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
823837 if (remove_dunder_class && PyDict_DelItemString (comp -> ste_symbols , "__class__" ) < 0 ) {
824838 return 0 ;
825839 }
840+ if (remove_dunder_classdict && PyDict_DelItemString (comp -> ste_symbols , "__classdict__" ) < 0 ) {
841+ return 0 ;
842+ }
843+ if (remove_dunder_cond_annotations && PyDict_DelItemString (comp -> ste_symbols , "__conditional_annotations__" ) < 0 ) {
844+ return 0 ;
845+ }
826846 return 1 ;
827847}
828848
0 commit comments