Skip to content

Commit 23c5f93

Browse files
authored
bpo-42294: Add borrowed/strong reference to doc glossary (GH-23206)
Add "borrowed reference" and "strong reference" to the documentation glossary. Enhance also Py_INCREF() and Py_NewRef() documentation.
1 parent a117167 commit 23c5f93

11 files changed

+72
-22
lines changed

Doc/c-api/arg.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ API Functions
482482
*min* and no more than *max*; *min* and *max* may be equal. Additional
483483
arguments must be passed to the function, each of which should be a pointer to a
484484
:c:type:`PyObject*` variable; these will be filled in with the values from
485-
*args*; they will contain borrowed references. The variables which correspond
485+
*args*; they will contain :term:`borrowed references <borrowed reference>`.
486+
The variables which correspond
486487
to optional parameters not given by *args* will not be filled in; these should
487488
be initialized by the caller. This function returns true on success and false if
488489
*args* is not a tuple or contains the wrong number of elements; an exception

Doc/c-api/init.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
10771077
10781078
Get the current frame of the Python thread state *tstate*.
10791079
1080-
Return a strong reference. Return ``NULL`` if no frame is currently
1080+
Return a :term:`strong reference`. Return ``NULL`` if no frame is currently
10811081
executing.
10821082
10831083
See also :c:func:`PyEval_GetFrame`.

Doc/c-api/intro.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ when it's no longer needed---or passing on this responsibility (usually to its
326326
caller). When a function passes ownership of a reference on to its caller, the
327327
caller is said to receive a *new* reference. When no ownership is transferred,
328328
the caller is said to *borrow* the reference. Nothing needs to be done for a
329-
borrowed reference.
329+
:term:`borrowed reference`.
330330

331331
Conversely, when a calling function passes in a reference to an object, there
332332
are two possibilities: the function *steals* a reference to the object, or it

Doc/c-api/refcounting.rst

+26-9
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ objects.
1313

1414
.. c:function:: void Py_INCREF(PyObject *o)
1515
16-
Increment the reference count for object *o*. The object must not be ``NULL``; if
17-
you aren't sure that it isn't ``NULL``, use :c:func:`Py_XINCREF`.
16+
Increment the reference count for object *o*.
1817
19-
See also :c:func:`Py_NewRef`.
18+
This function is usually used to convert a :term:`borrowed reference` to a
19+
:term:`strong reference` in-place. The :c:func:`Py_NewRef` function can be
20+
used to create a new :term:`strong reference`.
21+
22+
The object must not be ``NULL``; if you aren't sure that it isn't
23+
``NULL``, use :c:func:`Py_XINCREF`.
2024
2125
2226
.. c:function:: void Py_XINCREF(PyObject *o)
@@ -29,9 +33,14 @@ objects.
2933
3034
.. c:function:: PyObject* Py_NewRef(PyObject *o)
3135
32-
Increment the reference count of the object *o* and return the object *o*.
36+
Create a new :term:`strong reference` to an object: increment the reference
37+
count of the object *o* and return the object *o*.
38+
39+
When the :term:`strong reference` is no longer needed, :c:func:`Py_DECREF`
40+
should be called on it to decrement the object reference count.
3341
34-
The object *o* must not be ``NULL``.
42+
The object *o* must not be ``NULL``; use :c:func:`Py_XNewRef` if *o* can be
43+
``NULL``.
3544
3645
For example::
3746
@@ -42,6 +51,8 @@ objects.
4251
4352
self->attr = Py_NewRef(obj);
4453
54+
See also :c:func:`Py_INCREF`.
55+
4556
.. versionadded:: 3.10
4657
4758
@@ -56,10 +67,16 @@ objects.
5667
5768
.. c:function:: void Py_DECREF(PyObject *o)
5869
59-
Decrement the reference count for object *o*. The object must not be ``NULL``; if
60-
you aren't sure that it isn't ``NULL``, use :c:func:`Py_XDECREF`. If the reference
61-
count reaches zero, the object's type's deallocation function (which must not be
62-
``NULL``) is invoked.
70+
Decrement the reference count for object *o*.
71+
72+
If the reference count reaches zero, the object's type's deallocation
73+
function (which must not be ``NULL``) is invoked.
74+
75+
This function is usually used to delete a :term:`strong reference` before
76+
exiting its scope.
77+
78+
The object must not be ``NULL``; if you aren't sure that it isn't ``NULL``,
79+
use :c:func:`Py_XDECREF`.
6380
6481
.. warning::
6582

Doc/c-api/reflection.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Reflection
3535
3636
Get the *frame* next outer frame.
3737
38-
Return a strong reference, or ``NULL`` if *frame* has no outer frame.
38+
Return a :term:`strong reference`, or ``NULL`` if *frame* has no outer frame.
3939
4040
*frame* must not be ``NULL``.
4141
@@ -46,7 +46,7 @@ Reflection
4646
4747
Get the *frame* code.
4848
49-
Return a strong reference.
49+
Return a :term:`strong reference`.
5050
5151
*frame* must not be ``NULL``. The result (frame code) cannot be ``NULL``.
5252

