]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105927: Deprecate PyWeakref_GetObject() function (#106006)
authorVictor Stinner <vstinner@python.org>
Mon, 26 Jun 2023 10:10:53 +0000 (12:10 +0200)
committerGitHub <noreply@github.com>
Mon, 26 Jun 2023 10:10:53 +0000 (12:10 +0200)
Deprecate PyWeakref_GetObject() and PyWeakref_GET_OBJECT() functions.

Doc/c-api/weakref.rst
Doc/whatsnew/3.13.rst
Include/cpython/weakrefobject.h
Include/weakrefobject.h
Misc/NEWS.d/next/C API/2023-06-22-00-25-55.gh-issue-105927.GRxZtI.rst [new file with mode: 0644]
Modules/_testcapimodule.c
Objects/weakrefobject.c

index 44f4dce9ea02383cccdc25617938a11f99ef48fe..04781f78d23462cc68ce97ee3a1ee95e841e037f 100644 (file)
@@ -74,11 +74,17 @@ as much as it can.
       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:: void PyObject_ClearWeakRefs(PyObject *object)
 
index ee8c198734076d2431aaabcc3964cf532fdcef4b..b6d13a8aac0f0e091574cacd4cea6234955c3c76 100644 (file)
@@ -470,6 +470,14 @@ Deprecated
   Scheduled for removal in Python 3.15.
   (Contributed by Victor Stinner in :gh:`105396`.)
 
+* Deprecate the :c:func:`PyWeakref_GetObject` and
+  :c:func:`PyWeakref_GET_OBJECT` functions, which return a :term:`borrowed
+  reference`: use the new :c:func:`PyWeakref_GetRef` function instead, it
+  returns a :term:`strong reference`. 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.
+  (Contributed by Victor Stinner in :gh:`105927`.)
+
 Removed
 -------
 
@@ -565,7 +573,7 @@ Removed
 * Remove the old private, undocumented and untested ``_PyGC_FINALIZED()`` macro
   which was kept for backward compatibility with Python 3.8 and older: use
   :c:func:`PyObject_GC_IsFinalized()` instead. The `pythoncapi-compat project
-  <https://github.com/python/pythoncapi-compat/>`_ can be used to get this
+  <https://github.com/python/pythoncapi-compat/>`__ can be used to get this
   function on Python 3.8 and older.
   (Contributed by Victor Stinner in :gh:`105268`.)
 
index 6bf165538142529a5a960955c257236980374968..1559e2def61260c21b0419c9707d39210d933d3a 100644 (file)
@@ -32,7 +32,8 @@ struct _PyWeakReference {
     vectorcallfunc vectorcall;
 };
 
-static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
+Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj)
+{
     PyWeakReference *ref;
     PyObject *obj;
     assert(PyWeakref_Check(ref_obj));
index 2c69f9e4564ab3f83528a3f8eb3be8cfbc0b1571..727ba6934bbacbbd310c0450e716819168017c9f 100644 (file)
@@ -27,7 +27,7 @@ PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
                                         PyObject *callback);
 PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
                                           PyObject *callback);
-PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
+Py_DEPRECATED(3.13) PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
 PyAPI_FUNC(int) PyWeakref_GetRef(PyObject *ref, PyObject **pobj);
 
 
diff --git a/Misc/NEWS.d/next/C API/2023-06-22-00-25-55.gh-issue-105927.GRxZtI.rst b/Misc/NEWS.d/next/C API/2023-06-22-00-25-55.gh-issue-105927.GRxZtI.rst
new file mode 100644 (file)
index 0000000..57982dc
--- /dev/null
@@ -0,0 +1,3 @@
+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.
index d847539f6608dd2acc2c5655c0922c0d0237d98d..dc8acec6c7692191faae60dc467a2119e5b5370c 100644 (file)
@@ -3375,6 +3375,10 @@ error:
 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
+
     // Create a new heap type, create an instance of this type, and delete the
     // type. This object supports weak references.
     PyObject *new_type = PyObject_CallFunction((PyObject*)&PyType_Type,
@@ -3463,6 +3467,8 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
     Py_DECREF(weakref);
 
     Py_RETURN_NONE;
+
+    _Py_COMP_DIAG_POP
 }
 
 
index c54f663acdb66c970e43421503445cc420cb85b0..f3f6c86637e9de35b81c0060f2173ee17f1c9d82 100644 (file)
@@ -923,7 +923,12 @@ PyWeakref_GetObject(PyObject *ref)
         PyErr_BadInternalCall();
         return NULL;
     }
-    return PyWeakref_GET_OBJECT(ref);
+    PyObject *obj = _PyWeakref_GET_REF(ref);
+    if (obj == NULL) {
+        return Py_None;
+    }
+    Py_DECREF(obj);
+    return obj;  // borrowed reference
 }
 
 /* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's