From 3de35794302257fd9c1bf71feaa04ebff89751f3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 9 Jul 2023 10:25:46 +0200 Subject: [PATCH 1/2] gh-105927: PyWeakref_GetRef() returns 1 on success PyWeakref_GetRef() now returns 1 on success, and return 0 if the reference is dead. --- Doc/c-api/weakref.rst | 6 ++++-- Modules/_testcapimodule.c | 2 +- Objects/weakrefobject.c | 6 +++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index 04781f78d23462..038f54a9751fd1 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -55,9 +55,11 @@ as much as it can. Get a :term:`strong reference` to the referenced object from a weak reference, *ref*, into *\*pobj*. - Return 0 on success. Raise an exception and return -1 on error. - If the referent is no longer live, set *\*pobj* to ``NULL`` and return 0. + * On success, set *\*pobj* to a new :term:`strong reference` to the + referenced object and return 1. + * If the reference is dead, set *\*pobj* to ``NULL`` and return 0. + * On error, raise an exception and return -1. .. versionadded:: 3.13 diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 2baf453f710267..50eaff9917fd17 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3376,7 +3376,7 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) // test PyWeakref_GetRef(), reference is alive PyObject *ref = Py_True; // marker to check that value was set - assert(PyWeakref_GetRef(weakref, &ref) == 0); + assert(PyWeakref_GetRef(weakref, &ref) == 1); assert(ref == obj); assert(Py_REFCNT(obj) == (refcnt + 1)); Py_DECREF(ref); diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index bac3e79bb7c250..0970a8dafdcb12 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -913,7 +913,11 @@ PyWeakref_GetRef(PyObject *ref, PyObject **pobj) return -1; } *pobj = _PyWeakref_GET_REF(ref); - return 0; + if (*pobj == NULL) { + // The reference is dead + return 0; + } + return 1; } From 727a655345b7a76defb94752521da5e027063e79 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 9 Jul 2023 17:18:04 +0200 Subject: [PATCH 2/2] Update Objects/weakrefobject.c Co-authored-by: Serhiy Storchaka --- Objects/weakrefobject.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 0970a8dafdcb12..e9563729bf82ba 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -913,11 +913,7 @@ PyWeakref_GetRef(PyObject *ref, PyObject **pobj) return -1; } *pobj = _PyWeakref_GET_REF(ref); - if (*pobj == NULL) { - // The reference is dead - return 0; - } - return 1; + return (*pobj != NULL); }