Debian Codegraph @

Doc/c-api/slice.rst

raw
1 .. highlight:: c
2
3 .. _slice-objects:
4
5 Slice 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.
27
28 Return ``NULL`` with an exception set if
29 the new object could not be allocated.
30
31
32 .. c:function:: int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
33
34 Retrieve the start, stop and step indices from the slice object *slice*,
35 assuming a sequence of length *length*. Treats indices greater than
36 *length* as errors.
37
38 Returns ``0`` on success and ``-1`` on error with no exception set (unless one of
39 the indices was not ``None`` and failed to be converted to an integer,
40 in which case ``-1`` is returned with an exception set).
41
42 You probably do not want to use this function.
43
44 .. versionchanged:: 3.2
45 The parameter type for the *slice* parameter was ``PySliceObject*``
46 before.
47
48
49 .. 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)
50
51 Usable replacement for :c:func:`PySlice_GetIndices`. Retrieve the start,
52 stop, and step indices from the slice object *slice* assuming a sequence of
53 length *length*, and store the length of the slice in *slicelength*. Out
54 of bounds indices are clipped in a manner consistent with the handling of
55 normal slices.
56
57 Return ``0`` on success and ``-1`` on error with an exception set.
58
59 .. note::
60 This function is considered not safe for resizable sequences.
61 Its invocation should be replaced by a combination of
62 :c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` where ::
63
64 if (PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength) < 0) {
65 // return error
66 }
67
68 is replaced by ::
69
70 if (PySlice_Unpack(slice, &start, &stop, &step) < 0) {
71 // return error
72 }
73 slicelength = PySlice_AdjustIndices(length, &start, &stop, step);
74
75 .. versionchanged:: 3.2
76 The parameter type for the *slice* parameter was ``PySliceObject*``
77 before.
78
79 .. versionchanged:: 3.6.1
80 If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400``
81 and ``0x03060000`` (not including) or ``0x03060100`` or higher
82 :c:func:`!PySlice_GetIndicesEx` is implemented as a macro using
83 :c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
84 Arguments *start*, *stop* and *step* are evaluated more than once.
85
86 .. deprecated:: 3.6.1
87 If ``Py_LIMITED_API`` is set to the value less than ``0x03050400`` or
88 between ``0x03060000`` and ``0x03060100`` (not including)
89 :c:func:`!PySlice_GetIndicesEx` is a deprecated function.
90
91
92 .. c:function:: int PySlice_Unpack(PyObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
93
94 Extract the start, stop and step data members from a slice object as
95 C integers. Silently reduce values larger than ``PY_SSIZE_T_MAX`` to
96 ``PY_SSIZE_T_MAX``, silently boost the start and stop values less than
97 ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step
98 values less than ``-PY_SSIZE_T_MAX`` to ``-PY_SSIZE_T_MAX``.
99
100 Return ``-1`` with an exception set on error, ``0`` on success.
101
102 .. versionadded:: 3.6.1
103
104
105 .. c:function:: Py_ssize_t PySlice_AdjustIndices(Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
106
107 Adjust start/end slice indices assuming a sequence of the specified length.
108 Out of bounds indices are clipped in a manner consistent with the handling
109 of normal slices.
110
111 Return the length of the slice. Always successful. Doesn't call Python
112 code.
113
114 .. versionadded:: 3.6.1
115
116
117 Ellipsis Object
118 ^^^^^^^^^^^^^^^
119
120
121 .. c:var:: PyTypeObject PyEllipsis_Type
122
123 The type of Python :const:`Ellipsis` object. Same asĀ :class:`types.EllipsisType`
124 in the Python layer.
125
126
127 .. c:var:: PyObject *Py_Ellipsis
128
129 The Python ``Ellipsis`` object. This object has no methods. Like
130 :c:data:`Py_None`, it is an :term:`immortal` singleton object.
131
132 .. versionchanged:: 3.12
133 :c:data:`Py_Ellipsis` is immortal.