17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _slice-objects:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ciSlice Objects
67db96d56Sopenharmony_ci-------------
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci.. c:var:: PyTypeObject PySlice_Type
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci   The type object for slice objects.  This is the same as :class:`slice` in the
127db96d56Sopenharmony_ci   Python layer.
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ci.. c:function:: int PySlice_Check(PyObject *ob)
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ci   Return true if *ob* is a slice object; *ob* must not be ``NULL``.  This
187db96d56Sopenharmony_ci   function always succeeds.
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ci.. c:function:: PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ci   Return a new slice object with the given values.  The *start*, *stop*, and
247db96d56Sopenharmony_ci   *step* parameters are used as the values of the slice object attributes of
257db96d56Sopenharmony_ci   the same names.  Any of the values may be ``NULL``, in which case the
267db96d56Sopenharmony_ci   ``None`` will be used for the corresponding attribute.  Return ``NULL`` if
277db96d56Sopenharmony_ci   the new object could not be allocated.
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci.. c:function:: int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci   Retrieve the start, stop and step indices from the slice object *slice*,
337db96d56Sopenharmony_ci   assuming a sequence of length *length*. Treats indices greater than
347db96d56Sopenharmony_ci   *length* as errors.
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ci   Returns ``0`` on success and ``-1`` on error with no exception set (unless one of
377db96d56Sopenharmony_ci   the indices was not :const:`None` and failed to be converted to an integer,
387db96d56Sopenharmony_ci   in which case ``-1`` is returned with an exception set).
397db96d56Sopenharmony_ci
407db96d56Sopenharmony_ci   You probably do not want to use this function.
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci   .. versionchanged:: 3.2
437db96d56Sopenharmony_ci      The parameter type for the *slice* parameter was ``PySliceObject*``
447db96d56Sopenharmony_ci      before.
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ci.. c:function:: int PySlice_GetIndicesEx(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci   Usable replacement for :c:func:`PySlice_GetIndices`.  Retrieve the start,
507db96d56Sopenharmony_ci   stop, and step indices from the slice object *slice* assuming a sequence of
517db96d56Sopenharmony_ci   length *length*, and store the length of the slice in *slicelength*.  Out
527db96d56Sopenharmony_ci   of bounds indices are clipped in a manner consistent with the handling of
537db96d56Sopenharmony_ci   normal slices.
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci   Returns ``0`` on success and ``-1`` on error with exception set.
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci   .. note::
587db96d56Sopenharmony_ci      This function is considered not safe for resizable sequences.
597db96d56Sopenharmony_ci      Its invocation should be replaced by a combination of
607db96d56Sopenharmony_ci      :c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` where ::
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci         if (PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength) < 0) {
637db96d56Sopenharmony_ci             // return error
647db96d56Sopenharmony_ci         }
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci      is replaced by ::
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci         if (PySlice_Unpack(slice, &start, &stop, &step) < 0) {
697db96d56Sopenharmony_ci             // return error
707db96d56Sopenharmony_ci         }
717db96d56Sopenharmony_ci         slicelength = PySlice_AdjustIndices(length, &start, &stop, step);
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci   .. versionchanged:: 3.2
747db96d56Sopenharmony_ci      The parameter type for the *slice* parameter was ``PySliceObject*``
757db96d56Sopenharmony_ci      before.
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci   .. versionchanged:: 3.6.1
787db96d56Sopenharmony_ci      If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400``
797db96d56Sopenharmony_ci      and ``0x03060000`` (not including) or ``0x03060100`` or higher
807db96d56Sopenharmony_ci      :c:func:`!PySlice_GetIndicesEx` is implemented as a macro using
817db96d56Sopenharmony_ci      :c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
827db96d56Sopenharmony_ci      Arguments *start*, *stop* and *step* are evaluated more than once.
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   .. deprecated:: 3.6.1
857db96d56Sopenharmony_ci      If ``Py_LIMITED_API`` is set to the value less than ``0x03050400`` or
867db96d56Sopenharmony_ci      between ``0x03060000`` and ``0x03060100`` (not including)
877db96d56Sopenharmony_ci      :c:func:`!PySlice_GetIndicesEx` is a deprecated function.
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci.. c:function:: int PySlice_Unpack(PyObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci   Extract the start, stop and step data members from a slice object as
937db96d56Sopenharmony_ci   C integers.  Silently reduce values larger than ``PY_SSIZE_T_MAX`` to
947db96d56Sopenharmony_ci   ``PY_SSIZE_T_MAX``, silently boost the start and stop values less than
957db96d56Sopenharmony_ci   ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step
967db96d56Sopenharmony_ci   values less than ``-PY_SSIZE_T_MAX`` to ``-PY_SSIZE_T_MAX``.
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ci   Return ``-1`` on error, ``0`` on success.
997db96d56Sopenharmony_ci
1007db96d56Sopenharmony_ci   .. versionadded:: 3.6.1
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ci.. c:function:: Py_ssize_t PySlice_AdjustIndices(Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ci   Adjust start/end slice indices assuming a sequence of the specified length.
1067db96d56Sopenharmony_ci   Out of bounds indices are clipped in a manner consistent with the handling
1077db96d56Sopenharmony_ci   of normal slices.
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci   Return the length of the slice.  Always successful.  Doesn't call Python
1107db96d56Sopenharmony_ci   code.
1117db96d56Sopenharmony_ci
1127db96d56Sopenharmony_ci   .. versionadded:: 3.6.1
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ciEllipsis Object
1167db96d56Sopenharmony_ci---------------
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci.. c:var:: PyObject *Py_Ellipsis
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci   The Python ``Ellipsis`` object.  This object has no methods.  It needs to be
1227db96d56Sopenharmony_ci   treated just like any other object with respect to reference counts.  Like
1237db96d56Sopenharmony_ci   :c:data:`Py_None` it is a singleton object.
124