1.. highlightlang:: c
2
3.. _sequence:
4
5Sequence 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
21   failure.  This is equivalent to the Python expression ``len(o)``.
22
23   .. versionchanged:: 2.5
24      These functions returned an :c:type:`int` type. This might require
25      changes in your code for properly supporting 64-bit systems.
26
27
28.. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
29
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
34.. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
35
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
39   .. versionchanged:: 2.5
40      This function used an :c:type:`int` type for *count*. This might require
41      changes in your code for properly supporting 64-bit systems.
42
43
44.. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
45
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
51.. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
52
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
57   .. versionchanged:: 2.5
58      This function used an :c:type:`int` type for *count*. This might require
59      changes in your code for properly supporting 64-bit systems.
60
61
62.. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
63
64   Return the *i*\ th element of *o*, or *NULL* on failure. This is the equivalent of
65   the Python expression ``o[i]``.
66
67   .. versionchanged:: 2.5
68      This function used an :c:type:`int` type for *i*. This might require
69      changes in your code for properly supporting 64-bit systems.
70
71
72.. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
73
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
77   .. versionchanged:: 2.5
78      This function used an :c:type:`int` type for *i1* and *i2*. This might
79      require changes in your code for properly supporting 64-bit systems.
80
81
82.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
83
84   Assign object *v* to the *i*\ th element of *o*.  Raise an exception
85   and return ``-1`` on failure; return ``0`` on success.  This
86   is the equivalent of the Python statement ``o[i] = v``.  This function *does
87   not* steal a reference to *v*.
88
89   If *v* is *NULL*, the element is deleted, however this feature is
90   deprecated in favour of using :c:func:`PySequence_DelItem`.
91
92   .. versionchanged:: 2.5
93      This function used an :c:type:`int` type for *i*. This might require
94      changes in your code for properly supporting 64-bit systems.
95
96
97.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
98
99   Delete the *i*\ th element of object *o*.  Returns ``-1`` on failure.  This is the
100   equivalent of the Python statement ``del o[i]``.
101
102   .. versionchanged:: 2.5
103      This function used an :c:type:`int` type for *i*. This might require
104      changes in your code for properly supporting 64-bit systems.
105
106
107.. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
108
109   Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
110   *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`.
115
116   .. versionchanged:: 2.5
117      This function used an :c:type:`int` type for *i1* and *i2*. This might
118      require changes in your code for properly supporting 64-bit systems.
119
120
121.. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
122
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
126   .. versionchanged:: 2.5
127      This function used an :c:type:`int` type for *i1* and *i2*. This might
128      require changes in your code for properly supporting 64-bit systems.
129
130
131.. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
132
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
137   .. versionchanged:: 2.5
138      This function returned an :c:type:`int` type. This might require changes
139      in your code for properly supporting 64-bit systems.
140
141
142.. c:function:: int PySequence_Contains(PyObject *o, PyObject *value)
143
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
149.. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
150
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
154   .. versionchanged:: 2.5
155      This function returned an :c:type:`int` type. This might require changes
156      in your code for properly supporting 64-bit systems.
157
158
159.. c:function:: PyObject* PySequence_List(PyObject *o)
160
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
165.. c:function:: PyObject* PySequence_Tuple(PyObject *o)
166
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
175.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
176
177   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.
181
182
183.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
184
185   Return the *i*\ th element of *o*, assuming that *o* was returned by
186   :c:func:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
187
188   .. versionchanged:: 2.5
189      This function used an :c:type:`int` type for *i*. This might require
190      changes in your code for properly supporting 64-bit systems.
191
192
193.. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
194
195   Return the underlying array of PyObject pointers.  Assumes that *o* was returned
196   by :c:func:`PySequence_Fast` and *o* is not *NULL*.
197
198   Note, if a list gets resized, the reallocation may relocate the items array.
199   So, only use the underlying array pointer in contexts where the sequence
200   cannot change.
201
202   .. versionadded:: 2.4
203
204
205.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
206
207   Return the *i*\ th element of *o* or *NULL* on failure. Macro form of
208   :c:func:`PySequence_GetItem` but without checking that
209   :c:func:`PySequence_Check` on *o* is true and without adjustment for negative
210   indices.
211
212   .. versionadded:: 2.3
213
214   .. versionchanged:: 2.5
215      This function used an :c:type:`int` type for *i*. This might require
216      changes in your code for properly supporting 64-bit systems.
217
218
219.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
220
221   Returns the length of *o*, assuming that *o* was returned by
222   :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
225   or tuple.
226