Skip to content

Commit 3de3579

Browse files
committed
pythongh-105927: PyWeakref_GetRef() returns 1 on success
PyWeakref_GetRef() now returns 1 on success, and return 0 if the reference is dead.
1 parent da98ed0 commit 3de3579

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎Doc/c-api/weakref.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ as much as it can.
5555
5656
Get a :term:`strong reference` to the referenced object from a weak
5757
reference, *ref*, into *\*pobj*.
58-
Return 0 on success. Raise an exception and return -1 on error.
5958
60-
If the referent is no longer live, set *\*pobj* to ``NULL`` and return 0.
59+
* On success, set *\*pobj* to a new :term:`strong reference` to the
60+
referenced object and return 1.
61+
* If the reference is dead, set *\*pobj* to ``NULL`` and return 0.
62+
* On error, raise an exception and return -1.
6163
6264
.. versionadded:: 3.13
6365

‎Modules/_testcapimodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3376,7 +3376,7 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
33763376

33773377
// test PyWeakref_GetRef(), reference is alive
33783378
PyObject *ref = Py_True; // marker to check that value was set
3379-
assert(PyWeakref_GetRef(weakref, &ref) == 0);
3379+
assert(PyWeakref_GetRef(weakref, &ref) == 1);
33803380
assert(ref == obj);
33813381
assert(Py_REFCNT(obj) == (refcnt + 1));
33823382
Py_DECREF(ref);

‎Objects/weakrefobject.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,11 @@ PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
913913
return -1;
914914
}
915915
*pobj = _PyWeakref_GET_REF(ref);
916-
return 0;
916+
if (*pobj == NULL) {
917+
// The reference is dead
918+
return 0;
919+
}
920+
return 1;
917921
}
918922

919923

0 commit comments

Comments
 (0)