Python C code that processes slices for objects that implement the sq_slice slot cannot process integers over Py_ssize_t (== sys.maxsize ). The word sq_slice is the C-API equivalent of the __getslice__ special method.
For a two-element slice, Python 2 uses one of SLICE+* opcodes ; this is then handled by the apply_slice() function. This uses the _PyEval_SliceIndex function to convert Python index objects ( int , long or something that implements the __index__ method ) to an integer Py_ssize_t . The method has the following comment:
This means that any slicing in Python 2 using a 2-digit value syntax is limited to values ββin the sys.maxsize range when the sq_slice slot is sq_slice .
Slicing using a three-digit form ( item[start:stop:stride] ) instead uses the BUILD_SLICE (then BINARY_SUBSCR ), and instead creates a slice() object, not limited to sys.maxsize .
If the object does not implement the sq_slice() slot (therefore there is no __getslice__ ), the apply_slice() function apply_slice() also return to using the slice() object.
As for whether this is an implementation detail or part of a language: The section expression documentation distinguishes between simple_slicing and extended_slicing ; the first only allows the form short_slice . For simple slicing, indices should be integers:
The lower and upper boundary expressions, if any, should be evaluated equal to integers; the default values ββare zero and sys.maxint , respectively.
This suggests that Python 2 restricts indexes to sys.maxint values ββby prohibiting long integers. In Python 3, simple slicing was cut from the whole language.
If your code should support slicing with values ββgreater than sys.maxsize , and you need to inherit from a type that implements __getslice__ , then your options:
slice() objects can handle long integers just fine; however, the slice.indices() method cannot handle lengths by sys.maxsize yet:
>>> import sys >>> s = slice(0, sys.maxsize + 1) >>> s slice(0, 9223372036854775808L, None) >>> s.stop 9223372036854775808L >>> s.indices(sys.maxsize + 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: cannot fit 'long' into an index-sized integer