]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105927: PyWeakref_GetRef() returns 1 on success (#106561)
authorVictor Stinner <vstinner@python.org>
Sun, 9 Jul 2023 15:50:26 +0000 (17:50 +0200)
committerGitHub <noreply@github.com>
Sun, 9 Jul 2023 15:50:26 +0000 (15:50 +0000)
PyWeakref_GetRef() now returns 1 on success, and return 0 if the
reference is dead.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Doc/c-api/weakref.rst
Modules/_testcapimodule.c
Objects/weakrefobject.c

index 04781f78d23462cc68ce97ee3a1ee95e841e037f..038f54a9751fd1386e667a6935d3adbeff298719 100644 (file)
@@ -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
 
index 2baf453f71026746ec7455e7066bde2b2e9a0d23..50eaff9917fd177c15ab0ce948184a76d59c37ac 100644 (file)
@@ -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);
index bac3e79bb7c250e75ca23c8b67bc51513401f87c..e9563729bf82ba605df4789364ee385482e85b9d 100644 (file)
@@ -913,7 +913,7 @@ PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
         return -1;
     }
     *pobj = _PyWeakref_GET_REF(ref);
-    return 0;
+    return (*pobj != NULL);
 }