blob: 8271d9acfb645e182beda6727c6dd3c039b0d9bc [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _slice-objects:
4
5Slice Objects
6-------------
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:var:: PyTypeObject PySlice_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
Georg Brandl2aff3352010-10-17 10:59:41 +000011 The type object for slice objects. This is the same as :class:`slice` in the
12 Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000013
14
Georg Brandl60203b42010-10-06 10:11:56 +000015.. c:function:: int PySlice_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000016
Antonio Cuni315fc522021-01-06 12:38:26 +010017 Return true if *ob* is a slice object; *ob* must not be ``NULL``. This
18 function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20
Georg Brandl60203b42010-10-06 10:11:56 +000021.. c:function:: PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23 Return a new slice object with the given values. The *start*, *stop*, and
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000024 *step* parameters are used as the values of the slice object attributes of
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020025 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
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000027 the new object could not be allocated.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29
Martin v. Löwisdd590972010-12-11 18:17:22 +000030.. c:function:: int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 Retrieve the start, stop and step indices from the slice object *slice*,
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000033 assuming a sequence of length *length*. Treats indices greater than
34 *length* as errors.
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +030036 Returns ``0`` on success and ``-1`` on error with no exception set (unless one of
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000037 the indices was not :const:`None` and failed to be converted to an integer,
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +030038 in which case ``-1`` is returned with an exception set).
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
Georg Brandle6bcc912008-05-12 18:05:20 +000040 You probably do not want to use this function.
Georg Brandl54a3faa2008-01-20 09:30:57 +000041
Martin v. Löwis6d9839d2010-12-11 19:22:04 +000042 .. versionchanged:: 3.2
Georg Brandl18d378d2010-12-11 22:19:34 +000043 The parameter type for the *slice* parameter was ``PySliceObject*``
44 before.
Martin v. Löwis6d9839d2010-12-11 19:22:04 +000045
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
Martin v. Löwisdd590972010-12-11 18:17:22 +000047.. 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)
Georg Brandl54a3faa2008-01-20 09:30:57 +000048
Georg Brandl60203b42010-10-06 10:11:56 +000049 Usable replacement for :c:func:`PySlice_GetIndices`. Retrieve the start,
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000050 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.
Georg Brandl54a3faa2008-01-20 09:30:57 +000054
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +030055 Returns ``0`` on success and ``-1`` on error with exception set.
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000056
Serhiy Storchaka4d3f0842017-10-08 12:53:34 +030057 .. 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
Martin v. Löwis6d9839d2010-12-11 19:22:04 +000073 .. versionchanged:: 3.2
Georg Brandl18d378d2010-12-11 22:19:34 +000074 The parameter type for the *slice* parameter was ``PySliceObject*``
75 before.
Serhiy Storchaka6e08baf2017-01-25 13:27:44 +020076
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
Serhiy Storchaka4d3f0842017-10-08 12:53:34 +030081 :c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
Serhiy Storchaka6e08baf2017-01-25 13:27:44 +020082 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
Xiang Zhang2ddf5a12017-05-10 18:19:41 +080095 ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step
Serhiy Storchaka6e08baf2017-01-25 13:27:44 +020096 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
Michael Seifert0dc5c312017-04-14 21:18:35 +0200113
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.