33#include "Python.h"
44#include "pycore_call.h"
55#include "pycore_code.h" // CO_FAST_FREE
6- #include "pycore_symtable.h" // _Py_Mangle()
76#include "pycore_dict.h" // _PyDict_KeysSize()
7+ #include "pycore_frame.h" // _PyInterpreterFrame
88#include "pycore_initconfig.h" // _PyStatus_OK()
9+ #include "pycore_long.h" // _PyLong_IsNegative()
910#include "pycore_memoryobject.h" // _PyMemoryView_FromBufferProc()
1011#include "pycore_moduleobject.h" // _PyModule_GetDef()
1112#include "pycore_object.h" // _PyType_HasFeature()
12- #include "pycore_long.h" // _PyLong_IsNegative()
1313#include "pycore_pyerrors.h" // _PyErr_Occurred()
1414#include "pycore_pystate.h" // _PyThreadState_GET()
15+ #include "pycore_symtable.h" // _Py_Mangle()
1516#include "pycore_typeobject.h" // struct type_cache
1617#include "pycore_unionobject.h" // _Py_union_type_or
17- #include "pycore_frame .h" // _PyInterpreterFrame
18+ #include "pycore_weakref .h" // _PyWeakref_GET_REF()
1819#include "opcode.h" // MAKE_CELL
1920#include "structmember.h" // PyMemberDef
2021
@@ -75,13 +76,10 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value);
7576static inline PyTypeObject *
7677type_from_ref (PyObject * ref )
7778{
78- assert (PyWeakref_CheckRef (ref ));
79- PyObject * obj = PyWeakref_GET_OBJECT (ref ); // borrowed ref
80- assert (obj != NULL );
81- if (obj == Py_None ) {
79+ PyObject * obj = _PyWeakref_GET_REF (ref );
80+ if (obj == NULL ) {
8281 return NULL ;
8382 }
84- assert (PyType_Check (obj ));
8583 return _PyType_CAST (obj );
8684}
8785
@@ -450,15 +448,17 @@ _PyType_GetSubclasses(PyTypeObject *self)
450448 Py_ssize_t i = 0 ;
451449 PyObject * ref ; // borrowed ref
452450 while (PyDict_Next (subclasses , & i , NULL , & ref )) {
453- PyTypeObject * subclass = type_from_ref (ref ); // borrowed
451+ PyTypeObject * subclass = type_from_ref (ref );
454452 if (subclass == NULL ) {
455453 continue ;
456454 }
457455
458456 if (PyList_Append (list , _PyObject_CAST (subclass )) < 0 ) {
459457 Py_DECREF (list );
458+ Py_DECREF (subclass );
460459 return NULL ;
461460 }
461+ Py_DECREF (subclass );
462462 }
463463 return list ;
464464}
@@ -778,11 +778,12 @@ PyType_Modified(PyTypeObject *type)
778778 Py_ssize_t i = 0 ;
779779 PyObject * ref ;
780780 while (PyDict_Next (subclasses , & i , NULL , & ref )) {
781- PyTypeObject * subclass = type_from_ref (ref ); // borrowed
781+ PyTypeObject * subclass = type_from_ref (ref );
782782 if (subclass == NULL ) {
783783 continue ;
784784 }
785785 PyType_Modified (subclass );
786+ Py_DECREF (subclass );
786787 }
787788 }
788789
@@ -4989,12 +4990,13 @@ clear_static_tp_subclasses(PyTypeObject *type)
49894990 Py_ssize_t i = 0 ;
49904991 PyObject * key , * ref ; // borrowed ref
49914992 while (PyDict_Next (subclasses , & i , & key , & ref )) {
4992- PyTypeObject * subclass = type_from_ref (ref ); // borrowed
4993+ PyTypeObject * subclass = type_from_ref (ref );
49934994 if (subclass == NULL ) {
49944995 continue ;
49954996 }
49964997 // All static builtin subtypes should have been finalized already.
49974998 assert (!(subclass -> tp_flags & _Py_TPFLAGS_STATIC_BUILTIN ));
4999+ Py_DECREF (subclass );
49985000 }
49995001
50005002 clear_tp_subclasses (type );
@@ -7636,10 +7638,15 @@ get_subclasses_key(PyTypeObject *type, PyTypeObject *base)
76367638 PyObject * subclasses = lookup_tp_subclasses (base );
76377639 if (subclasses != NULL ) {
76387640 while (PyDict_Next (subclasses , & i , & key , & ref )) {
7639- PyTypeObject * subclass = type_from_ref (ref ); // borrowed
7641+ PyTypeObject * subclass = type_from_ref (ref );
7642+ if (subclass == NULL ) {
7643+ continue ;
7644+ }
76407645 if (subclass == type ) {
7646+ Py_DECREF (subclass );
76417647 return Py_NewRef (key );
76427648 }
7649+ Py_DECREF (subclass );
76437650 }
76447651 }
76457652 /* It wasn't found. */
@@ -10035,7 +10042,7 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1003510042 Py_ssize_t i = 0 ;
1003610043 PyObject * ref ;
1003710044 while (PyDict_Next (subclasses , & i , NULL , & ref )) {
10038- PyTypeObject * subclass = type_from_ref (ref ); // borrowed
10045+ PyTypeObject * subclass = type_from_ref (ref );
1003910046 if (subclass == NULL ) {
1004010047 continue ;
1004110048 }
@@ -10045,16 +10052,20 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1004510052 if (dict != NULL && PyDict_Check (dict )) {
1004610053 int r = PyDict_Contains (dict , attr_name );
1004710054 if (r < 0 ) {
10055+ Py_DECREF (subclass );
1004810056 return -1 ;
1004910057 }
1005010058 if (r > 0 ) {
10059+ Py_DECREF (subclass );
1005110060 continue ;
1005210061 }
1005310062 }
1005410063
1005510064 if (update_subclasses (subclass , attr_name , callback , data ) < 0 ) {
10065+ Py_DECREF (subclass );
1005610066 return -1 ;
1005710067 }
10068+ Py_DECREF (subclass );
1005810069 }
1005910070 return 0 ;
1006010071}
0 commit comments