mirror of https://github.com/python/cpython
[3.13] gh-121652: Handle `allocate_weakref` returning NULL (GH-121653) (#121721)
The `allocate_weakref` may return NULL when out of memory. We need to
handle that case and propagate the error.
(cherry picked from commit a640a605a8
)
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
parent
f0c29a2d9f
commit
f78e1aa1f0
|
@ -426,6 +426,10 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
|
||||||
return basic_ref;
|
return basic_ref;
|
||||||
}
|
}
|
||||||
PyWeakReference *newref = allocate_weakref(type, obj, callback);
|
PyWeakReference *newref = allocate_weakref(type, obj, callback);
|
||||||
|
if (newref == NULL) {
|
||||||
|
UNLOCK_WEAKREFS(obj);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
insert_weakref(newref, list);
|
insert_weakref(newref, list);
|
||||||
UNLOCK_WEAKREFS(obj);
|
UNLOCK_WEAKREFS(obj);
|
||||||
return newref;
|
return newref;
|
||||||
|
@ -433,6 +437,9 @@ get_or_create_weakref(PyTypeObject *type, PyObject *obj, PyObject *callback)
|
||||||
else {
|
else {
|
||||||
// We may not be able to safely allocate inside the lock
|
// We may not be able to safely allocate inside the lock
|
||||||
PyWeakReference *newref = allocate_weakref(type, obj, callback);
|
PyWeakReference *newref = allocate_weakref(type, obj, callback);
|
||||||
|
if (newref == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
LOCK_WEAKREFS(obj);
|
LOCK_WEAKREFS(obj);
|
||||||
insert_weakref(newref, list);
|
insert_weakref(newref, list);
|
||||||
UNLOCK_WEAKREFS(obj);
|
UNLOCK_WEAKREFS(obj);
|
||||||
|
|
Loading…
Reference in New Issue