Skip to content

Commit 57b7dbc

Browse files
mattipbenjaminp
authored andcommitted
Emphasize the need to always call PySequence_Fast. (GH-11140)
1 parent 088b63e commit 57b7dbc

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Doc/c-api/sequence.rst

+14-7
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,26 @@ Sequence Protocol
130130
131131
.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
132132
133-
Return the sequence or iterable *o* as a list, unless it is already a tuple or list, in
134-
which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access
135-
the members of the result. Returns *NULL* on failure. If the object is not
136-
a sequence or iterable, raises :exc:`TypeError` with *m* as the message text.
133+
Return the sequence or iterable *o* as an object usable by the other
134+
``PySequence_Fast*`` family of functions. If the object is not a sequence or
135+
iterable, raises :exc:`TypeError` with *m* as the message text. Returns
136+
*NULL* on failure.
137+
138+
The ``PySequence_Fast*`` functions are thus named because they assume
139+
*o* is a :c:type:`PyTupleObject` or a :c:type:`PyListObject` and access
140+
the data fields of *o* directly.
141+
142+
As a CPython implementation detail, if *o* is already a sequence or list, it
143+
will be returned.
137144
138145
139146
.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
140147
141148
Returns the length of *o*, assuming that *o* was returned by
142149
:c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
143150
gotten by calling :c:func:`PySequence_Size` on *o*, but
144-
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
145-
or tuple.
151+
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a
152+
list or tuple.
146153
147154
148155
.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
@@ -163,7 +170,7 @@ Sequence Protocol
163170
164171
.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
165172
166-
Return the *i*\ th element of *o* or *NULL* on failure. Macro form of
173+
Return the *i*\ th element of *o* or *NULL* on failure. Faster form of
167174
:c:func:`PySequence_GetItem` but without checking that
168175
:c:func:`PySequence_Check` on *o* is true and without adjustment for negative
169176
indices.

0 commit comments

Comments
 (0)