Skip to content

Commit 5841078

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

File tree

11 files changed

+36
-4
lines changed

11 files changed

+36
-4
lines changed

Doc/c-api/weakref.rst

Lines changed: 9 additions & 0 deletions
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/data/refcounts.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,9 @@ PyWeakref_GET_OBJECT:PyObject*:ref:0:
28102810
PyWeakref_GetObject:PyObject*::0:
28112811
PyWeakref_GetObject:PyObject*:ref:0:
28122812

2813+
PyWeakref_GetRef:PyObject*::+1:
2814+
PyWeakref_GetRef:PyObject*:ref:0:
2815+
28132816
PyWeakref_NewProxy:PyObject*::+1:
28142817
PyWeakref_NewProxy:PyObject*:ob:0:
28152818
PyWeakref_NewProxy:PyObject*:callback:0:

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/whatsnew/3.13.rst

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

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

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
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.

Misc/stable_abi.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,3 +2430,5 @@
24302430
added = '3.12'
24312431
[function.PyImport_AddModuleRef]
24322432
added = '3.13'
2433+
[function.PyWeakref_GetRef]
2434+
added = '3.13'

Modules/_abc.c

Lines changed: 2 additions & 4 deletions
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

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

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)