]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133644: remove `PyWeakref_GetObject` and `PyWeakref_GET_OBJECT` (GH-133657)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Wed, 24 Sep 2025 09:25:56 +0000 (11:25 +0200)
committerGitHub <noreply@github.com>
Wed, 24 Sep 2025 09:25:56 +0000 (11:25 +0200)
16 files changed:
Doc/c-api/weakref.rst
Doc/data/refcounts.dat
Doc/data/stable_abi.dat
Doc/deprecations/c-api-pending-removal-in-3.15.rst
Doc/howto/free-threading-extensions.rst
Doc/whatsnew/3.11.rst
Doc/whatsnew/3.13.rst
Doc/whatsnew/3.15.rst
Include/cpython/weakrefobject.h
Include/weakrefobject.h
Misc/NEWS.d/3.11.0a2.rst
Misc/NEWS.d/3.13.0a1.rst
Misc/NEWS.d/next/C_API/2025-05-08-12-40-59.gh-issue-133644.FNexLJ.rst [new file with mode: 0644]
Misc/stable_abi.toml
Modules/_testcapimodule.c
Objects/weakrefobject.c

index c3c6cf413dcef586888ff51d7b0cd432fbe547fb..14ec9d951c4a5fddba6402cc34efb6c9e96b8513 100644 (file)
@@ -64,30 +64,6 @@ as much as it can.
    .. versionadded:: 3.13
 
 
-.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
-
-   Return a :term:`borrowed reference` to the referenced object from a weak
-   reference, *ref*.  If the referent is no longer live, returns ``Py_None``.
-
-   .. note::
-
-      This function returns a :term:`borrowed reference` to the referenced object.
-      This means that you should always call :c:func:`Py_INCREF` on the object
-      except when it cannot be destroyed before the last usage of the borrowed
-      reference.
-
-   .. deprecated-removed:: 3.13 3.15
-      Use :c:func:`PyWeakref_GetRef` instead.
-
-
-.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)
-
-   Similar to :c:func:`PyWeakref_GetObject`, but does no error checking.
-
-   .. deprecated-removed:: 3.13 3.15
-      Use :c:func:`PyWeakref_GetRef` instead.
-
-
 .. c:function:: int PyWeakref_IsDead(PyObject *ref)
 
    Test if the weak reference *ref* is dead. Returns 1 if the reference is
index 144c5608e074262e968167a542c6c341bee40908..44ee25863012593f6042394ca687400a0d243a52 100644 (file)
@@ -2947,12 +2947,6 @@ PyWeakref_CheckProxy:PyObject*:ob:0:
 PyWeakref_CheckRef:int:::
 PyWeakref_CheckRef:PyObject*:ob:0:
 
-PyWeakref_GET_OBJECT:PyObject*::0:
-PyWeakref_GET_OBJECT:PyObject*:ref:0:
-
-PyWeakref_GetObject:PyObject*::0:
-PyWeakref_GetObject:PyObject*:ref:0:
-
 PyWeakref_GetRef:int:::
 PyWeakref_GetRef:PyObject*:ref:0:
 PyWeakref_GetRef:PyObject**:pobj:+1:
index 3f51254494c654edade8f7a80ec809383f7af04d..7ad5f3ecfab5b46e12e6eb26eabbd1d853a95def 100644 (file)
@@ -830,7 +830,6 @@ member,PyVarObject.ob_size,3.2,,
 func,PyVectorcall_Call,3.12,,
 func,PyVectorcall_NARGS,3.12,,
 type,PyWeakReference,3.2,,opaque
-func,PyWeakref_GetObject,3.2,,
 func,PyWeakref_GetRef,3.13,,
 func,PyWeakref_NewProxy,3.2,,
 func,PyWeakref_NewRef,3.2,,
index 8db3631cab31f1f6f01c0072028e2d9619503089..9927b876760d34483d32c23e41d2f6d0ab52114d 100644 (file)
@@ -3,7 +3,7 @@ Pending removal in Python 3.15
 
 * The :c:func:`!PyImport_ImportModuleNoBlock`:
   Use :c:func:`PyImport_ImportModule` instead.
-* :c:func:`PyWeakref_GetObject` and :c:func:`PyWeakref_GET_OBJECT`:
+* :c:func:`!PyWeakref_GetObject` and :c:func:`!PyWeakref_GET_OBJECT`:
   Use :c:func:`PyWeakref_GetRef` instead. The `pythoncapi-compat project
   <https://github.com/python/pythoncapi-compat/>`__ can be used to get
   :c:func:`PyWeakref_GetRef` on Python 3.12 and older.
index 577e283bb9cb4c64afd7b1c99929969a185384a0..3776132c685414c672a29d3288e25475464a165b 100644 (file)
@@ -173,9 +173,9 @@ that return :term:`strong references <strong reference>`.
 +-----------------------------------+-----------------------------------+
 | :c:func:`PyDict_Next`             | none (see :ref:`PyDict_Next`)     |
 +-----------------------------------+-----------------------------------+
