1 .. highlightlang:: 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 sequence protocol, and ``0`` otherwise. 12 This function always succeeds. 13 14 15 .. c:function:: Py_ssize_t PySequence_Size(PyObject *o) 16 Py_ssize_t PySequence_Length(PyObject *o) 17 18 .. index:: builtin: len 19 20 Returns the number of objects in sequence *o* on success, and ``-1`` on failure. 21 For objects that do not provide sequence protocol, this is equivalent to the 22 Python expression ``len(o)``. 23 24 .. versionchanged:: 2.5 25 These functions returned an :c:type:`int` type. This might require 26 changes in your code for properly supporting 64-bit systems. 27 28 29 .. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2) 30 31 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure. 32 This is the equivalent of the Python expression ``o1 + o2``. 33 34 35 .. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count) 36 37 Return the result of repeating sequence object *o* *count* times, or *NULL* on 38 failure. This is the equivalent of the Python expression ``o * count``. 39 40 .. versionchanged:: 2.5 41 This function used an :c:type:`int` type for *count*. This might require 42 changes in your code for properly supporting 64-bit systems. 43 44 45 .. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2) 46 47 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure. 48 The operation is done *in-place* when *o1* supports it. This is the equivalent 49 of the Python expression ``o1 += o2``. 50 51 52 .. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) 53 54 Return the result of repeating sequence object *o* *count* times, or *NULL* on 55 failure. The operation is done *in-place* when *o* supports it. This is the 56 equivalent of the Python expression ``o *= count``. 57 58 .. versionchanged:: 2.5 59 This function used an :c:type:`int` type for *count*. This might require 60 changes in your code for properly supporting 64-bit systems. 61 62 63 .. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i) 64 65 Return the *i*\ th element of *o*, or *NULL* on failure. This is the equivalent of 66 the Python expression ``o[i]``. 67 68 .. versionchanged:: 2.5 69 This function used an :c:type:`int` type for *i*. This might require 70 changes in your code for properly supporting 64-bit systems. 71 72 73 .. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) 74 75 Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on 76 failure. This is the equivalent of the Python expression ``o[i1:i2]``. 77 78 .. versionchanged:: 2.5 79 This function used an :c:type:`int` type for *i1* and *i2*. This might 80 require changes in your code for properly supporting 64-bit systems. 81 82 83 .. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v) 84 85 Assign object *v* to the *i*\ th element of *o*. Raise an exception 86 and return ``-1`` on failure; return ``0`` on success. This 87 is the equivalent of the Python statement ``o[i] = v``. This function *does 88 not* steal a reference to *v*. 89 90 If *v* is *NULL*, the element is deleted, however this feature is 91 deprecated in favour of using :c:func:`PySequence_DelItem`. 92 93 .. versionchanged:: 2.5 94 This function used an :c:type:`int` type for *i*. This might require 95 changes in your code for properly supporting 64-bit systems. 96 97 98 .. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i) 99 100 Delete the *i*\ th element of object *o*. Returns ``-1`` on failure. This is the 101 equivalent of the Python statement ``del o[i]``. 102 103 .. versionchanged:: 2.5 104 This function used an :c:type:`int` type for *i*. This might require 105 changes in your code for properly supporting 64-bit systems. 106 107 108 .. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v) 109 110 Assign the sequence object *v* to the slice in sequence object *o* from *i1* to 111 *i2*. Raise an exception and return ``-1`` on failure; return ``0`` on success. 112 This is the equivalent of the Python statement ``o[i1:i2] = v``. 113 114 If *v* is *NULL*, the slice is deleted, however this feature is 115 deprecated in favour of using :c:func:`PySequence_DelSlice`. 116 117 .. versionchanged:: 2.5 118 This function used an :c:type:`int` type for *i1* and *i2*. This might 119 require changes in your code for properly supporting 64-bit systems. 120 121 122 .. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) 123 124 Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on 125 failure. This is the equivalent of the Python statement ``del o[i1:i2]``. 126 127 .. versionchanged:: 2.5 128 This function used an :c:type:`int` type for *i1* and *i2*. This might 129 require changes in your code for properly supporting 64-bit systems. 130 131 132 .. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value) 133 134 Return the number of occurrences of *value* in *o*, that is, return the number 135 of keys for which ``o[key] == value``. On failure, return ``-1``. This is 136 equivalent to the Python expression ``o.count(value)``. 137 138 .. versionchanged:: 2.5 139 This function returned an :c:type:`int` type. This might require changes 140 in your code for properly supporting 64-bit systems. 141 142 143 .. c:function:: int PySequence_Contains(PyObject *o, PyObject *value) 144 145 Determine if *o* contains *value*. If an item in *o* is equal to *value*, 146 return ``1``, otherwise return ``0``. On error, return ``-1``. This is 147 equivalent to the Python expression ``value in o``. 148 149 150 .. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value) 151 152 Return the first index *i* for which ``o[i] == value``. On error, return 153 ``-1``. This is equivalent to the Python expression ``o.index(value)``. 154 155 .. versionchanged:: 2.5 156 This function returned an :c:type:`int` type. This might require changes 157 in your code for properly supporting 64-bit systems. 158 159 160 .. c:function:: PyObject* PySequence_List(PyObject *o) 161 162 Return a list object with the same contents as the arbitrary sequence *o*. The 163 returned list is guaranteed to be new. 164 165 166 .. c:function:: PyObject* PySequence_Tuple(PyObject *o) 167 168 .. index:: builtin: tuple 169 170 Return a tuple object with the same contents as the arbitrary sequence *o* or 171 *NULL* on failure. If *o* is a tuple, a new reference will be returned, 172 otherwise a tuple will be constructed with the appropriate contents. This is 173 equivalent to the Python expression ``tuple(o)``. 174 175 176 .. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m) 177 178 Return the sequence *o* as a list, unless it is already a tuple or list, in 179 which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access 180 the members of the result. Returns *NULL* on failure. If the object is not 181 a sequence, raises :exc:`TypeError` with *m* as the message text. 182 183 184 .. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i) 185 186 Return the *i*\ th element of *o*, assuming that *o* was returned by 187 :c:func:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds. 188 189 .. versionchanged:: 2.5 190 This function used an :c:type:`int` type for *i*. This might require 191 changes in your code for properly supporting 64-bit systems. 192 193 194 .. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o) 195 196 Return the underlying array of PyObject pointers. Assumes that *o* was returned 197 by :c:func:`PySequence_Fast` and *o* is not *NULL*. 198 199 Note, if a list gets resized, the reallocation may relocate the items array. 200 So, only use the underlying array pointer in contexts where the sequence 201 cannot change. 202 203 .. versionadded:: 2.4 204 205 206 .. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i) 207 208 Return the *i*\ th element of *o* or *NULL* on failure. Macro form of 209 :c:func:`PySequence_GetItem` but without checking that 210 :c:func:`PySequence_Check` on *o* is true and without adjustment for negative 211 indices. 212 213 .. versionadded:: 2.3 214 215 .. versionchanged:: 2.5 216 This function used an :c:type:`int` type for *i*. This might require 217 changes in your code for properly supporting 64-bit systems. 218 219 220 .. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o) 221 222 Returns the length of *o*, assuming that *o* was returned by 223 :c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be 224 gotten by calling :c:func:`PySequence_Size` on *o*, but 225 :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list 226 or tuple. 227