Skip to content

Commit b7997c9

Browse files
committed
gh-105927: Add PyWeakref_GetRef() function
1 parent cb388c9 commit b7997c9

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

Doc/c-api/weakref.rst

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ as much as it can.
5151
``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
5252
5353
54+
.. c:function:: PyObject* PyWeakref_GetRef(PyObject *ref)
55+
56+
Return a :term:`strong reference` to the referenced object from a weak
57+
reference, *ref*. If the referent is no longer live, returns
58+
``NULL``.
59+
60+
.. versionadded:: 3.13
61+
62+
5463
.. c:function:: PyObject* PyWeakref_GetObject(PyObject *ref)
5564
5665
Return the referenced object from a weak reference, *ref*. If the referent is

Doc/whatsnew/3.13.rst

+4
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ New Features
431431
of a :term:`borrowed reference`.
432432
(Contributed by Victor Stinner in :gh:`105922`.)
433433

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

435439
Porting to Python 3.13
436440
----------------------

Include/weakrefobject.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
2828
PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
2929
PyObject *callback);
3030
PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
31+
PyAPI_FUNC(PyObject *) PyWeakref_GetRef(PyObject *ref);
3132

3233

3334
#ifndef Py_LIMITED_API
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyWeakref_GetRef` function: similar to
2+
:c:func:`PyWeakref_GetObject` but returns a :term:`strong reference`, or
3+
``NULL`` if the referent is no longer live. Patch by Victor Stinner.

Modules/_abc.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,10 @@ _in_weak_set(PyObject *set, PyObject *obj)
150150
static PyObject *
151151
_destroy(PyObject *setweakref, PyObject *objweakref)
152152
{
153-
PyObject *set;
154-
set = PyWeakref_GET_OBJECT(setweakref);
155-
if (set == Py_None) {
153+
PyObject *set = PyWeakref_GetRef(setweakref);
154+
if (set == NULL) {
156155
Py_RETURN_NONE;
157156
}
158-
Py_INCREF(set);
159157
if (PySet_Discard(set, objweakref) < 0) {
160158
Py_DECREF(set);
161159
return NULL;

Objects/weakrefobject.c

+9
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,15 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
894894
}
895895

896896

897+
PyObject*
898+
PyWeakref_GetRef(PyObject *ref)
899+
{
900+
assert(ref != NULL);
901+
assert(PyWeakref_Check(ref) != NULL);
902+
return _PyWeakref_GET_REF(ref);
903+
}
904+
905+
897906
PyObject *
898907
PyWeakref_GetObject(PyObject *ref)
899908
{

0 commit comments

Comments
 (0)