• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. _slice-objects:
4
5Slice Objects
6-------------
7
8
9.. c:var:: PyTypeObject PySlice_Type
10
11   The type object for slice objects.  This is the same as :class:`slice` in the
12   Python layer.
13
14
15.. c:function:: int PySlice_Check(PyObject *ob)
16
17   Return true if *ob* is a slice object; *ob* must not be ``NULL``.  This
18   function always succeeds.
19
20
21.. c:function:: PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
22
23   Return a new slice object with the given values.  The *start*, *stop*, and
24   *step* parameters are used as the values of the slice object attributes of
25   the same names.  Any of the values may be ``NULL``, in which case the
26   ``None`` will be used for the corresponding attribute.  Return ``NULL`` if
27   the new object could not be allocated.
28
29
30.. c:function:: int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
31
32   Retrieve the start, stop and step indices from the slice object *slice*,
33   assuming a sequence of length *length*. Treats indices greater than
34   *length* as errors.
35
36   Returns ``0`` on success and ``-1`` on error with no exception set (unless one of
37   the indices was not :const:`None` and failed to be converted to an integer,
38   in which case ``-1`` is returned with an exception set).
39
40   You probably do not want to use this function.
41
42   .. versionchanged:: 3.2
43      The parameter type for the *slice* parameter was ``PySliceObject*``
44      before.
45
46
47.. 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)
48
49   Usable replacement for :c:func:`PySlice_GetIndices`.  Retrieve the start,
50   stop, and step indices from the slice object *slice* assuming a sequence of
51   length *length*, and store the length of the slice in *slicelength*.  Out
52   of bounds indices are clipped in a manner consistent with the handling of
53   normal slices.
54
55   Returns ``0`` on success and ``-1`` on error with exception set.
56
57   .. note::
58      This function is considered not safe for resizable sequences.
59      Its invocation should be replaced by a combination of
60      :c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` where ::
61
62         if (PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength) < 0) {
63             // return error
64         }
65
66      is replaced by ::
67
68         if (PySlice_Unpack(slice, &start, &stop, &step) < 0) {
69             // return error
70         }
71         slicelength = PySlice_AdjustIndices(length, &start, &stop, step);
72
73   .. versionchanged:: 3.2
74      The parameter type for the *slice* parameter was ``PySliceObject*``
75      before.
76
77   .. versionchanged:: 3.6.1
78      If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400``
79      and ``0x03060000`` (not including) or ``0x03060100`` or higher
80      :c:func:`!PySlice_GetIndicesEx` is implemented as a macro using
81      :c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
82      Arguments *start*, *stop* and *step* are evaluated more than once.
83
84   .. deprecated:: 3.6.1
85      If ``Py_LIMITED_API`` is set to the value less than ``0x03050400`` or
86      between ``0x03060000`` and ``0x03060100`` (not including)
87      :c:func:`!PySlice_GetIndicesEx` is a deprecated function.
88
89
90.. c:function:: int PySlice_Unpack(PyObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
91
92   Extract the start, stop and step data members from a slice object as
93   C integers.  Silently reduce values larger than ``PY_SSIZE_T_MAX`` to
94   ``PY_SSIZE_T_MAX``, silently boost the start and stop values less than
95   ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step
96   values less than ``-PY_SSIZE_T_MAX`` to ``-PY_SSIZE_T_MAX``.
97
98   Return ``-1`` on error, ``0`` on success.
99
100   .. versionadded:: 3.6.1
101
102
103.. c:function:: Py_ssize_t PySlice_AdjustIndices(Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
104
105   Adjust start/end slice indices assuming a sequence of the specified length.
106   Out of bounds indices are clipped in a manner consistent with the handling
107   of normal slices.
108
109   Return the length of the slice.  Always successful.  Doesn't call Python
110   code.
111
112   .. versionadded:: 3.6.1
113
114
115Ellipsis Object
116---------------
117
118
119.. c:var:: PyObject *Py_Ellipsis
120
121   The Python ``Ellipsis`` object.  This object has no methods.  It needs to be
122   treated just like any other object with respect to reference counts.  Like
123   :c:data:`Py_None` it is a singleton object.
124