Skip to content

Commit 5753693

Browse files
committed
gh-105927: Deprecate PyWeakref_GetObject() function
Deprecate PyWeakref_GetObject() and PyWeakref_GET_OBJECT() functions.
1 parent 6a80664 commit 5753693

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

Doc/c-api/weakref.rst

+6
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ as much as it can.
7474
except when it cannot be destroyed before the last usage of the borrowed
7575
reference.
7676
77+
.. deprecated-removed:: 3.13 3.15
78+
Use :c:func:`PyWeakref_GetRef` instead.
79+
7780
7881
.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)
7982
8083
Similar to :c:func:`PyWeakref_GetObject`, but does no error checking.
8184
85+
.. deprecated-removed:: 3.13 3.15
86+
Use :c:func:`PyWeakref_GetRef` instead.
87+
8288
8389
.. c:function:: void PyObject_ClearWeakRefs(PyObject *object)
8490

Doc/whatsnew/3.13.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ Deprecated
470470
Scheduled for removal in Python 3.15.
471471
(Contributed by Victor Stinner in :gh:`105396`.)
472472

473+
* Deprecate the :c:func:`PyWeakref_GetObject` and
474+
:c:func:`PyWeakref_GET_OBJECT` functions, which return a :term:`borrowed
475+
reference`: use the new :c:func:`PyWeakref_GetRef` function instead, it
476+
returns a :term:`strong reference`. The `pythoncapi-compat project
477+
<https://github.com/python/pythoncapi-compat/>`__ can be used to get
478+
:c:func:`PyWeakref_GetRef` on Python 3.12 and older.
479+
(Contributed by Victor Stinner in :gh:`105927`.)
480+
473481
Removed
474482
-------
475483

@@ -565,6 +573,6 @@ Removed
565573
* Remove the old private, undocumented and untested ``_PyGC_FINALIZED()`` macro
566574
which was kept for backward compatibility with Python 3.8 and older: use
567575
:c:func:`PyObject_GC_IsFinalized()` instead. The `pythoncapi-compat project
568-
<https://github.com/python/pythoncapi-compat/>`_ can be used to get this
576+
<https://github.com/python/pythoncapi-compat/>`__ can be used to get this
569577
function on Python 3.8 and older.
570578
(Contributed by Victor Stinner in :gh:`105268`.)

Include/cpython/weakrefobject.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ struct _PyWeakReference {
3232
vectorcallfunc vectorcall;
3333
};
3434

35-
static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
35+
Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj)
36+
{
3637
PyWeakReference *ref;
3738
PyObject *obj;
3839
assert(PyWeakref_Check(ref_obj));

Include/weakrefobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
2727
PyObject *callback);
2828
PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
2929
PyObject *callback);
30-
PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
30+
Py_DEPRECATED(3.13) PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
3131
PyAPI_FUNC(int) PyWeakref_GetRef(PyObject *ref, PyObject **pobj);
3232

3333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deprecate the :c:func:`PyWeakref_GetObject` and
2+
:c:func:`PyWeakref_GET_OBJECT` functions: use the new
3+
:c:func:`PyWeakref_GetRef` function instead. Patch by Victor Stinner.

Modules/_testcapimodule.c

+6
Original file line numberDiff line numberDiff line change
@@ -3375,6 +3375,10 @@ check_pyimport_addmodule(PyObject *self, PyObject *args)
33753375
static PyObject *
33763376
test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
33773377
{
3378+
// Ignore PyWeakref_GetObject() deprecation, we test it on purpose
3379+
_Py_COMP_DIAG_PUSH
3380+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
3381+
33783382
// Create a new heap type, create an instance of this type, and delete the
33793383
// type. This object supports weak references.
33803384
PyObject *new_type = PyObject_CallFunction((PyObject*)&PyType_Type,
@@ -3463,6 +3467,8 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
34633467
Py_DECREF(weakref);
34643468

34653469
Py_RETURN_NONE;
3470+
3471+
_Py_COMP_DIAG_POP
34663472
}
34673473

34683474

Objects/weakrefobject.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,12 @@ PyWeakref_GetObject(PyObject *ref)
923923
PyErr_BadInternalCall();
924924
return NULL;
925925
}
926-
return PyWeakref_GET_OBJECT(ref);
926+
PyObject *obj = _PyWeakref_GET_REF(ref);
927+
if (obj == NULL) {
928+
return Py_None;
929+
}
930+
Py_DECREF(obj);
931+
return obj; // borrowed reference
927932
}
928933

929934
/* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's

0 commit comments

Comments
 (0)