| 1 |
.. highlight:: c |
| 2 |
|
| 3 |
.. _sequence: |
| 4 |
|
| 5 |
Sequence Protocol |
| 6 |
================= |
| 7 |
|
| 8 |
|
| 9 |
.. c:function:: int PySequence_Check(PyObject *o) |
| 10 |
|
| 11 |
Return ``1`` if the object provides the sequence protocol, and ``0`` otherwise. |
| 12 |
Note that it returns ``1`` for Python classes with a :meth:`~object.__getitem__` |
| 13 |
method, unless they are :class:`dict` subclasses, since in general it |
| 14 |
is impossible to determine what type of keys the class supports. This |
| 15 |
function always succeeds. |
| 16 |
|
| 17 |
|
| 18 |
.. c:function:: Py_ssize_t PySequence_Size(PyObject *o) |
| 19 |
Py_ssize_t PySequence_Length(PyObject *o) |
| 20 |
|
| 21 |
.. index:: pair: built-in function; len |
| 22 |
|
| 23 |
Returns the number of objects in sequence *o* on success, and ``-1`` on |
| 24 |
failure. This is equivalent to the Python expression ``len(o)``. |
| 25 |
|
| 26 |
|
| 27 |
.. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2) |
| 28 |
|
| 29 |
Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure. |
| 30 |
This is the equivalent of the Python expression ``o1 + o2``. |
| 31 |
|
| 32 |
|
| 33 |
.. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count) |
| 34 |
|
| 35 |
Return the result of repeating sequence object *o* *count* times, or ``NULL`` on |
| 36 |
failure. This is the equivalent of the Python expression ``o * count``. |
| 37 |
|
| 38 |
|
| 39 |
.. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2) |
| 40 |
|
| 41 |
Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure. |
| 42 |
The operation is done *in-place* when *o1* supports it. This is the equivalent |
| 43 |
of the Python expression ``o1 += o2``. |
| 44 |
|
| 45 |
|
| 46 |
.. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
| 47 |
|
| 48 |
Return the result of repeating sequence object *o* *count* times, or ``NULL`` on |
| 49 |
failure. The operation is done *in-place* when *o* supports it. This is the |
| 50 |
equivalent of the Python expression ``o *= count``. |
| 51 |
|
| 52 |
|
| 53 |
.. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i) |
| 54 |
|
| 55 |
Return the *i*\ th element of *o*, or ``NULL`` on failure. This is the equivalent of |
| 56 |
the Python expression ``o[i]``. |
| 57 |
|
| 58 |
|
| 59 |
.. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) |
| 60 |
|
| 61 |
Return the slice of sequence object *o* between *i1* and *i2*, or ``NULL`` on |
| 62 |
failure. This is the equivalent of the Python expression ``o[i1:i2]``. |
| 63 |
|
| 64 |
|
| 65 |
.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v) |
| 66 |
|
| 67 |
Assign object *v* to the *i*\ th element of *o*. Raise an exception |
| 68 |
and return ``-1`` on failure; return ``0`` on success. This |
| 69 |
is the equivalent of the Python statement ``o[i] = v``. This function *does |
| 70 |
not* steal a reference to *v*. |
| 71 |
|
| 72 |
If *v* is ``NULL``, the element is deleted, but this feature is |
| 73 |
deprecated in favour of using :c:func:`PySequence_DelItem`. |
| 74 |
|
| 75 |
|
| 76 |
.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i) |
| 77 |
|
| 78 |
Delete the *i*\ th element of object *o*. Returns ``-1`` on failure. This is the |
| 79 |
equivalent of the Python statement ``del o[i]``. |
| 80 |
|
| 81 |
|
| 82 |
.. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v) |
| 83 |
|
| 84 |
Assign the sequence object *v* to the slice in sequence object *o* from *i1* to |
| 85 |
*i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``. |
| 86 |
|
| 87 |
|
| 88 |
.. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) |
| 89 |
|
| 90 |
Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on |
| 91 |
failure. This is the equivalent of the Python statement ``del o[i1:i2]``. |
| 92 |
|
| 93 |
|
| 94 |
.. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value) |
| 95 |
|
| 96 |
Return the number of occurrences of *value* in *o*, that is, return the number |
| 97 |
of keys for which ``o[key] == value``. On failure, return ``-1``. This is |
| 98 |
equivalent to the Python expression ``o.count(value)``. |
| 99 |
|
| 100 |
|
| 101 |
.. c:function:: int PySequence_Contains(PyObject *o, PyObject *value) |
| 102 |
|
| 103 |
Determine if *o* contains *value*. If an item in *o* is equal to *value*, |
| 104 |
return ``1``, otherwise return ``0``. On error, return ``-1``. This is |
| 105 |
equivalent to the Python expression ``value in o``. |
| 106 |
|
| 107 |
|
| 108 |
.. c:function:: int PySequence_In(PyObject *o, PyObject *value) |
| 109 |
|
| 110 |
Alias for :c:func:`PySequence_Contains`. |
| 111 |
|
| 112 |
.. soft-deprecated:: 3.14 |
| 113 |
The function should no longer be used to write new code. |
| 114 |
|
| 115 |
|
| 116 |
.. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value) |
| 117 |
|
| 118 |
Return the first index *i* for which ``o[i] == value``. On error, return |
| 119 |
``-1``. This is equivalent to the Python expression ``o.index(value)``. |
| 120 |
|
| 121 |
|
| 122 |
.. c:function:: PyObject* PySequence_List(PyObject *o) |
| 123 |
|
| 124 |
Return a list object with the same contents as the sequence or iterable *o*, |
| 125 |
or ``NULL`` on failure. The returned list is guaranteed to be new. This is |
| 126 |
equivalent to the Python expression ``list(o)``. |
| 127 |
|
| 128 |
|
| 129 |
.. c:function:: PyObject* PySequence_Tuple(PyObject *o) |
| 130 |
|
| 131 |
.. index:: pair: built-in function; tuple |
| 132 |
|
| 133 |
Return a tuple object with the same contents as the sequence or iterable *o*, |
| 134 |
or ``NULL`` on failure. If *o* is a tuple, a new reference will be returned, |
| 135 |
otherwise a tuple will be constructed with the appropriate contents. This is |
| 136 |
equivalent to the Python expression ``tuple(o)``. |
| 137 |
|
| 138 |
|
| 139 |
.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m) |
| 140 |
|
| 141 |
Return the sequence or iterable *o* as an object usable by the other |
| 142 |
``PySequence_Fast*`` family of functions. If the object is not a sequence or |
| 143 |
iterable, raises :exc:`TypeError` with *m* as the message text. Returns |
| 144 |
``NULL`` on failure. |
| 145 |
|
| 146 |
The ``PySequence_Fast*`` functions are thus named because they assume |
| 147 |
*o* is a :c:type:`PyTupleObject` or a :c:type:`PyListObject` and access |
| 148 |
the data fields of *o* directly. |
| 149 |
|
| 150 |
As a CPython implementation detail, if *o* is already a sequence or list, it |
| 151 |
will be returned. |
| 152 |
|
| 153 |
|
| 154 |
.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o) |
| 155 |
|
| 156 |
Returns the length of *o*, assuming that *o* was returned by |
| 157 |
:c:func:`PySequence_Fast` and that *o* is not ``NULL``. The size can also be |
| 158 |
retrieved by calling :c:func:`PySequence_Size` on *o*, but |
| 159 |
:c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a |
| 160 |
list or tuple. |
| 161 |
|
| 162 |
|
| 163 |
.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i) |
| 164 |
|
| 165 |
Return the *i*\ th element of *o*, assuming that *o* was returned by |
| 166 |
:c:func:`PySequence_Fast`, *o* is not ``NULL``, and that *i* is within bounds. |
| 167 |
|
| 168 |
|
| 169 |
.. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o) |
| 170 |
|
| 171 |
Return the underlying array of PyObject pointers. Assumes that *o* was returned |
| 172 |
by :c:func:`PySequence_Fast` and *o* is not ``NULL``. |
| 173 |
|
| 174 |
Note, if a list gets resized, the reallocation may relocate the items array. |
| 175 |
So, only use the underlying array pointer in contexts where the sequence |
| 176 |
cannot change. |
| 177 |
|
| 178 |
|
| 179 |
.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i) |
| 180 |
|
| 181 |
Return the *i*\ th element of *o* or ``NULL`` on failure. Faster form of |
| 182 |
:c:func:`PySequence_GetItem` but without checking that |
| 183 |
:c:func:`PySequence_Check` on *o* is true and without adjustment for negative |
| 184 |
indices. |