Skip to content

Commit 34503f3

Browse files
gh-144473: Add "steal" term to glossary; clarify "stealing" on error (GH-144502)
With one exception, all "stealing" functions also steal on error, but it makes sense to note this in each case. Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent 1402ac7 commit 34503f3

12 files changed

Lines changed: 55 additions & 35 deletions

File tree

Doc/c-api/bytes.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ called with a non-bytes parameter.
184184
.. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
185185
186186
Create a new bytes object in *\*bytes* containing the contents of *newpart*
187-
appended to *bytes*; the caller will own the new reference. The reference to
188-
the old value of *bytes* will be stolen. If the new object cannot be
189-
created, the old reference to *bytes* will still be discarded and the value
190-
of *\*bytes* will be set to ``NULL``; the appropriate exception will be set.
187+
appended to *bytes*; the caller will own the new reference.
188+
The reference to the old value of *bytes* will be ":term:`stolen <steal>`".
189+
If the new object cannot be created, the old reference to *bytes* will still
190+
be "stolen", the value of *\*bytes* will be set to ``NULL``, and
191+
the appropriate exception will be set.
191192
192193
.. note::
193194
If *newpart* implements the buffer protocol, then the buffer

Doc/c-api/dict.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ Dictionary objects
107107
108108
Insert *val* into the dictionary *p* with a key of *key*. *key* must be
109109
:term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return
110-
``0`` on success or ``-1`` on failure. This function *does not* steal a
111-
reference to *val*.
110+
``0`` on success or ``-1`` on failure.
111+
This function *does not* ":term:`steal`" a reference to *val*.
112112
113113
.. note::
114114

Doc/c-api/exceptions.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ Querying the error indicator
503503
504504
.. warning::
505505
506-
This call steals a reference to *exc*, which must be a valid exception.
506+
This call ":term:`steals <steal>`" a reference to *exc*,
507+
which must be a valid exception.
507508
508509
.. versionadded:: 3.12
509510
@@ -641,7 +642,8 @@ Querying the error indicator
641642
642643
Set the exception info, as known from ``sys.exc_info()``. This refers
643644
to an exception that was *already caught*, not to an exception that was
644-
freshly raised. This function steals the references of the arguments.
645+
freshly raised. This function ":term:`steals <steal>`" the references
646+
of the arguments.
645647
To clear the exception state, pass ``NULL`` for all three arguments.
646648
This function is kept for backwards compatibility. Prefer using
647649
:c:func:`PyErr_SetHandledException`.
@@ -658,8 +660,8 @@ Querying the error indicator
658660
.. versionchanged:: 3.11
659661
The ``type`` and ``traceback`` arguments are no longer used and
660662
can be NULL. The interpreter now derives them from the exception
661-
instance (the ``value`` argument). The function still steals
662-
references of all three arguments.
663+
instance (the ``value`` argument). The function still
664+
":term:`steals <steal>`" references of all three arguments.
663665
664666
665667
Signal Handling
@@ -869,7 +871,7 @@ Exception Objects
869871
870872
Set the context associated with the exception to *ctx*. Use ``NULL`` to clear
871873
it. There is no type check to make sure that *ctx* is an exception instance.
872-
This steals a reference to *ctx*.
874+
This ":term:`steals <steal>`" a reference to *ctx*.
873875
874876
875877
.. c:function:: PyObject* PyException_GetCause(PyObject *ex)
@@ -884,7 +886,8 @@ Exception Objects
884886
885887
Set the cause associated with the exception to *cause*. Use ``NULL`` to clear
886888
it. There is no type check to make sure that *cause* is either an exception
887-
instance or ``None``. This steals a reference to *cause*.
889+
instance or ``None``.
890+
This ":term:`steals <steal>`" a reference to *cause*.
888891
889892
The :attr:`~BaseException.__suppress_context__` attribute is implicitly set
890893
to ``True`` by this function.

Doc/c-api/gen.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ than explicitly calling :c:func:`PyGen_New` or :c:func:`PyGen_NewWithQualName`.
3535
.. c:function:: PyObject* PyGen_New(PyFrameObject *frame)
3636
3737
Create and return a new generator object based on the *frame* object.
38-
A reference to *frame* is stolen by this function. The argument must not be
39-
``NULL``.
38+
A reference to *frame* is ":term:`stolen <steal>`" by this function (even
39+
on error). The argument must not be ``NULL``.
4040
4141
.. deprecated-removed:: 3.16 3.18
4242
@@ -48,8 +48,8 @@ than explicitly calling :c:func:`PyGen_New` or :c:func:`PyGen_NewWithQualName`.
4848
4949
Create and return a new generator object based on the *frame* object,
5050
with ``__name__`` and ``__qualname__`` set to *name* and *qualname*.
51-
A reference to *frame* is stolen by this function. The *frame* argument
52-
must not be ``NULL``.
51+
A reference to *frame* is ":term:`stolen <steal>`" by this function (even
52+
on error). The *frame* argument must not be ``NULL``.
5353
5454
.. deprecated-removed:: 3.16 3.18
5555
@@ -80,8 +80,9 @@ Asynchronous Generator Objects
8080
.. c:function:: PyObject *PyAsyncGen_New(PyFrameObject *frame, PyObject *name, PyObject *qualname)
8181
8282
Create a new asynchronous generator wrapping *frame*, with ``__name__`` and
83-
``__qualname__`` set to *name* and *qualname*. *frame* is stolen by this
84-
function and must not be ``NULL``.
83+
``__qualname__`` set to *name* and *qualname*.
84+
*frame* is ":term:`stolen <steal>`" by this function (even on error) and
85+
must not be ``NULL``.
8586
8687
On success, this function returns a :term:`strong reference` to the
8788
new asynchronous generator. On failure, this function returns ``NULL``

