Skip to content

Commit 9ae5989

Browse files
committed
Add "steal" term to glossary; clarify stealing on error
With one exception, all "stealing" functions also steal on error, but it makes sense to note this in each case.
1 parent 0bb4eca commit 9ae5989

File tree

12 files changed

+56
-37
lines changed

12 files changed

+56
-37
lines changed

Doc/c-api/bytes.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,11 @@ called with a non-bytes parameter.
180180
.. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
181181
182182
Create a new bytes object in *\*bytes* containing the contents of *newpart*
183-
appended to *bytes*; the caller will own the new reference. The reference to
184-
the old value of *bytes* will be stolen. If the new object cannot be
185-
created, the old reference to *bytes* will still be discarded and the value
186-
of *\*bytes* will be set to ``NULL``; the appropriate exception will be set.
183+
appended to *bytes*; the caller will own the new reference.
184+
The reference to the old value of *bytes* will be ":term:`stolen <steal>`".
185+
If the new object cannot be created, the old reference to *bytes* will still
186+
be "stolen", the value of *\*bytes* will be set to ``NULL``, and
187+
the appropriate exception will be set.
187188
188189
189190
.. c:function:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)

Doc/c-api/dict.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ Dictionary Objects
8484
8585
Insert *val* into the dictionary *p* with a key of *key*. *key* must be
8686
:term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return
87-
``0`` on success or ``-1`` on failure. This function *does not* steal a
88-
reference to *val*.
87+
``0`` on success or ``-1`` on failure.
88+
This function *does not* ":term:`steal`" a reference to *val*.
8989
9090
9191
.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)

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
@@ -844,7 +846,7 @@ Exception Objects
844846
845847
Set the context associated with the exception to *ctx*. Use ``NULL`` to clear
846848
it. There is no type check to make sure that *ctx* is an exception instance.
847-
This steals a reference to *ctx*.
849+
This ":term:`steals <steal>`" a reference to *ctx*.
848850
849851
850852
.. c:function:: PyObject* PyException_GetCause(PyObject *ex)
@@ -859,7 +861,8 @@ Exception Objects
859861
860862
Set the cause associated with the exception to *cause*. Use ``NULL`` to clear
861863
it. There is no type check to make sure that *cause* is either an exception
862-
instance or ``None``. This steals a reference to *cause*.
864+
instance or ``None``.
865+
This ":term:`steals <steal>`" a reference to *cause*.
863866
864867
The :attr:`~BaseException.__suppress_context__` attribute is implicitly set
865868
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,15 +35,15 @@ 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
.. c:function:: PyObject* PyGen_NewWithQualName(PyFrameObject *frame, PyObject *name, PyObject *qualname)
4242
4343
Create and return a new generator object based on the *frame* object,
4444
with ``__name__`` and ``__qualname__`` set to *name* and *qualname*.
45-
A reference to *frame* is stolen by this function. The *frame* argument
46-
must not be ``NULL``.
45+
A reference to *frame* is ":term:`stolen <steal>`" by this function (even
46+
on error). The *frame* argument must not be ``NULL``.
4747
4848
4949
.. c:function:: PyCodeObject* PyGen_GetCode(PyGenObject *gen)
@@ -68,8 +68,9 @@ Asynchronous Generator Objects
6868
.. c:function:: PyObject *PyAsyncGen_New(PyFrameObject *frame, PyObject *name, PyObject *qualname)
6969
7070
Create a new asynchronous generator wrapping *frame*, with ``__name__`` and
71-
``__qualname__`` set to *name* and *qualname*. *frame* is stolen by this
72-
function and must not be ``NULL``.
71+
``__qualname__`` set to *name* and *qualname*.
72+
*frame* is ":term:`stolen <steal>`" by this function (even on error) and
73+
must not be ``NULL``.
7374
7475
On success, this function returns a :term:`strong reference` to the
7576
new asynchronous generator. On failure, this function returns ``NULL``

