gh-121652: Handle `allocate_weakref` returning NULL (#121653)

The `allocate_weakref` may return NULL when out of memory. We need to
handle that case and propagate the error.
This commit is contained in:
Sam Gross 2024-07-13 12:07:52 -04:00 committed by GitHub
parent a183474293
commit a640a605a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 0 deletions

View File

@ -426,6 +426,10 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
return basic_ref;
}
PyWeakReference *newref = allocate_weakref(type, obj, callback);
if (newref == NULL) {
UNLOCK_WEAKREFS(obj);
return NULL;
}
insert_weakref(newref, list);
UNLOCK_WEAKREFS(obj);
return newref;
@ -433,6 +437,9 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
else {
// We may not be able to safely allocate inside the lock
PyWeakReference *newref = allocate_weakref(type, obj, callback);
if (newref == NULL) {
return NULL;
}
LOCK_WEAKREFS(obj);
insert_weakref(newref, list);
UNLOCK_WEAKREFS(obj);