Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _sequence: |
| 4 | |
| 5 | Sequence Protocol |
| 6 | ================= |
| 7 | |
| 8 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 9 | .. c:function:: int PySequence_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 10 | |
| 11 | Return ``1`` if the object provides sequence protocol, and ``0`` otherwise. |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 12 | Note that it returns ``1`` for Python classes with a :meth:`__getitem__` |
| 13 | method unless they are :class:`dict` subclasses since in general case it |
| 14 | is impossible to determine what the type of keys it supports. This |
| 15 | function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 16 | |
| 17 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | .. c:function:: Py_ssize_t PySequence_Size(PyObject *o) |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 19 | Py_ssize_t PySequence_Length(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 20 | |
| 21 | .. index:: builtin: len |
| 22 | |
Zackery Spytz | 7a1e178 | 2018-02-20 10:24:29 -0700 | [diff] [blame] | 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)``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 27 | .. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 29 | Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 30 | This is the equivalent of the Python expression ``o1 + o2``. |
| 31 | |
| 32 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 33 | .. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 34 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 35 | Return the result of repeating sequence object *o* *count* times, or ``NULL`` on |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 36 | failure. This is the equivalent of the Python expression ``o * count``. |
| 37 | |
| 38 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 39 | .. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 41 | Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | The operation is done *in-place* when *o1* supports it. This is the equivalent |
| 43 | of the Python expression ``o1 += o2``. |
| 44 | |
| 45 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 46 | .. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 47 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 48 | Return the result of repeating sequence object *o* *count* times, or ``NULL`` on |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 53 | .. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 54 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 55 | Return the *i*\ th element of *o*, or ``NULL`` on failure. This is the equivalent of |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 56 | the Python expression ``o[i]``. |
| 57 | |
| 58 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | .. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 60 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 61 | Return the slice of sequence object *o* between *i1* and *i2*, or ``NULL`` on |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 62 | failure. This is the equivalent of the Python expression ``o[i1:i2]``. |
| 63 | |
| 64 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 65 | .. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 66 | |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 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 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 | is the equivalent of the Python statement ``o[i] = v``. This function *does |
| 70 | not* steal a reference to *v*. |
| 71 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 72 | If *v* is ``NULL``, the element is deleted, however this feature is |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 73 | deprecated in favour of using :c:func:`PySequence_DelItem`. |
| 74 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 78 | Delete the *i*\ th element of object *o*. Returns ``-1`` on failure. This is the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 79 | equivalent of the Python statement ``del o[i]``. |
| 80 | |
| 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | .. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 94 | .. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 101 | .. c:function:: int PySequence_Contains(PyObject *o, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 108 | .. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 109 | |
| 110 | Return the first index *i* for which ``o[i] == value``. On error, return |
| 111 | ``-1``. This is equivalent to the Python expression ``o.index(value)``. |
| 112 | |
| 113 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 114 | .. c:function:: PyObject* PySequence_List(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 115 | |
Berker Peksag | 3279995 | 2015-03-13 02:55:45 +0200 | [diff] [blame] | 116 | Return a list object with the same contents as the sequence or iterable *o*, |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 117 | or ``NULL`` on failure. The returned list is guaranteed to be new. This is |
Berker Peksag | 3279995 | 2015-03-13 02:55:45 +0200 | [diff] [blame] | 118 | equivalent to the Python expression ``list(o)``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 119 | |
| 120 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 121 | .. c:function:: PyObject* PySequence_Tuple(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 122 | |
| 123 | .. index:: builtin: tuple |
| 124 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 125 | Return a tuple object with the same contents as the sequence or iterable *o*, |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 126 | or ``NULL`` on failure. If *o* is a tuple, a new reference will be returned, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 | otherwise a tuple will be constructed with the appropriate contents. This is |
| 128 | equivalent to the Python expression ``tuple(o)``. |
| 129 | |
| 130 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 131 | .. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 132 | |
Matti Picus | 57b7dbc | 2019-09-12 19:19:06 +0300 | [diff] [blame] | 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 |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 136 | ``NULL`` on failure. |
Matti Picus | 57b7dbc | 2019-09-12 19:19:06 +0300 | [diff] [blame] | 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. |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 144 | |
| 145 | |
| 146 | .. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o) |
| 147 | |
| 148 | Returns the length of *o*, assuming that *o* was returned by |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 149 | :c:func:`PySequence_Fast` and that *o* is not ``NULL``. The size can also be |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 150 | gotten by calling :c:func:`PySequence_Size` on *o*, but |
Matti Picus | 57b7dbc | 2019-09-12 19:19:06 +0300 | [diff] [blame] | 151 | :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a |
| 152 | list or tuple. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 153 | |
| 154 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 155 | .. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 156 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 157 | Return the *i*\ th element of *o*, assuming that *o* was returned by |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 158 | :c:func:`PySequence_Fast`, *o* is not ``NULL``, and that *i* is within bounds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 159 | |
| 160 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 161 | .. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 162 | |
| 163 | Return the underlying array of PyObject pointers. Assumes that *o* was returned |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 164 | by :c:func:`PySequence_Fast` and *o* is not ``NULL``. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 165 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 166 | Note, if a list gets resized, the reallocation may relocate the items array. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 167 | So, only use the underlying array pointer in contexts where the sequence |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 168 | cannot change. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 169 | |
| 170 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 171 | .. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 172 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 173 | Return the *i*\ th element of *o* or ``NULL`` on failure. Faster form of |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 174 | :c:func:`PySequence_GetItem` but without checking that |
Sandro Tosi | 7bf4363 | 2011-08-13 00:39:46 +0200 | [diff] [blame] | 175 | :c:func:`PySequence_Check` on *o* is true and without adjustment for negative |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 176 | indices. |