Skip to main content

Objects/sliceobject.c

cpython 3.14 @ ab2d84fe1023/Objects/sliceobject.c

Objects/sliceobject.c defines PySliceObject and the Ellipsis singleton. Slices store start, stop, and step as Python objects (which may be None). The helper functions compute concrete integer indices from these objects given a sequence length.

Map

LinesSymbolRole
1-80EllipsisObject, Py_EllipsisThe ... singleton
81-200PySlice_New, slice_reprSlice construction and display
201-320PySlice_GetIndicesExCompute concrete bounded indices (deprecated path)
321-440PySlice_Unpack, PySlice_AdjustIndicesNew 3.6+ split API

Reading

PySlice_Unpack

PySlice_Unpack(slice, &start, &stop, &step) converts None to default values without bounds-clamping. step defaults to 1; start defaults to 0 or PY_SSIZE_T_MAX depending on step sign; stop defaults to PY_SSIZE_T_MAX or PY_SSIZE_T_MIN.

// CPython: Objects/sliceobject.c:330 PySlice_Unpack
int
PySlice_Unpack(PyObject *_r,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
{
PySliceObject *r = (PySliceObject *)_r;
if (r->step == Py_None) {
*step = 1;
} else {
if (!_PyEval_SliceIndex(r->step, step)) return -1;
if (*step == 0) {
PyErr_SetString(PyExc_ValueError, "slice step cannot be zero");
return -1;
}
}
...
}

PySlice_AdjustIndices

PySlice_AdjustIndices(length, &start, &stop, step) clamps start and stop into [0, length] and returns the number of elements in the slice. This is the second step after PySlice_Unpack; together they replace the older single-call PySlice_GetIndicesEx.

Ellipsis

Py_Ellipsis is the singleton .... It has no state; PyEllipsis_Type only defines __repr__ as "Ellipsis". It is used in extended slicing (a[..., 0]) and as a type annotation placeholder.

gopy notes

PySliceObject is implemented in the core objects layer. PySlice_Unpack and PySlice_AdjustIndices are called by __getitem__ implementations for list, tuple, str, and bytes when the index is a slice object.