Skip to content

Commit

Permalink
pythongh-105927: Use PyWeakref_GetRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jun 20, 2023
1 parent b7997c9 commit c90f229
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
9 changes: 5 additions & 4 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 10 additions & 13 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/******************************************************************/
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions Modules/_ssl/debughelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 */
Expand Down
14 changes: 7 additions & 7 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down
30 changes: 19 additions & 11 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit c90f229

Please sign in to comment.