Skip to content

Commit

Permalink
pythongh-105927: Add PyWeakref_GetRef() function
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jun 20, 2023
1 parent cb388c9 commit 5841078
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Doc/c-api/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ as much as it can.
``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
.. c:function:: PyObject* PyWeakref_GetRef(PyObject *ref)
Return a :term:`strong reference` to the referenced object from a weak
reference, *ref*. If the referent is no longer live, returns
``NULL``.
.. versionadded:: 3.13
.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
Return the referenced object from a weak reference, *ref*. If the referent is
Expand Down
3 changes: 3 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -2810,6 +2810,9 @@ PyWeakref_GET_OBJECT:PyObject*:ref:0:
PyWeakref_GetObject:PyObject*::0:
PyWeakref_GetObject:PyObject*:ref:0:

PyWeakref_GetRef:PyObject*::+1:
PyWeakref_GetRef:PyObject*:ref:0:

PyWeakref_NewProxy:PyObject*::+1:
PyWeakref_NewProxy:PyObject*:ob:0:
PyWeakref_NewProxy:PyObject*:callback:0:
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ New Features
of a :term:`borrowed reference`.
(Contributed by Victor Stinner in :gh:`105922`.)

* Add :c:func:`PyWeakref_GetRef` function: similar to
:c:func:`PyWeakref_GetObject` but returns a :term:`strong reference`, or
``NULL`` if the referent is no longer live.
(Contributed by Victor Stinner in :gh:`105927`.)

Porting to Python 3.13
----------------------
Expand Down
1 change: 1 addition & 0 deletions Include/weakrefobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
PyObject *callback);
PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
PyAPI_FUNC(PyObject *) PyWeakref_GetRef(PyObject *ref);


#ifndef Py_LIMITED_API
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :c:func:`PyWeakref_GetRef` function: similar to
:c:func:`PyWeakref_GetObject` but returns a :term:`strong reference`, or
``NULL`` if the referent is no longer live. Patch by Victor Stinner.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2430,3 +2430,5 @@
added = '3.12'
[function.PyImport_AddModuleRef]
added = '3.13'
[function.PyWeakref_GetRef]
added = '3.13'
6 changes: 2 additions & 4 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ _in_weak_set(PyObject *set, PyObject *obj)
static PyObject *
_destroy(PyObject *setweakref, PyObject *objweakref)
{
PyObject *set;
set = PyWeakref_GET_OBJECT(setweakref);
if (set == Py_None) {
PyObject *set = PyWeakref_GetRef(setweakref);
if (set == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(set);
if (PySet_Discard(set, objweakref) < 0) {
Py_DECREF(set);
return NULL;
Expand Down
9 changes: 9 additions & 0 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,15 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
}


PyObject*
PyWeakref_GetRef(PyObject *ref)
{
assert(ref != NULL);
assert(PyWeakref_Check(ref) != NULL);
return _PyWeakref_GET_REF(ref);
}


PyObject *
PyWeakref_GetObject(PyObject *ref)
{
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5841078

Please sign in to comment.