mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-128008: Add PyWeakref_IsDead()
(GH-128009)
The `PyWeakref_IsDead()` function tests if a weak reference is dead without any side effects. Although you can also detect if a weak reference is dead using `PyWeakref_GetRef()`, that function returns a strong reference that must be `Py_DECREF()`'d, which can introduce side effects if the last reference is concurrently dropped (at least in the free threading build).
This commit is contained in:
parent
b9b3e4a076
commit
7b811d0562
5 changed files with 41 additions and 0 deletions
|
@ -932,6 +932,19 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
|
|||
return (PyObject *)get_or_create_weakref(type, ob, callback);
|
||||
}
|
||||
|
||||
int
|
||||
PyWeakref_IsDead(PyObject *ref)
|
||||
{
|
||||
if (ref == NULL) {
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
if (!PyWeakref_Check(ref)) {
|
||||
PyErr_Format(PyExc_TypeError, "expected a weakref, got %T", ref);
|
||||
return -1;
|
||||
}
|
||||
return _PyWeakref_IS_DEAD(ref);
|
||||
}
|
||||
|
||||
int
|
||||
PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue