blob: d95ef2398cc5eb4b31ec88ae31b7189d20bf7445 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _sequence:
4
5Sequence Protocol
6=================
7
8
Sandro Tosi98ed08f2012-01-14 16:42:02 +01009.. c:function:: int PySequence_Check(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000010
11 Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
12 This function always succeeds.
13
14
Sandro Tosi98ed08f2012-01-14 16:42:02 +010015.. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000016 Py_ssize_t PySequence_Length(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000017
18 .. index:: builtin: len
19
Miss Islington (bot)ecaa3722018-02-25 13:22:43 -080020 Returns the number of objects in sequence *o* on success, and ``-1`` on
21 failure. This is equivalent to the Python expression ``len(o)``.
Georg Brandlf6842722008-01-19 22:08:21 +000022
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000023 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010024 These functions returned an :c:type:`int` type. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000025 changes in your code for properly supporting 64-bit systems.
Georg Brandlf6842722008-01-19 22:08:21 +000026
27
Sandro Tosi98ed08f2012-01-14 16:42:02 +010028.. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000029
30 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
31 This is the equivalent of the Python expression ``o1 + o2``.
32
33
Sandro Tosi98ed08f2012-01-14 16:42:02 +010034.. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
Georg Brandlf6842722008-01-19 22:08:21 +000035
36 Return the result of repeating sequence object *o* *count* times, or *NULL* on
37 failure. This is the equivalent of the Python expression ``o * count``.
38
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000039 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010040 This function used an :c:type:`int` type for *count*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000041 changes in your code for properly supporting 64-bit systems.
42
Georg Brandlf6842722008-01-19 22:08:21 +000043
Sandro Tosi98ed08f2012-01-14 16:42:02 +010044.. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000045
46 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
47 The operation is done *in-place* when *o1* supports it. This is the equivalent
48 of the Python expression ``o1 += o2``.
49
50
Sandro Tosi98ed08f2012-01-14 16:42:02 +010051.. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Georg Brandlf6842722008-01-19 22:08:21 +000052
53 Return the result of repeating sequence object *o* *count* times, or *NULL* on
54 failure. The operation is done *in-place* when *o* supports it. This is the
55 equivalent of the Python expression ``o *= count``.
56
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000057 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058 This function used an :c:type:`int` type for *count*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000059 changes in your code for properly supporting 64-bit systems.
60
Georg Brandlf6842722008-01-19 22:08:21 +000061
Sandro Tosi98ed08f2012-01-14 16:42:02 +010062.. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
Georg Brandlf6842722008-01-19 22:08:21 +000063
Georg Brandl6a468982009-05-30 10:33:23 +000064 Return the *i*\ th element of *o*, or *NULL* on failure. This is the equivalent of
Georg Brandlf6842722008-01-19 22:08:21 +000065 the Python expression ``o[i]``.
66
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000067 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010068 This function used an :c:type:`int` type for *i*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000069 changes in your code for properly supporting 64-bit systems.
70
Georg Brandlf6842722008-01-19 22:08:21 +000071
Sandro Tosi98ed08f2012-01-14 16:42:02 +010072.. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
Georg Brandlf6842722008-01-19 22:08:21 +000073
74 Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on
75 failure. This is the equivalent of the Python expression ``o[i1:i2]``.
76
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000077 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010078 This function used an :c:type:`int` type for *i1* and *i2*. This might
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000079 require changes in your code for properly supporting 64-bit systems.
80
Georg Brandlf6842722008-01-19 22:08:21 +000081
Sandro Tosi98ed08f2012-01-14 16:42:02 +010082.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
Georg Brandlf6842722008-01-19 22:08:21 +000083
Martin Pantered826042016-11-30 10:32:40 +000084 Assign object *v* to the *i*\ th element of *o*. Raise an exception
85 and return ``-1`` on failure; return ``0`` on success. This
Georg Brandlf6842722008-01-19 22:08:21 +000086 is the equivalent of the Python statement ``o[i] = v``. This function *does
87 not* steal a reference to *v*.
88
Martin Pantered826042016-11-30 10:32:40 +000089 If *v* is *NULL*, the element is deleted, however this feature is
90 deprecated in favour of using :c:func:`PySequence_DelItem`.
91
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000092 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010093 This function used an :c:type:`int` type for *i*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000094 changes in your code for properly supporting 64-bit systems.
95
Georg Brandlf6842722008-01-19 22:08:21 +000096
Sandro Tosi98ed08f2012-01-14 16:42:02 +010097.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
Georg Brandlf6842722008-01-19 22:08:21 +000098
Georg Brandl6a468982009-05-30 10:33:23 +000099 Delete the *i*\ th element of object *o*. Returns ``-1`` on failure. This is the
Georg Brandlf6842722008-01-19 22:08:21 +0000100 equivalent of the Python statement ``del o[i]``.
101
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000102 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100103 This function used an :c:type:`int` type for *i*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000104 changes in your code for properly supporting 64-bit systems.
105
Georg Brandlf6842722008-01-19 22:08:21 +0000106
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100107.. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
Georg Brandlf6842722008-01-19 22:08:21 +0000108
109 Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
Martin Pantered826042016-11-30 10:32:40 +0000110 *i2*. Raise an exception and return ``-1`` on failure; return ``0`` on success.
111 This is the equivalent of the Python statement ``o[i1:i2] = v``.
112
113 If *v* is *NULL*, the slice is deleted, however this feature is
114 deprecated in favour of using :c:func:`PySequence_DelSlice`.
Georg Brandlf6842722008-01-19 22:08:21 +0000115
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000116 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100117 This function used an :c:type:`int` type for *i1* and *i2*. This might
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000118 require changes in your code for properly supporting 64-bit systems.
119
Georg Brandlf6842722008-01-19 22:08:21 +0000120
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100121.. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
Georg Brandlf6842722008-01-19 22:08:21 +0000122
123 Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on
124 failure. This is the equivalent of the Python statement ``del o[i1:i2]``.
125
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000126 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100127 This function used an :c:type:`int` type for *i1* and *i2*. This might
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000128 require changes in your code for properly supporting 64-bit systems.
129
Georg Brandlf6842722008-01-19 22:08:21 +0000130
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100131.. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
Georg Brandlf6842722008-01-19 22:08:21 +0000132
133 Return the number of occurrences of *value* in *o*, that is, return the number
134 of keys for which ``o[key] == value``. On failure, return ``-1``. This is
135 equivalent to the Python expression ``o.count(value)``.
136
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000137 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100138 This function returned an :c:type:`int` type. This might require changes
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000139 in your code for properly supporting 64-bit systems.
140
Georg Brandlf6842722008-01-19 22:08:21 +0000141
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100142.. c:function:: int PySequence_Contains(PyObject *o, PyObject *value)
Georg Brandlf6842722008-01-19 22:08:21 +0000143
144 Determine if *o* contains *value*. If an item in *o* is equal to *value*,
145 return ``1``, otherwise return ``0``. On error, return ``-1``. This is
146 equivalent to the Python expression ``value in o``.
147
148
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100149.. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
Georg Brandlf6842722008-01-19 22:08:21 +0000150
151 Return the first index *i* for which ``o[i] == value``. On error, return
152 ``-1``. This is equivalent to the Python expression ``o.index(value)``.
153
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000154 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100155 This function returned an :c:type:`int` type. This might require changes
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000156 in your code for properly supporting 64-bit systems.
157
Georg Brandlf6842722008-01-19 22:08:21 +0000158
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100159.. c:function:: PyObject* PySequence_List(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000160
161 Return a list object with the same contents as the arbitrary sequence *o*. The
162 returned list is guaranteed to be new.
163
164
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100165.. c:function:: PyObject* PySequence_Tuple(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000166
167 .. index:: builtin: tuple
168
169 Return a tuple object with the same contents as the arbitrary sequence *o* or
170 *NULL* on failure. If *o* is a tuple, a new reference will be returned,
171 otherwise a tuple will be constructed with the appropriate contents. This is
172 equivalent to the Python expression ``tuple(o)``.
173
174
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100175.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
Georg Brandlf6842722008-01-19 22:08:21 +0000176
Benjamin Peterson96c9a352014-04-08 10:48:36 -0400177 Return the sequence *o* as a list, unless it is already a tuple or list, in
178 which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access
179 the members of the result. Returns *NULL* on failure. If the object is not
180 a sequence, raises :exc:`TypeError` with *m* as the message text.
Georg Brandlf6842722008-01-19 22:08:21 +0000181
182
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100183.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
Georg Brandlf6842722008-01-19 22:08:21 +0000184
Georg Brandl6a468982009-05-30 10:33:23 +0000185 Return the *i*\ th element of *o*, assuming that *o* was returned by
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100186 :c:func:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
Georg Brandlf6842722008-01-19 22:08:21 +0000187
Jeroen Ruigrok van der Werven36378712009-04-25 20:50:27 +0000188 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100189 This function used an :c:type:`int` type for *i*. This might require
Jeroen Ruigrok van der Werven36378712009-04-25 20:50:27 +0000190 changes in your code for properly supporting 64-bit systems.
191
Georg Brandlf6842722008-01-19 22:08:21 +0000192
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100193.. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000194
195 Return the underlying array of PyObject pointers. Assumes that *o* was returned
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100196 by :c:func:`PySequence_Fast` and *o* is not *NULL*.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000197
Raymond Hettinger8dbbb962008-07-11 12:00:21 +0000198 Note, if a list gets resized, the reallocation may relocate the items array.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000199 So, only use the underlying array pointer in contexts where the sequence
Raymond Hettinger8dbbb962008-07-11 12:00:21 +0000200 cannot change.
Georg Brandlf6842722008-01-19 22:08:21 +0000201
202 .. versionadded:: 2.4
203
204
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100205.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
Georg Brandlf6842722008-01-19 22:08:21 +0000206
Georg Brandl6a468982009-05-30 10:33:23 +0000207 Return the *i*\ th element of *o* or *NULL* on failure. Macro form of
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100208 :c:func:`PySequence_GetItem` but without checking that
209 :c:func:`PySequence_Check` on *o* is true and without adjustment for negative
Georg Brandlf6842722008-01-19 22:08:21 +0000210 indices.
211
212 .. versionadded:: 2.3
213
Jeroen Ruigrok van der Werven36378712009-04-25 20:50:27 +0000214 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100215 This function used an :c:type:`int` type for *i*. This might require
Jeroen Ruigrok van der Werven36378712009-04-25 20:50:27 +0000216 changes in your code for properly supporting 64-bit systems.
217
Georg Brandlf6842722008-01-19 22:08:21 +0000218
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100219.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000220
221 Returns the length of *o*, assuming that *o* was returned by
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100222 :c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
223 gotten by calling :c:func:`PySequence_Size` on *o*, but
224 :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
Georg Brandlf6842722008-01-19 22:08:21 +0000225 or tuple.