Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 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. |
| 12 | This function always succeeds. |
| 13 | |
| 14 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 15 | .. c:function:: Py_ssize_t PySequence_Size(PyObject *o) |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 16 | Py_ssize_t PySequence_Length(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 24 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 25 | .. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 26 | |
| 27 | Return the concatenation of *o1* and *o2* on success, and *NULL* on failure. |
| 28 | This is the equivalent of the Python expression ``o1 + o2``. |
| 29 | |
| 30 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | .. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 32 | |
| 33 | Return the result of repeating sequence object *o* *count* times, or *NULL* on |
| 34 | failure. This is the equivalent of the Python expression ``o * count``. |
| 35 | |
| 36 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 37 | .. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 | |
| 39 | Return the concatenation of *o1* and *o2* on success, and *NULL* on failure. |
| 40 | The operation is done *in-place* when *o1* supports it. This is the equivalent |
| 41 | of the Python expression ``o1 += o2``. |
| 42 | |
| 43 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 45 | |
| 46 | Return the result of repeating sequence object *o* *count* times, or *NULL* on |
| 47 | failure. The operation is done *in-place* when *o* supports it. This is the |
| 48 | equivalent of the Python expression ``o *= count``. |
| 49 | |
| 50 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 51 | .. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 53 | 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] | 54 | the Python expression ``o[i]``. |
| 55 | |
| 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | .. 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] | 58 | |
| 59 | Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on |
| 60 | failure. This is the equivalent of the Python expression ``o[i1:i2]``. |
| 61 | |
| 62 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 63 | .. 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] | 64 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 65 | Assign object *v* to the *i*\ th element of *o*. Returns ``-1`` on failure. This |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 66 | is the equivalent of the Python statement ``o[i] = v``. This function *does |
| 67 | not* steal a reference to *v*. |
| 68 | |
| 69 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 71 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 72 | 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] | 73 | equivalent of the Python statement ``del o[i]``. |
| 74 | |
| 75 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. 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] | 77 | |
| 78 | Assign the sequence object *v* to the slice in sequence object *o* from *i1* to |
| 79 | *i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``. |
| 80 | |
| 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | .. 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] | 83 | |
| 84 | Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on |
| 85 | failure. This is the equivalent of the Python statement ``del o[i1:i2]``. |
| 86 | |
| 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 89 | |
| 90 | Return the number of occurrences of *value* in *o*, that is, return the number |
| 91 | of keys for which ``o[key] == value``. On failure, return ``-1``. This is |
| 92 | equivalent to the Python expression ``o.count(value)``. |
| 93 | |
| 94 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 95 | .. c:function:: int PySequence_Contains(PyObject *o, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 96 | |
| 97 | Determine if *o* contains *value*. If an item in *o* is equal to *value*, |
| 98 | return ``1``, otherwise return ``0``. On error, return ``-1``. This is |
| 99 | equivalent to the Python expression ``value in o``. |
| 100 | |
| 101 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 102 | .. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 103 | |
| 104 | Return the first index *i* for which ``o[i] == value``. On error, return |
| 105 | ``-1``. This is equivalent to the Python expression ``o.index(value)``. |
| 106 | |
| 107 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 108 | .. c:function:: PyObject* PySequence_List(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 109 | |
| 110 | Return a list object with the same contents as the arbitrary sequence *o*. The |
| 111 | returned list is guaranteed to be new. |
| 112 | |
| 113 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 114 | .. c:function:: PyObject* PySequence_Tuple(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 115 | |
| 116 | .. index:: builtin: tuple |
| 117 | |
| 118 | Return a tuple object with the same contents as the arbitrary sequence *o* or |
| 119 | *NULL* on failure. If *o* is a tuple, a new reference will be returned, |
| 120 | otherwise a tuple will be constructed with the appropriate contents. This is |
| 121 | equivalent to the Python expression ``tuple(o)``. |
| 122 | |
| 123 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 124 | .. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | ce0700a | 2014-04-08 10:48:36 -0400 | [diff] [blame] | 126 | Return the sequence *o* as a list, unless it is already a tuple or list, in |
| 127 | which case *o* is returned. Use :c:func:`PySequence_Fast_GET_ITEM` to access |
| 128 | the members of the result. Returns *NULL* on failure. If the object is not |
| 129 | a sequence, raises :exc:`TypeError` with *m* as the message text. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 130 | |
| 131 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 132 | .. 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] | 133 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 134 | Return the *i*\ th element of *o*, assuming that *o* was returned by |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 135 | :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] | 136 | |
| 137 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 138 | .. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 139 | |
| 140 | Return the underlying array of PyObject pointers. Assumes that *o* was returned |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 141 | by :c:func:`PySequence_Fast` and *o* is not *NULL*. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 142 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 143 | 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] | 144 | So, only use the underlying array pointer in contexts where the sequence |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 145 | cannot change. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 146 | |
| 147 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 148 | .. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 149 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 150 | Return the *i*\ th element of *o* or *NULL* on failure. Macro form of |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 151 | :c:func:`PySequence_GetItem` but without checking that |
Sandro Tosi | 7bf4363 | 2011-08-13 00:39:46 +0200 | [diff] [blame] | 152 | :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] | 153 | indices. |
| 154 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 155 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 156 | .. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 157 | |
| 158 | Returns the length of *o*, assuming that *o* was returned by |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 159 | :c:func:`PySequence_Fast` and that *o* is not *NULL*. The size can also be |
| 160 | gotten by calling :c:func:`PySequence_Size` on *o*, but |
| 161 | :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 162 | or tuple. |