This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author akira
Recipients akira, docs@python
Date 2017-01-23.16:49:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1485190156.24.0.631194950251.issue29352@psf.upfronthosting.co.za>
In-reply-to
Content
I've failed to find where the behavior for negative indices in s[i:j]
expression (i, j < -len(s)) for standard sequences (str, list, etc) is
formally defined.

The observed behavior implemented in PySlice_GetIndicesEx(): If "len(s)
+ i" or "len(s) + j" is negative, use 0. [1] I don't see it in the docs.

        if (*start < 0) *start += length;
        if (*start < 0) *start = (*step < 0) ? -1 : 0;
        ...
        if (*stop < 0) *stop += length;
        if (*stop < 0) *stop = (*step < 0) ? -1 : 0;

The tutorial mentions [2]:

> out of range slice indexes are handled gracefully when used for
> slicing"

slice.indices() documentation says [3]:

> Missing or out-of-bounds indices are handled in a manner consistent
> with regular slices.

Neither define it explicitly.

The behavior for the upper boundary is defined explicitly [4]:

> If *i* or *j* is greater than ``len(s)``, use ``len(s)``

I've added the documentation patch that defines the behavior for the
lower boundary too.

[1] Objects/sliceobject.c
[2] Doc/tutorial/introduction.rst
[3] Doc/reference/datamodel.rst
[4] Doc/library/stdtypes.rst
History
Date User Action Args
2017-01-23 16:49:16akirasetrecipients: + akira, docs@python
2017-01-23 16:49:16akirasetmessageid: <1485190156.24.0.631194950251.issue29352@psf.upfronthosting.co.za>
2017-01-23 16:49:16akiralinkissue29352 messages
2017-01-23 16:49:16akiracreate