gh-105927: Deprecate PyWeakref_GetObject() function (#106006) · python/cpython@c075a19 · GitHub
Skip to content

Commit c075a19

Browse files
authored
gh-105927: Deprecate PyWeakref_GetObject() function (#106006)
Deprecate PyWeakref_GetObject() and PyWeakref_GET_OBJECT() functions.
1 parent dac3d38 commit c075a19

7 files changed

Lines changed: 33 additions & 4 deletions

File tree

Doc/c-api/weakref.rst

Lines changed: 6 additions & 0 deletions

Doc/whatsnew/3.13.rst

Lines changed: 9 additions & 1 deletion
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,7 +573,7 @@ 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`.)
571579

Include/cpython/weakrefobject.h

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion

0 commit comments

Comments
 (0)