@@ -314,15 +314,6 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
314314 }
315315}
316316
317- static PyTypeObject *
318- managed_static_type_get_def (PyTypeObject * self , int isbuiltin )
319- {
320- size_t index = managed_static_type_index_get (self );
321- size_t full_index = isbuiltin
322- ? index
323- : index + _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES ;
324- return & _PyRuntime .types .managed_static .types [full_index ].def ;
325- }
326317
327318
328319PyObject *
@@ -5927,6 +5918,7 @@ fini_static_type(PyInterpreterState *interp, PyTypeObject *type,
59275918
59285919 _PyStaticType_ClearWeakRefs (interp , type );
59295920 managed_static_type_state_clear (interp , type , isbuiltin , final );
5921+ /* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
59305922}
59315923
59325924void
@@ -7939,7 +7931,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
79397931 return 0 ;
79407932}
79417933
7942- static int add_operators (PyTypeObject * , PyTypeObject * );
7934+ static int add_operators (PyTypeObject * type );
79437935static int add_tp_new_wrapper (PyTypeObject * type );
79447936
79457937#define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING)
@@ -8104,10 +8096,10 @@ type_dict_set_doc(PyTypeObject *type)
81048096
81058097
81068098static int
8107- type_ready_fill_dict (PyTypeObject * type , PyTypeObject * def )
8099+ type_ready_fill_dict (PyTypeObject * type )
81088100{
81098101 /* Add type-specific descriptors to tp_dict */
8110- if (add_operators (type , def ) < 0 ) {
8102+ if (add_operators (type ) < 0 ) {
81118103 return -1 ;
81128104 }
81138105 if (type_add_methods (type ) < 0 ) {
@@ -8426,7 +8418,7 @@ type_ready_post_checks(PyTypeObject *type)
84268418
84278419
84288420static int
8429- type_ready (PyTypeObject * type , PyTypeObject * def , int initial )
8421+ type_ready (PyTypeObject * type , int initial )
84308422{
84318423 ASSERT_TYPE_LOCK_HELD ();
84328424
@@ -8465,7 +8457,7 @@ type_ready(PyTypeObject *type, PyTypeObject *def, int initial)
84658457 if (type_ready_set_new (type , initial ) < 0 ) {
84668458 goto error ;
84678459 }
8468- if (type_ready_fill_dict (type , def ) < 0 ) {
8460+ if (type_ready_fill_dict (type ) < 0 ) {
84698461 goto error ;
84708462 }
84718463 if (initial ) {
@@ -8522,7 +8514,7 @@ PyType_Ready(PyTypeObject *type)
85228514 int res ;
85238515 BEGIN_TYPE_LOCK ();
85248516 if (!(type -> tp_flags & Py_TPFLAGS_READY )) {
8525- res = type_ready (type , NULL , 1 );
8517+ res = type_ready (type , 1 );
85268518 } else {
85278519 res = 0 ;
85288520 assert (_PyType_CheckConsistency (type ));
@@ -8558,20 +8550,14 @@ init_static_type(PyInterpreterState *interp, PyTypeObject *self,
85588550
85598551 managed_static_type_state_init (interp , self , isbuiltin , initial );
85608552
8561- PyTypeObject * def = managed_static_type_get_def (self , isbuiltin );
8562- if (initial ) {
8563- memcpy (def , self , sizeof (PyTypeObject ));
8564- }
8565-
85668553 int res ;
85678554 BEGIN_TYPE_LOCK ();
8568- res = type_ready (self , def , initial );
8555+ res = type_ready (self , initial );
85698556 END_TYPE_LOCK ();
85708557 if (res < 0 ) {
85718558 _PyStaticType_ClearWeakRefs (interp , self );
85728559 managed_static_type_state_clear (interp , self , isbuiltin , initial );
85738560 }
8574-
85758561 return res ;
85768562}
85778563
@@ -11182,6 +11168,26 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1118211168 return 0 ;
1118311169}
1118411170
11171+ static int
11172+ slot_inherited (PyTypeObject * type , pytype_slotdef * slotdef , void * * slot )
11173+ {
11174+ void * * slot_base = slotptr (type -> tp_base , slotdef -> offset );
11175+ if (slot_base == NULL || * slot != * slot_base ) {
11176+ return 0 ;
11177+ }
11178+
11179+ /* Some slots are inherited in pairs. */
11180+ if (slot == (void * )& type -> tp_hash ) {
11181+ return (type -> tp_richcompare == type -> tp_base -> tp_richcompare );
11182+ }
11183+ else if (slot == (void * )& type -> tp_richcompare ) {
11184+ return (type -> tp_hash == type -> tp_base -> tp_hash );
11185+ }
11186+
11187+ /* It must be inherited (see type_ready_inherit()). */
11188+ return 1 ;
11189+ }
11190+
1118511191/* This function is called by PyType_Ready() to populate the type's
1118611192 dictionary with method descriptors for function slots. For each
1118711193 function slot (like tp_repr) that's defined in the type, one or more
@@ -11213,24 +11219,26 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1121311219 infinite recursion here.) */
1121411220
1121511221static int
11216- add_operators (PyTypeObject * type , PyTypeObject * def )
11222+ add_operators (PyTypeObject * type )
1121711223{
1121811224 PyObject * dict = lookup_tp_dict (type );
1121911225 pytype_slotdef * p ;
1122011226 PyObject * descr ;
1122111227 void * * ptr ;
1122211228
11223- assert (def == NULL || (type -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN ));
11224- if (def == NULL ) {
11225- def = type ;
11226- }
11227-
1122811229 for (p = slotdefs ; p -> name ; p ++ ) {
1122911230 if (p -> wrapper == NULL )
1123011231 continue ;
11231- ptr = slotptr (def , p -> offset );
11232+ ptr = slotptr (type , p -> offset );
1123211233 if (!ptr || !* ptr )
1123311234 continue ;
11235+ /* Also ignore when the type slot has been inherited. */
11236+ if (type -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN
11237+ && type -> tp_base != NULL
11238+ && slot_inherited (type , p , ptr ))
11239+ {
11240+ continue ;
11241+ }
1123411242 int r = PyDict_Contains (dict , p -> name_strobj );
1123511243 if (r > 0 )
1123611244 continue ;
0 commit comments