Skip to content

Commit 334fc92

Browse files
bpo-38557: Improve documentation for list and tuple C API. (GH-16925)
(cherry picked from commit d898d20) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 91fc9cf commit 334fc92

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

Doc/c-api/list.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ List Objects
7171
7272
.. c:function:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
7373
74-
Set the item at index *index* in list to *item*. Return ``0`` on success
75-
or ``-1`` on failure.
74+
Set the item at index *index* in list to *item*. Return ``0`` on success.
75+
If *index* is out of bounds, return ``-1`` and set an :exc:`IndexError`
76+
exception.
7677
7778
.. note::
7879
@@ -111,17 +112,16 @@ List Objects
111112
112113
Return a list of the objects in *list* containing the objects *between* *low*
113114
and *high*. Return *NULL* and set an exception if unsuccessful. Analogous
114-
to ``list[low:high]``. Negative indices, as when slicing from Python, are not
115-
supported.
115+
to ``list[low:high]``. Indexing from the end of the list is not supported.
116116
117117
118118
.. c:function:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
119119
120120
Set the slice of *list* between *low* and *high* to the contents of
121121
*itemlist*. Analogous to ``list[low:high] = itemlist``. The *itemlist* may
122122
be *NULL*, indicating the assignment of an empty list (slice deletion).
123-
Return ``0`` on success, ``-1`` on failure. Negative indices, as when
124-
slicing from Python, are not supported.
123+
Return ``0`` on success, ``-1`` on failure. Indexing from the end of the
124+
list is not supported.
125125
126126
127127
.. c:function:: int PyList_Sort(PyObject *list)

Doc/c-api/tuple.rst

+12-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Tuple Objects
5757
.. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
5858
5959
Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
60-
out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
60+
out of bounds, return *NULL* and set an :exc:`IndexError` exception.
6161
6262
6363
.. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
@@ -67,18 +67,21 @@ Tuple Objects
6767
6868
.. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
6969
70-
Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
71-
as a new tuple.
70+
Return the slice of the tuple pointed to by *p* between *low* and *high*,
71+
or *NULL* on failure. This is the equivalent of the Python expression
72+
``p[low:high]``. Indexing from the end of the list is not supported.
7273
7374
7475
.. c:function:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
7576
7677
Insert a reference to object *o* at position *pos* of the tuple pointed to by
77-
*p*. Return ``0`` on success.
78+
*p*. Return ``0`` on success. If *pos* is out of bounds, return ``-1``
79+
and set an :exc:`IndexError` exception.
7880
7981
.. note::
8082
81-
This function "steals" a reference to *o*.
83+
This function "steals" a reference to *o* and discards a reference to
84+
an item already in the tuple at the affected position.
8285
8386
8487
.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
@@ -88,7 +91,10 @@ Tuple Objects
8891
8992
.. note::
9093
91-
This function "steals" a reference to *o*.
94+
This macro "steals" a reference to *o*, and, unlike
95+
:c:func:`PyTuple_SetItem`, does *not* discard a reference to any item that
96+
is being replaced; any reference in the tuple at position *pos* will be
97+
leaked.
9298
9399
94100
.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)

Doc/tools/susp-ignored.csv

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ c-api/arg,,:ref,"PyArg_ParseTuple(args, ""O|O:ref"", &object, &callback)"
22
c-api/list,,:high,list[low:high]
33
c-api/sequence,,:i2,del o[i1:i2]
44
c-api/sequence,,:i2,o[i1:i2]
5+
c-api/tuple,,:high,p[low:high]
56
c-api/unicode,,:end,str[start:end]
67
c-api/unicode,,:start,unicode[start:start+length]
78
distutils/examples,267,`,This is the description of the ``foobar`` package.

0 commit comments

Comments
 (0)