@@ -211,6 +211,8 @@ uuid_getpid(void) {
211211#endif
212212}
213213
214+ static struct PyModuleDef uuidmodule ;
215+
214216static inline uuid_state *
215217get_uuid_state (PyObject * mod )
216218{
@@ -222,7 +224,9 @@ get_uuid_state(PyObject *mod)
222224static inline uuid_state *
223225get_uuid_state_by_cls (PyTypeObject * cls )
224226{
225- uuid_state * state = (uuid_state * )PyType_GetModuleState (cls );
227+ PyObject * module = PyType_GetModuleByDef (cls , & uuidmodule );
228+ assert (module != NULL );
229+ uuid_state * state = get_uuid_state (module );
226230 assert (state != NULL );
227231 return state ;
228232}
@@ -1019,18 +1023,25 @@ Uuid_dealloc(PyObject *obj)
10191023 }
10201024 Py_CLEAR (uuid -> is_safe );
10211025
1022- int added_to_freelist = 0 ;
1026+ int skip_dealloc = 0 ;
1027+ if (type != state -> UuidType ) {
1028+ // We only apply the freelist optimization to the known C type,
1029+ // not any subclasses of it.
1030+ goto dealloc ;
1031+ }
1032+
10231033 Py_BEGIN_CRITICAL_SECTION (type );
10241034 if (state -> freelist_size < MAX_FREE_LIST_SIZE ) {
10251035 uuidobject * head = state -> freelist ;
10261036 state -> freelist = uuid ;
10271037 uuid -> weakreflist = (PyObject * )head ;
10281038 state -> freelist_size ++ ;
1029- added_to_freelist = 1 ;
1039+ skip_dealloc = 1 ;
10301040 }
10311041 Py_END_CRITICAL_SECTION ();
10321042
1033- if (!added_to_freelist ) {
1043+ dealloc :
1044+ if (!skip_dealloc ) {
10341045 type -> tp_free (uuid );
10351046 // UUID is a heap allocated type so we have to decref the type ref
10361047 Py_DECREF (type );
0 commit comments