Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105927: Deprecate PyWeakref_GetObject() function #106006

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/c-api/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down Expand Up @@ -565,6 +573,6 @@ 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`.)
3 changes: 2 additions & 1 deletion Include/cpython/weakrefobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion Include/weakrefobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3375,6 +3375,10 @@ check_pyimport_addmodule(PyObject *self, PyObject *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
Comment on lines +3379 to +3380
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know these macros; TIL!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very useful to write tests on deprecated APIs.


// 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,
Expand Down Expand Up @@ -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
}


Expand Down
7 changes: 6 additions & 1 deletion Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down