Doc/c-api/init.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,9 +1481,10 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
14811481
.. c:function:: int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
14821482
14831483
Asynchronously raise an exception in a thread. The *id* argument is the thread
1484-
id of the target thread; *exc* is the exception object to be raised. This
1485-
function does not steal any references to *exc*. To prevent naive misuse, you
1486-
must write your own C extension to call this. Must be called with an :term:`attached thread state`.
1484+
id of the target thread; *exc* is the exception object to be raised.
1485+
This function does not :term:`steal` any references to *exc*.
1486+
To prevent naive misuse, you must write your own C extension to call this.
1487+
Must be called with an :term:`attached thread state`.
14871488
Returns the number of thread states modified; this is normally one, but will be
14881489
zero if the thread id isn't found. If *exc* is ``NULL``, the pending
14891490
exception (if any) for the thread is cleared. This raises no exceptions.

Doc/c-api/intro.rst

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

533533
Conversely, when a calling function passes in a reference to an object, there
534534
are two possibilities: the function *steals* a reference to the object, or it
535-
does not. *Stealing a reference* means that when you pass a reference to a
536-
function, that function assumes that it now owns that reference, and you are not
537-
responsible for it any longer.
535+
does not.
536+
537+
*Stealing a reference* means that when you pass a reference to a
538+
function, that function assumes that it now owns that reference.
539+
Since the new owner can use :c:func:`!Py_DECREF` at its discretion,
540+
you (the caller) must not use that reference after the call.
538541

539542
.. index::
540543
single: PyList_SetItem (C function)

Doc/c-api/list.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ List Objects
8888
8989
.. note::
9090
91-
This function "steals" a reference to *item* and discards a reference to
92-
an item already in the list at the affected position.
91+
This function ":term:`steals <steal>`" a reference to *item*,
92+
even on error.
93+
On success, it discards a reference to an item already in the list
94+
at the affected position (unless it was NULL).
9395
9496
9597
.. c:function:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
@@ -103,7 +105,7 @@ List Objects
103105
104106
.. note::
105107
106-
This macro "steals" a reference to *item*, and, unlike
108+
This macro ":term:`steals <steal>`" a reference to *item*, and, unlike
107109
:c:func:`PyList_SetItem`, does *not* discard a reference to any item that
108110
is being replaced; any reference in *list* at position *i* will be
109111
leaked.

Doc/c-api/module.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ or code that creates modules dynamically.
900900
901901
.. c:function:: int PyModule_Add(PyObject *module, const char *name, PyObject *value)
902902
903-
Similar to :c:func:`PyModule_AddObjectRef`, but "steals" a reference
904-
to *value*.
903+
Similar to :c:func:`PyModule_AddObjectRef`, but ":term:`steals <steal>`"
904+
a reference to *value* (even on error).
905905
It can be called with a result of function that returns a new reference
906906
without bothering to check its result or even saving it to a variable.
907907
@@ -916,8 +916,8 @@ or code that creates modules dynamically.
916916
917917
.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
918918
919-
Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to
920-
*value* on success (if it returns ``0``).
919+
Similar to :c:func:`PyModule_AddObjectRef`, but :term:`steals <steal>`
920+
a reference to *value* on success (if it returns ``0``).
921921
922922
The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef`
923923
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/tuple.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ Tuple Objects
103103
104104
.. note::
105105
106-
This function "steals" a reference to *o* and discards a reference to
107-
an item already in the tuple at the affected position.
106+
This function ":term:`steals <steal>`" a reference to *o* and discards
107+
a reference to an item already in the tuple at the affected position
108+
(unless it was NULL).
108109
109110
110111
.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
@@ -117,7 +118,7 @@ Tuple Objects
117118
118119
.. note::
119120
120-
This function "steals" a reference to *o*, and, unlike
121+
This function ":term:`steals <steal>`" a reference to *o*, and, unlike
121122
:c:func:`PyTuple_SetItem`, does *not* discard a reference to any item that
122123
is being replaced; any reference in the tuple at position *pos* will be
123124
leaked.
@@ -260,7 +261,7 @@ type.
260261
261262
.. note::
262263
263-
This function "steals" a reference to *o*.
264+
This function ":term:`steals <steal>`" a reference to *o*.
264265
265266
266267
.. c:function:: void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)

0 commit comments

Comments
 (0)