Doc/c-api/structures.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ the definition of all other Python objects.
6666
6767
Get the type of the Python object *o*.
6868
69-
Return a borrowed reference.
69+
Return a :term:`borrowed reference`.
7070
7171
.. versionchanged:: 3.10
7272
:c:func:`Py_TYPE()` is changed to the inline static function.

Doc/c-api/typeobj.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,9 @@ and :c:type:`PyType_Type` effectively act as defaults.)
12131213
:func:`~gc.get_referents` function will include it.
12141214

12151215
.. warning::
1216-
When implementing :c:member:`~PyTypeObject.tp_traverse`, only the members
1217-
that the instance *owns* (by having strong references to them) must be
1216+
When implementing :c:member:`~PyTypeObject.tp_traverse`, only the
1217+
members that the instance *owns* (by having :term:`strong references
1218+
<strong reference>` to them) must be
12181219
visited. For instance, if an object supports weak references via the
12191220
:c:member:`~PyTypeObject.tp_weaklist` slot, the pointer supporting
12201221
the linked list (what *tp_weaklist* points to) must **not** be

Doc/c-api/weakref.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ as much as it can.
5757
5858
.. note::
5959
60-
This function returns a **borrowed reference** to the referenced object.
60+
This function returns a :term:`borrowed reference` to the referenced object.
6161
This means that you should always call :c:func:`Py_INCREF` on the object
62-
except if you know that it cannot be destroyed while you are still
63-
using it.
62+
except it cannot be destroyed before the last usage of the borrowed
63+
reference.
6464
6565
6666
.. c:function:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)

Doc/data/refcounts.dat

+6
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,9 @@ Py_GetVersion:const char*:::
30073007
Py_INCREF:void:::
30083008
Py_INCREF:PyObject*:o:+1:
30093009

3010+
Py_NewRef:void:::
3011+
Py_NewRef:PyObject*:o:+1:
3012+
30103013
Py_Initialize:void:::
30113014

30123015
Py_IsInitialized:int:::
@@ -3028,6 +3031,9 @@ Py_XDECREF:PyObject*:o:-1:if o is not NULL
30283031
Py_XINCREF:void:::
30293032
Py_XINCREF:PyObject*:o:+1:if o is not NULL
30303033

3034+
Py_XNewRef:void:::
3035+
Py_XNewRef:PyObject*:o:+1:if o is not NULL
3036+
30313037
_PyImport_Fini:void:::
30323038

30333039
_PyObject_New:PyObject*::+1:

Doc/glossary.rst

+24
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ Glossary
158158
See also :term:`text file` for a file object able to read and write
159159
:class:`str` objects.
160160

161+
borrowed reference
162+
In the Python's C API, a borrowed reference is a reference to an object.
163+
It does not modify the object reference count. It becomes a dangling
164+
pointer if the object is destroyed. For example, a garbage collection can
165+
remove the last :term:`strong reference` to the object and so destroy it.
166+
167+
Calling :c:func:`Py_INCREF` on the :term:`borrowed reference` is
168+
recommended to convert it to a :term:`strong reference` in-place, except
169+
if the object cannot be destroyed before the last usage of the borrowed
170+
reference. The :c:func:`Py_NewRef` function can be used to create a new
171+
:term:`strong reference`.
172+
161173
bytes-like object
162174
An object that supports the :ref:`bufferobjects` and can
163175
export a C-:term:`contiguous` buffer. This includes all :class:`bytes`,
@@ -1100,6 +1112,18 @@ Glossary
11001112
an :term:`expression` or one of several constructs with a keyword, such
11011113
as :keyword:`if`, :keyword:`while` or :keyword:`for`.
11021114

1115+
strong reference
1116+
In the Python's C API, a strong reference is a reference to an object
1117+
which increments object reference count when it is created and
1118+
decrements the object reference count when it is deleted.
1119+
1120+
The :c:func:`Py_NewRef` function can be used to create a strong reference
1121+
to an object. Usually, the :c:func:`Py_DECREF` function must be called on
1122+
the strong reference before exiting the scope of the strong reference, to
1123+
avoid leaking one reference.
1124+
1125+
See also :term:`borrowed reference`.
1126+
11031127
text encoding
11041128
A codec which encodes Unicode strings to bytes.
11051129

Include/object.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,11 @@ they can have object code that is not dependent on Python compilation flags.
526526
PyAPI_FUNC(void) Py_IncRef(PyObject *);
527527
PyAPI_FUNC(void) Py_DecRef(PyObject *);
528528

529-
// Increment the reference count of the object and return the object.
529+
// Create a new strong reference to an object:
530+
// increment the reference count of the object and return the object.
530531
PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
531532

532-
// Similar to Py_NewRef() but the object can be NULL.
533+
// Similar to Py_NewRef(), but the object can be NULL.
533534
PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
534535

535536
static inline PyObject* _Py_NewRef(PyObject *obj)

0 commit comments

Comments
 (0)