diff --git a/Modules/_abc.c b/Modules/_abc.c index d585898372ada8d..e68078475214a0a 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -841,16 +841,17 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass, assert(i == registry_size); for (i = 0; i < registry_size; i++) { - PyObject *rkey = PyWeakref_GetObject(copy[i]); - if (rkey == NULL) { + if (!PyWeakref_Check(copy[i])) { + PyErr_BadInternalCall(); // Someone inject non-weakref type in the registry. ret = -1; break; } - if (rkey == Py_None) { + + PyObject *rkey = PyWeakref_GetRef(copy[i]); + if (rkey == NULL) { continue; } - Py_INCREF(rkey); int r = PyObject_IsSubclass(subclass, rkey); Py_DECREF(rkey); if (r < 0) { diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 534ef8c1d6cf8f4..d19924c28a8969a 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -238,20 +238,18 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item) return result; } -PyObject * -PyDict_GetItemProxy(PyObject *dict, PyObject *key) +static PyObject * +_PyDict_GetItemProxy(PyObject *dict, PyObject *key) { - PyObject *result; PyObject *item = PyDict_GetItemWithError(dict, key); - - if (item == NULL) - return NULL; - if (!PyWeakref_CheckProxy(item)) - return item; - result = PyWeakref_GET_OBJECT(item); - if (result == Py_None) + if (item == NULL) { return NULL; - return result; + } + + if (!PyWeakref_CheckProxy(item)) { + return Py_NewRef(item); + } + return PyWeakref_GetRef(item); } /******************************************************************/ @@ -4848,9 +4846,8 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length) Py_DECREF(len); if (!key) return NULL; - result = PyDict_GetItemProxy(cache, key); + result = _PyDict_GetItemProxy(cache, key); if (result) { - Py_INCREF(result); Py_DECREF(key); return result; } diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 7a13821f9d7b5c1..75dc396212c97bc 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -4393,14 +4393,13 @@ _servername_callback(SSL *s, int *al, void *args) * will be passed. If both do not exist only then the C-level object is * passed. */ if (ssl->owner) - ssl_socket = PyWeakref_GetObject(ssl->owner); + ssl_socket = PyWeakref_GetRef(ssl->owner); else if (ssl->Socket) - ssl_socket = PyWeakref_GetObject(ssl->Socket); + ssl_socket = PyWeakref_GetRef(ssl->Socket); else - ssl_socket = (PyObject *) ssl; + ssl_socket = Py_NewRef(ssl); - Py_INCREF(ssl_socket); - if (ssl_socket == Py_None) + if (ssl_socket == Py_NULL) goto error; if (servername == NULL) { diff --git a/Modules/_ssl/debughelpers.c b/Modules/_ssl/debughelpers.c index a81f0aad05a8021..e626c4f6dcad54a 100644 --- a/Modules/_ssl/debughelpers.c +++ b/Modules/_ssl/debughelpers.c @@ -15,7 +15,6 @@ _PySSL_msg_callback(int write_p, int version, int content_type, PyGILState_STATE threadstate; PyObject *res = NULL; PySSLSocket *ssl_obj = NULL; /* ssl._SSLSocket, borrowed ref */ - PyObject *ssl_socket = NULL; /* ssl.SSLSocket or ssl.SSLObject */ int msg_type; threadstate = PyGILState_Ensure(); @@ -27,13 +26,14 @@ _PySSL_msg_callback(int write_p, int version, int content_type, return; } + PyObject *ssl_socket; /* ssl.SSLSocket or ssl.SSLObject */ if (ssl_obj->owner) - ssl_socket = PyWeakref_GetObject(ssl_obj->owner); + ssl_socket = PyWeakref_GetRef(ssl_obj->owner); else if (ssl_obj->Socket) - ssl_socket = PyWeakref_GetObject(ssl_obj->Socket); + ssl_socket = PyWeakref_GetRef(ssl_obj->Socket); else - ssl_socket = (PyObject *)ssl_obj; - Py_INCREF(ssl_socket); + ssl_socket = (PyObject *)Py_NewRef(ssl_obj); + assert(ssl_socket != NULL); /* assume that OpenSSL verifies all payload and buf len is of sufficient length */ diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index c553d039462af05..c9bf9be3b614760 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1025,14 +1025,14 @@ static PyObject * _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) { assert(PyWeakref_CheckRef(localweakref)); - PyObject *obj = PyWeakref_GET_OBJECT(localweakref); - if (obj == Py_None) { + PyObject *obj = PyWeakref_GetRef(localweakref); + if (obj == NULL) { Py_RETURN_NONE; } /* If the thread-local object is still alive and not being cleared, remove the corresponding local dict */ - localobject *self = (localobject *)Py_NewRef(obj); + localobject *self = (localobject *)obj; if (self->dummies != NULL) { PyObject *ldict; ldict = PyDict_GetItemWithError(self->dummies, dummyweakref); @@ -1320,14 +1320,14 @@ release_sentinel(void *wr_raw) /* Tricky: this function is called when the current thread state is being deleted. Therefore, only simple C code can safely execute here. */ - PyObject *obj = PyWeakref_GET_OBJECT(wr); - lockobject *lock; - if (obj != Py_None) { - lock = (lockobject *) obj; + PyObject *obj = PyWeakref_GetRef(wr); + if (obj != NULL) { + lockobject *lock = (lockobject *) obj; if (lock->locked) { PyThread_release_lock(lock->lock_lock); lock->locked = 0; } + Py_DECREF(obj); } /* Deallocating a weakref with a NULL callback only calls PyObject_GC_Del(), which can't call any Python code. */ diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 5d29d2625753152..02b1894677549e1 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5,16 +5,16 @@ #include "pycore_code.h" // CO_FAST_FREE #include "pycore_symtable.h" // _Py_Mangle() #include "pycore_dict.h" // _PyDict_KeysSize() +#include "pycore_frame.h" // _PyInterpreterFrame #include "pycore_initconfig.h" // _PyStatus_OK() +#include "pycore_long.h" // _PyLong_IsNegative() #include "pycore_memoryobject.h" // _PyMemoryView_FromBufferProc() #include "pycore_moduleobject.h" // _PyModule_GetDef() #include "pycore_object.h" // _PyType_HasFeature() -#include "pycore_long.h" // _PyLong_IsNegative() #include "pycore_pyerrors.h" // _PyErr_Occurred() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_typeobject.h" // struct type_cache #include "pycore_unionobject.h" // _Py_union_type_or -#include "pycore_frame.h" // _PyInterpreterFrame #include "opcode.h" // MAKE_CELL #include "structmember.h" // PyMemberDef @@ -75,10 +75,8 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value); static inline PyTypeObject * type_from_ref(PyObject *ref) { - assert(PyWeakref_CheckRef(ref)); - PyObject *obj = PyWeakref_GET_OBJECT(ref); // borrowed ref - assert(obj != NULL); - if (obj == Py_None) { + PyObject *obj = PyWeakref_GetRef(ref); + if (obj == NULL) { return NULL; } assert(PyType_Check(obj)); @@ -450,15 +448,17 @@ _PyType_GetSubclasses(PyTypeObject *self) Py_ssize_t i = 0; PyObject *ref; // borrowed ref while (PyDict_Next(subclasses, &i, NULL, &ref)) { - PyTypeObject *subclass = type_from_ref(ref); // borrowed + PyTypeObject *subclass = type_from_ref(ref); if (subclass == NULL) { continue; } if (PyList_Append(list, _PyObject_CAST(subclass)) < 0) { Py_DECREF(list); + Py_DECREF(subclass); return NULL; } + Py_DECREF(subclass); } return list; } @@ -778,11 +778,12 @@ PyType_Modified(PyTypeObject *type) Py_ssize_t i = 0; PyObject *ref; while (PyDict_Next(subclasses, &i, NULL, &ref)) { - PyTypeObject *subclass = type_from_ref(ref); // borrowed + PyTypeObject *subclass = type_from_ref(ref); if (subclass == NULL) { continue; } PyType_Modified(subclass); + Py_DECREF(subclass); } } @@ -4989,12 +4990,13 @@ clear_static_tp_subclasses(PyTypeObject *type) Py_ssize_t i = 0; PyObject *key, *ref; // borrowed ref while (PyDict_Next(subclasses, &i, &key, &ref)) { - PyTypeObject *subclass = type_from_ref(ref); // borrowed + PyTypeObject *subclass = type_from_ref(ref); if (subclass == NULL) { continue; } // All static builtin subtypes should have been finalized already. assert(!(subclass->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN)); + Py_DECREF(subclass); } clear_tp_subclasses(type); @@ -7636,10 +7638,12 @@ get_subclasses_key(PyTypeObject *type, PyTypeObject *base) PyObject *subclasses = lookup_tp_subclasses(base); if (subclasses != NULL) { while (PyDict_Next(subclasses, &i, &key, &ref)) { - PyTypeObject *subclass = type_from_ref(ref); // borrowed + PyTypeObject *subclass = type_from_ref(ref); if (subclass == type) { + Py_DECREF(subclass); return Py_NewRef(key); } + Py_DECREF(subclass); } } /* It wasn't found. */ @@ -10035,7 +10039,7 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name, Py_ssize_t i = 0; PyObject *ref; while (PyDict_Next(subclasses, &i, NULL, &ref)) { - PyTypeObject *subclass = type_from_ref(ref); // borrowed + PyTypeObject *subclass = type_from_ref(ref); if (subclass == NULL) { continue; } @@ -10045,16 +10049,20 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name, if (dict != NULL && PyDict_Check(dict)) { int r = PyDict_Contains(dict, attr_name); if (r < 0) { + Py_DECREF(subclass); return -1; } if (r > 0) { + Py_DECREF(subclass); continue; } } if (update_subclasses(subclass, attr_name, callback, data) < 0) { + Py_DECREF(subclass); return -1; } + Py_DECREF(subclass); } return 0; }