-| :c:func:`PyWeakref_GetObject`     | :c:func:`PyWeakref_GetRef`        |
+| :c:func:`!PyWeakref_GetObject`    | :c:func:`PyWeakref_GetRef`        |
 +-----------------------------------+-----------------------------------+
-| :c:func:`PyWeakref_GET_OBJECT`    | :c:func:`PyWeakref_GetRef`        |
+| :c:func:`!PyWeakref_GET_OBJECT`   | :c:func:`PyWeakref_GetRef`        |
 +-----------------------------------+-----------------------------------+
 | :c:func:`PyImport_AddModule`      | :c:func:`PyImport_AddModuleRef`   |
 +-----------------------------------+-----------------------------------+
index 2dd205dd2b8831321c503aa7481d2ffbad303198..a095d887352127a8f3d15b605e349ad9cd864d89 100644 (file)
@@ -2673,7 +2673,7 @@ Removed
 
   (Contributed by Victor Stinner in :issue:`45474`.)
 
-* Exclude :c:func:`PyWeakref_GET_OBJECT` from the limited C API. It never
+* Exclude :c:func:`!PyWeakref_GET_OBJECT` from the limited C API. It never
   worked since the :c:type:`!PyWeakReference` structure is opaque in the
   limited C API.
   (Contributed by Victor Stinner in :issue:`35134`.)
index 67fec4ebc4a234271d5f6cdec5bbcb28ce4d9807..fbb27adbf9969c23ea8b03cbf8cca83b707cada3 100644 (file)
@@ -2246,7 +2246,7 @@ New Features
   (Contributed by Serhiy Storchaka in :gh:`110289`.)
 
 * Add the :c:func:`PyWeakref_GetRef` function
-  as an alternative to  :c:func:`PyWeakref_GetObject`
+  as an alternative to  :c:func:`!PyWeakref_GetObject`
   that returns a :term:`strong reference`
   or ``NULL`` if the referent is no longer live.
   (Contributed by Victor Stinner in :gh:`105927`.)
@@ -2531,8 +2531,8 @@ Deprecated C APIs
   are just aliases to :c:type:`!wchar_t`.
   (Contributed by Victor Stinner in :gh:`105156`.)
 
-* Deprecate the :c:func:`PyWeakref_GetObject` and
-  :c:func:`PyWeakref_GET_OBJECT` functions,
+* Deprecate the :c:func:`!PyWeakref_GetObject` and
+  :c:func:`!PyWeakref_GET_OBJECT` functions,
   which return a :term:`borrowed reference`.
   Replace them with the new :c:func:`PyWeakref_GetRef` function,
   which returns a :term:`strong reference`.
index 7b146621dddcfaa7aa3c23be273fc32b586324a4..d5d387d9a0aaa72acd3c8031b42674d1482b158d 100644 (file)
@@ -869,6 +869,11 @@ Removed C APIs
   of :c:func:`PyImport_ImportModule`.
   (Contributed by Bénédikt Tran in :gh:`133644`.)
 
+* :c:func:`!PyWeakref_GetObject` and :c:macro:`!PyWeakref_GET_OBJECT`:
+  use :c:func:`PyWeakref_GetRef` instead. The |pythoncapi_compat_project|
+  can be used to get :c:func:`!PyWeakref_GetRef` on Python 3.12 and older.
+  (Contributed by Bénédikt Tran in :gh:`133644`.)
+
 * Remove deprecated :c:func:`!PySys_ResetWarnOptions`.
   Clear :data:`sys.warnoptions` and :data:`!warnings.filters` instead.
 
index da8e77cddaca63823c65e577b0b23f664bf4a32a..e0711407cee47085fe898bc1ab071a9211bbb18c 100644 (file)
@@ -47,20 +47,3 @@ PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
 
 // Test if a weak reference is dead.
 PyAPI_FUNC(int) PyWeakref_IsDead(PyObject *ref);
-
-Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj)
-{
-    PyWeakReference *ref = _PyWeakref_CAST(ref_obj);
-    PyObject *obj = ref->wr_object;
-    // Explanation for the Py_REFCNT() check: when a weakref's target is part
-    // of a long chain of deallocations which triggers the trashcan mechanism,
-    // clearing the weakrefs can be delayed long after the target's refcount
-    // has dropped to zero.  In the meantime, code accessing the weakref will
-    // be able to "see" the target object even though it is supposed to be
-    // unreachable.  See issue gh-60806.
-    if (Py_REFCNT(obj) > 0) {
-        return obj;
-    }
-    return Py_None;
-}
-#define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))
index a6e71eb178b124b236b0668ddda4fc27f5b64a7b..17fac62961c0fb8ec58a648605c5933b9fe952d7 100644 (file)
@@ -27,7 +27,6 @@ PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
                                         PyObject *callback);
 PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
                                           PyObject *callback);
-Py_DEPRECATED(3.13) PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
 
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000
 PyAPI_FUNC(int) PyWeakref_GetRef(PyObject *ref, PyObject **pobj);
