blob: 81f8557ea6e665d544ff33cb56f380d5831ee753 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _sequence:
4
5Sequence Protocol
6=================
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:function:: int PySequence_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11 Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
12 This function always succeeds.
13
14
Georg Brandl60203b42010-10-06 10:11:56 +000015.. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000016 Py_ssize_t PySequence_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
18 .. index:: builtin: len
19
Zackery Spytz7a1e1782018-02-20 10:24:29 -070020 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 Brandl54a3faa2008-01-20 09:30:57 +000022
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
27 This is the equivalent of the Python expression ``o1 + o2``.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 Return the result of repeating sequence object *o* *count* times, or *NULL* on
33 failure. This is the equivalent of the Python expression ``o * count``.
34
35
Georg Brandl60203b42010-10-06 10:11:56 +000036.. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
38 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
39 The operation is done *in-place* when *o1* supports it. This is the equivalent
40 of the Python expression ``o1 += o2``.
41
42
Georg Brandl60203b42010-10-06 10:11:56 +000043.. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
45 Return the result of repeating sequence object *o* *count* times, or *NULL* on
46 failure. The operation is done *in-place* when *o* supports it. This is the
47 equivalent of the Python expression ``o *= count``.
48
49
Georg Brandl60203b42010-10-06 10:11:56 +000050.. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Benjamin Petersonfa0d7032009-06-01 22:42:33 +000052 Return the *i*\ th element of *o*, or *NULL* on failure. This is the equivalent of
Georg Brandl54a3faa2008-01-20 09:30:57 +000053 the Python expression ``o[i]``.
54
55
Georg Brandl60203b42010-10-06 10:11:56 +000056.. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
58 Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on
59 failure. This is the equivalent of the Python expression ``o[i1:i2]``.
60
61
Georg Brandl60203b42010-10-06 10:11:56 +000062.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
Martin Panter45be8d62015-12-08 00:03:20 +000064 Assign object *v* to the *i*\ th element of *o*. Raise an exception
65 and return ``-1`` on failure; return ``0`` on success. This
Georg Brandl54a3faa2008-01-20 09:30:57 +000066 is the equivalent of the Python statement ``o[i] = v``. This function *does
67 not* steal a reference to *v*.
68
Martin Panter45be8d62015-12-08 00:03:20 +000069 If *v* is *NULL*, the element is deleted, however this feature is
70 deprecated in favour of using :c:func:`PySequence_DelItem`.
71
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
Georg Brandl60203b42010-10-06 10:11:56 +000073.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
Georg Brandl54a3faa2008-01-20 09:30:57 +000074
Benjamin Petersonfa0d7032009-06-01 22:42:33 +000075 Delete the *i*\ th element of object *o*. Returns ``-1`` on failure. This is the
Georg Brandl54a3faa2008-01-20 09:30:57 +000076 equivalent of the Python statement ``del o[i]``.
77
78
Georg Brandl60203b42010-10-06 10:11:56 +000079.. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
81 Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
82 *i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``.
83
84
Georg Brandl60203b42010-10-06 10:11:56 +000085.. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
87 Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on
88 failure. This is the equivalent of the Python statement ``del o[i1:i2]``.
89
90
Georg Brandl60203b42010-10-06 10:11:56 +000091.. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
93 Return the number of occurrences of *value* in *o*, that is, return the number
94 of keys for which ``o[key] == value``. On failure, return ``-1``. This is
95 equivalent to the Python expression ``o.count(value)``.
96
97
Georg Brandl60203b42010-10-06 10:11:56 +000098.. c:function:: int PySequence_Contains(PyObject *o, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +000099
100 Determine if *o* contains *value*. If an item in *o* is equal to *value*,
101 return ``1``, otherwise return ``0``. On error, return ``-1``. This is
102 equivalent to the Python expression ``value in o``.
103
104
Georg Brandl60203b42010-10-06 10:11:56 +0000105.. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000106
107 Return the first index *i* for which ``o[i] == value``. On error, return
108 ``-1``. This is equivalent to the Python expression ``o.index(value)``.
109
110
Georg Brandl60203b42010-10-06 10:11:56 +0000111.. c:function:: PyObject* PySequence_List(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
Berker Peksag32799952015-03-13 02:55:45 +0200113 Return a list object with the same contents as the sequence or iterable *o*,
114 or *NULL* on failure. The returned list is guaranteed to be new. This is
115 equivalent to the Python expression ``list(o)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000116
117
Georg Brandl60203b42010-10-06 10:11:56 +0000118.. c:function:: PyObject* PySequence_Tuple(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
120 .. index:: builtin: tuple
121
122 Return a tuple object with the same contents as the arbitrary sequence *o* or
123 *NULL* on failure. If *o* is a tuple, a new reference will be returned,
124 otherwise a tuple will be constructed with the appropriate contents. This is
125 equivalent to the Python expression ``tuple(o)``.
126
127
Georg Brandl60203b42010-10-06 10:11:56 +0000128.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
Benjamin Petersonce0700a2014-04-08 10:48:36 -0400130 Return the sequence *o* as a list, unless it is already a tuple or list, in
131 which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access
132 the members of the result. Returns *NULL* on failure. If the object is not
133 a sequence, raises :exc:`TypeError` with *m* as the message text.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
135
Georg Brandl60203b42010-10-06 10:11:56 +0000136.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000138 Return the *i*\ th element of *o*, assuming that *o* was returned by
Georg Brandl60203b42010-10-06 10:11:56 +0000139 :c:func:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
141
Georg Brandl60203b42010-10-06 10:11:56 +0000142.. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000143
144 Return the underlying array of PyObject pointers. Assumes that *o* was returned
Georg Brandl60203b42010-10-06 10:11:56 +0000145 by :c:func:`PySequence_Fast` and *o* is not *NULL*.
Georg Brandl48310cd2009-01-03 21:18:54 +0000146
Georg Brandl2ee470f2008-07-16 12:55:28 +0000147 Note, if a list gets resized, the reallocation may relocate the items array.
Georg Brandl48310cd2009-01-03 21:18:54 +0000148 So, only use the underlying array pointer in contexts where the sequence
Georg Brandl2ee470f2008-07-16 12:55:28 +0000149 cannot change.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000150
151
Georg Brandl60203b42010-10-06 10:11:56 +0000152.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000153
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000154 Return the *i*\ th element of *o* or *NULL* on failure. Macro form of
Georg Brandl60203b42010-10-06 10:11:56 +0000155 :c:func:`PySequence_GetItem` but without checking that
Sandro Tosi7bf43632011-08-13 00:39:46 +0200156 :c:func:`PySequence_Check` on *o* is true and without adjustment for negative
Georg Brandl54a3faa2008-01-20 09:30:57 +0000157 indices.
158
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159
Georg Brandl60203b42010-10-06 10:11:56 +0000160.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000161
162 Returns the length of *o*, assuming that *o* was returned by
Georg Brandl60203b42010-10-06 10:11:56 +0000163 :c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
164 gotten by calling :c:func:`PySequence_Size` on *o*, but
165 :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166 or tuple.