Skip to content

Commit ee46cb6

Browse files
gh-105927: PyWeakref_GetRef() returns 1 on success (#106561)
PyWeakref_GetRef() now returns 1 on success, and return 0 if the reference is dead. Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 1e12c8c commit ee46cb6

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

Doc/c-api/weakref.rst

+4-2
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
913913
return -1;
914914
}
915915
*pobj = _PyWeakref_GET_REF(ref);
916-
return 0;
916+
return (*pobj != NULL);
917917
}
918918

919919

0 commit comments

Comments
 (0)