index 48cf2c1e428d878f4f09bbe2688ade1600f0e8ea..12e03b46db0b3f1a8ffc4858b065a431d15520ce 100644 (file)
@@ -1188,7 +1188,7 @@ context objects can now be disabled.
 .. nonce: Z0Zk_m
 .. section: C API
 
-Exclude :c:func:`PyWeakref_GET_OBJECT` from the limited C API. It never
+Exclude :c:func:`!PyWeakref_GET_OBJECT` from the limited C API. It never
 worked since the :c:type:`!PyWeakReference` structure is opaque in the
 limited C API.
 
index a3aa7353a1bba15b76c403603e58f692f1c1f1d5..a5f0161e8f591e263beb47b64c8fa487dc87fcd9 100644 (file)
@@ -6458,8 +6458,8 @@ Victor Stinner.
 .. nonce: GRxZtI
 .. section: C API
 
-Deprecate the :c:func:`PyWeakref_GetObject` and
-:c:func:`PyWeakref_GET_OBJECT` functions: use the new
+Deprecate the :c:func:`!PyWeakref_GetObject` and
+:c:func:`!PyWeakref_GET_OBJECT` functions: use the new
 :c:func:`PyWeakref_GetRef` function instead. Patch by Victor Stinner.
 
 ..
@@ -6470,7 +6470,7 @@ Deprecate the :c:func:`PyWeakref_GetObject` and
 .. section: C API
 
 Add :c:func:`PyWeakref_GetRef` function: similar to
-:c:func:`PyWeakref_GetObject` but returns a :term:`strong reference`, or
+:c:func:`!PyWeakref_GetObject` but returns a :term:`strong reference`, or
 ``NULL`` if the referent is no longer live. Patch by Victor Stinner.
 
 ..
diff --git a/Misc/NEWS.d/next/C_API/2025-05-08-12-40-59.gh-issue-133644.FNexLJ.rst b/Misc/NEWS.d/next/C_API/2025-05-08-12-40-59.gh-issue-133644.FNexLJ.rst
new file mode 100644 (file)
index 0000000..71f1eaa
--- /dev/null
@@ -0,0 +1,3 @@
+Remove deprecated function :c:func:`!PyWeakref_GetObject` and macro
+:c:macro:`!PyWeakref_GET_OBJECT`. Use :c:func:`PyWeakref_GetRef` instead.
+Patch by Bénédikt Tran.
index d651e0fac111b169d6ff9c619f3f9c682420bb5c..4a03cc76f5e1e9fd69e1b87f7d2b4aaf2ad70b81 100644 (file)
     added = '3.2'
 [function.PyWeakref_GetObject]
     added = '3.2'
+    abi_only = true
 [function.PyWeakref_NewProxy]
     added = '3.2'
 [function.PyWeakref_NewRef]
index a5c4604056ab4e32c85c3b1ba2d2618fd7fce26c..508ef55511e49deaa59e24398eea94209a15590c 100644 (file)
@@ -2206,9 +2206,8 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args))
 static PyObject *
 test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
 {
-    // Ignore PyWeakref_GetObject() deprecation, we test it on purpose
-    _Py_COMP_DIAG_PUSH
-    _Py_COMP_DIAG_IGNORE_DEPR_DECLS
+    // Get the function (removed in 3.15) from the stable ABI.
+    PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *);
 
     // Create a new heap type, create an instance of this type, and delete the
     // type. This object supports weak references.
@@ -2249,19 +2248,12 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
     ref = PyWeakref_GetObject(weakref);  // borrowed ref
     assert(ref == obj);
 
-    // test PyWeakref_GET_OBJECT(), reference is alive
-    ref = PyWeakref_GET_OBJECT(weakref);  // borrowed ref
-    assert(ref == obj);
-
     // delete the referenced object: clear the weakref
     assert(Py_REFCNT(obj) == 1);
     Py_DECREF(obj);
 
     assert(PyWeakref_IsDead(weakref));
 
-    // test PyWeakref_GET_OBJECT(), reference is dead
-    assert(PyWeakref_GET_OBJECT(weakref) == Py_None);
-
     // test PyWeakref_GetRef(), reference is dead
     ref = UNINITIALIZED_PTR;
     assert(PyWeakref_GetRef(weakref, &ref) == 0);
@@ -2312,8 +2304,6 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
     Py_DECREF(weakref);
 
     Py_RETURN_NONE;
-
-    _Py_COMP_DIAG_POP
 }
 
 struct simpletracer_data {
index bd4c4ac9b3475a426c168530741b40ea1496fdd7..61fa3ddad0bfd83712bde080e1b4845820daf758 100644 (file)
@@ -964,7 +964,8 @@ PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
 }
 
 
-PyObject *
+/* removed in 3.15, but kept for stable ABI compatibility */
+PyAPI_FUNC(PyObject *)
 PyWeakref_GetObject(PyObject *ref)
 {
     if (ref == NULL || !PyWeakref_Check(ref)) {