Doc/c-api/intro.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,12 @@ the caller is said to *borrow* the reference. Nothing needs to be done for a
737737

738738
Conversely, when a calling function passes in a reference to an object, there
739739
are two possibilities: the function *steals* a reference to the object, or it
740-
does not. *Stealing a reference* means that when you pass a reference to a
741-
function, that function assumes that it now owns that reference, and you are not
742-
responsible for it any longer.
740+
does not.
741+
742+
*Stealing a reference* means that when you pass a reference to a
743+
function, that function assumes that it now owns that reference.
744+
Since the new owner can use :c:func:`!Py_DECREF` at its discretion,
745+
you (the caller) must not use that reference after the call.
743746

744747
.. index::
745748
single: PyList_SetItem (C function)

Doc/c-api/list.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ List Objects
102102
103103
.. note::
104104
105-
This function "steals" a reference to *item* and discards a reference to
106-
an item already in the list at the affected position.
105+
This function ":term:`steals <steal>`" a reference to *item*,
106+
even on error.
107+
On success, it discards a reference to an item already in the list
108+
at the affected position (unless it was ``NULL``).
107109
108110
109111
.. c:function:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
@@ -117,7 +119,7 @@ List Objects
117119
118120
.. note::
119121
120-
This macro "steals" a reference to *item*, and, unlike
122+
This macro ":term:`steals <steal>`" a reference to *item*, and, unlike
121123
:c:func:`PyList_SetItem`, does *not* discard a reference to any item that
122124
is being replaced; any reference in *list* at position *i* will be
123125
leaked.

Doc/c-api/module.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,8 @@ or code that creates modules dynamically.
974974
975975
.. c:function:: int PyModule_Add(PyObject *module, const char *name, PyObject *value)
976976
977-
Similar to :c:func:`PyModule_AddObjectRef`, but "steals" a reference
978-
to *value*.
977+
Similar to :c:func:`PyModule_AddObjectRef`, but ":term:`steals <steal>`"
978+
a reference to *value* (even on error).
979979
It can be called with a result of function that returns a new reference
980980
without bothering to check its result or even saving it to a variable.
981981
@@ -990,8 +990,8 @@ or code that creates modules dynamically.
990990
991991
.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
992992
993-
Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to
994-
*value* on success (if it returns ``0``).
993+
Similar to :c:func:`PyModule_AddObjectRef`, but :term:`steals <steal>`
994+
a reference to *value* on success (if it returns ``0``).
995995
996996
The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef`
997997
functions are recommended, since it is

Doc/c-api/sequence.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Sequence Protocol
6767
Assign object *v* to the *i*\ th element of *o*. Raise an exception
6868
and return ``-1`` on failure; return ``0`` on success. This
6969
is the equivalent of the Python statement ``o[i] = v``. This function *does
70-
not* steal a reference to *v*.
70+
not* ":term:`steal`" a reference to *v*.
7171
7272
If *v* is ``NULL``, the element is deleted, but this feature is
7373
deprecated in favour of using :c:func:`PySequence_DelItem`.

Doc/c-api/threads.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ pointer and a void pointer argument.
924924
925925
To prevent naive misuse, you must write your own C extension to call this.
926926
This function must be called with an :term:`attached thread state`.
927-
This function does not steal any references to *exc*.
927+
This function does not :term:`steal` any references to *exc*.
928928
This function does not necessarily interrupt system calls such as
929929
:py:func:`~time.sleep`.
930930

Doc/c-api/tuple.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ Tuple Objects
104104
105105
.. note::
106106
107-
This function "steals" a reference to *o* and discards a reference to
108-
an item already in the tuple at the affected position.
107+
This function ":term:`steals <steal>`" a reference to *o* and discards
108+
a reference to an item already in the tuple at the affected position
109+
(unless it was NULL).
109110
110111
111112
.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
@@ -118,7 +119,7 @@ Tuple Objects
118119
119120
.. note::
120121
121-
This function "steals" a reference to *o*, and, unlike
122+
This function ":term:`steals <steal>`" a reference to *o*, and, unlike
122123
:c:func:`PyTuple_SetItem`, does *not* discard a reference to any item that
123124
is being replaced; any reference in the tuple at position *pos* will be
124125
leaked.
@@ -263,7 +264,7 @@ type.
263264
264265
.. note::
265266
266-
This function "steals" a reference to *o*.
267+
This function ":term:`steals <steal>`" a reference to *o*.
267268
268269
269270
.. c:function:: void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)

0 commit comments

Comments
 (0)