17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _sequence:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ciSequence Protocol
67db96d56Sopenharmony_ci=================
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci.. c:function:: int PySequence_Check(PyObject *o)
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci   Return ``1`` if the object provides the sequence protocol, and ``0`` otherwise.
127db96d56Sopenharmony_ci   Note that it returns ``1`` for Python classes with a :meth:`__getitem__`
137db96d56Sopenharmony_ci   method, unless they are :class:`dict` subclasses, since in general it
147db96d56Sopenharmony_ci   is impossible to determine what type of keys the class supports.  This
157db96d56Sopenharmony_ci   function always succeeds.
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ci.. c:function:: Py_ssize_t PySequence_Size(PyObject *o)
197db96d56Sopenharmony_ci               Py_ssize_t PySequence_Length(PyObject *o)
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ci   .. index:: pair: built-in function; len
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ci   Returns the number of objects in sequence *o* on success, and ``-1`` on
247db96d56Sopenharmony_ci   failure.  This is equivalent to the Python expression ``len(o)``.
257db96d56Sopenharmony_ci
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci   Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure.
307db96d56Sopenharmony_ci   This is the equivalent of the Python expression ``o1 + o2``.
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci   Return the result of repeating sequence object *o* *count* times, or ``NULL`` on
367db96d56Sopenharmony_ci   failure.  This is the equivalent of the Python expression ``o * count``.
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci   Return the concatenation of *o1* and *o2* on success, and ``NULL`` on failure.
427db96d56Sopenharmony_ci   The operation is done *in-place* when *o1* supports it.  This is the equivalent
437db96d56Sopenharmony_ci   of the Python expression ``o1 += o2``.
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci   Return the result of repeating sequence object *o* *count* times, or ``NULL`` on
497db96d56Sopenharmony_ci   failure.  The operation is done *in-place* when *o* supports it.  This is the
507db96d56Sopenharmony_ci   equivalent of the Python expression ``o *= count``.
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci   Return the *i*\ th element of *o*, or ``NULL`` on failure. This is the equivalent of
567db96d56Sopenharmony_ci   the Python expression ``o[i]``.
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci   Return the slice of sequence object *o* between *i1* and *i2*, or ``NULL`` on
627db96d56Sopenharmony_ci   failure. This is the equivalent of the Python expression ``o[i1:i2]``.
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ci.. c:function:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
667db96d56Sopenharmony_ci
677db96d56Sopenharmony_ci   Assign object *v* to the *i*\ th element of *o*.  Raise an exception
687db96d56Sopenharmony_ci   and return ``-1`` on failure; return ``0`` on success.  This
697db96d56Sopenharmony_ci   is the equivalent of the Python statement ``o[i] = v``.  This function *does
707db96d56Sopenharmony_ci   not* steal a reference to *v*.
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci   If *v* is ``NULL``, the element is deleted, but this feature is
737db96d56Sopenharmony_ci   deprecated in favour of using :c:func:`PySequence_DelItem`.
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci
767db96d56Sopenharmony_ci.. c:function:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   Delete the *i*\ th element of object *o*.  Returns ``-1`` on failure.  This is the
797db96d56Sopenharmony_ci   equivalent of the Python statement ``del o[i]``.
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci.. c:function:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
857db96d56Sopenharmony_ci   *i2*.  This is the equivalent of the Python statement ``o[i1:i2] = v``.
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ci.. c:function:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci   Delete the slice in sequence object *o* from *i1* to *i2*.  Returns ``-1`` on
917db96d56Sopenharmony_ci   failure.  This is the equivalent of the Python statement ``del o[i1:i2]``.
927db96d56Sopenharmony_ci
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci.. c:function:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci   Return the number of occurrences of *value* in *o*, that is, return the number
977db96d56Sopenharmony_ci   of keys for which ``o[key] == value``.  On failure, return ``-1``.  This is
987db96d56Sopenharmony_ci   equivalent to the Python expression ``o.count(value)``.
997db96d56Sopenharmony_ci
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci.. c:function:: int PySequence_Contains(PyObject *o, PyObject *value)
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ci   Determine if *o* contains *value*.  If an item in *o* is equal to *value*,
1047db96d56Sopenharmony_ci   return ``1``, otherwise return ``0``. On error, return ``-1``.  This is
1057db96d56Sopenharmony_ci   equivalent to the Python expression ``value in o``.
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ci.. c:function:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
1097db96d56Sopenharmony_ci
1107db96d56Sopenharmony_ci   Return the first index *i* for which ``o[i] == value``.  On error, return
1117db96d56Sopenharmony_ci   ``-1``.    This is equivalent to the Python expression ``o.index(value)``.
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_List(PyObject *o)
1157db96d56Sopenharmony_ci
1167db96d56Sopenharmony_ci   Return a list object with the same contents as the sequence or iterable *o*,
1177db96d56Sopenharmony_ci   or ``NULL`` on failure.  The returned list is guaranteed to be new.  This is
1187db96d56Sopenharmony_ci   equivalent to the Python expression ``list(o)``.
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_Tuple(PyObject *o)
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci   .. index:: pair: built-in function; tuple
1247db96d56Sopenharmony_ci
1257db96d56Sopenharmony_ci   Return a tuple object with the same contents as the sequence or iterable *o*,
1267db96d56Sopenharmony_ci   or ``NULL`` on failure.  If *o* is a tuple, a new reference will be returned,
1277db96d56Sopenharmony_ci   otherwise a tuple will be constructed with the appropriate contents.  This is
1287db96d56Sopenharmony_ci   equivalent to the Python expression ``tuple(o)``.
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci
1317db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_Fast(PyObject *o, const char *m)
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci   Return the sequence or iterable *o* as an object usable by the other
1347db96d56Sopenharmony_ci   ``PySequence_Fast*`` family of functions. If the object is not a sequence or
1357db96d56Sopenharmony_ci   iterable, raises :exc:`TypeError` with *m* as the message text. Returns
1367db96d56Sopenharmony_ci   ``NULL`` on failure.
1377db96d56Sopenharmony_ci
1387db96d56Sopenharmony_ci   The ``PySequence_Fast*`` functions are thus named because they assume
1397db96d56Sopenharmony_ci   *o* is a :c:type:`PyTupleObject` or a :c:type:`PyListObject` and access
1407db96d56Sopenharmony_ci   the data fields of *o* directly.
1417db96d56Sopenharmony_ci
1427db96d56Sopenharmony_ci   As a CPython implementation detail, if *o* is already a sequence or list, it
1437db96d56Sopenharmony_ci   will be returned.
1447db96d56Sopenharmony_ci
1457db96d56Sopenharmony_ci
1467db96d56Sopenharmony_ci.. c:function:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci   Returns the length of *o*, assuming that *o* was returned by
1497db96d56Sopenharmony_ci   :c:func:`PySequence_Fast` and that *o* is not ``NULL``.  The size can also be
1507db96d56Sopenharmony_ci   retrieved by calling :c:func:`PySequence_Size` on *o*, but
1517db96d56Sopenharmony_ci   :c:func:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a
1527db96d56Sopenharmony_ci   list or tuple.
1537db96d56Sopenharmony_ci
1547db96d56Sopenharmony_ci
1557db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ci   Return the *i*\ th element of *o*, assuming that *o* was returned by
1587db96d56Sopenharmony_ci   :c:func:`PySequence_Fast`, *o* is not ``NULL``, and that *i* is within bounds.
1597db96d56Sopenharmony_ci
1607db96d56Sopenharmony_ci
1617db96d56Sopenharmony_ci.. c:function:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ci   Return the underlying array of PyObject pointers.  Assumes that *o* was returned
1647db96d56Sopenharmony_ci   by :c:func:`PySequence_Fast` and *o* is not ``NULL``.
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ci   Note, if a list gets resized, the reallocation may relocate the items array.
1677db96d56Sopenharmony_ci   So, only use the underlying array pointer in contexts where the sequence
1687db96d56Sopenharmony_ci   cannot change.
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci
1717db96d56Sopenharmony_ci.. c:function:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
1727db96d56Sopenharmony_ci
1737db96d56Sopenharmony_ci   Return the *i*\ th element of *o* or ``NULL`` on failure. Faster form of
1747db96d56Sopenharmony_ci   :c:func:`PySequence_GetItem` but without checking that
1757db96d56Sopenharmony_ci   :c:func:`PySequence_Check` on *o* is true and without adjustment for negative
1767db96d56Sopenharmony_ci   indices.
177