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.

classification
Title: inconsistent behavior of __getslice__
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, douady, gvanrossum, rhettinger
Priority: normal Keywords:

Created on 2002-05-24 11:59 by douady, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
str.diff rhettinger, 2002-05-24 12:58 Patch to stringobject.c
Messages (6)
msg10917 - (view) Author: Cesar Douady (douady) Date: 2002-05-24 11:59
The following trace shows what I mean:
>>> help(str.__getslice__)
Help on wrapper_descriptor:

__getslice__(...)
    x.__getslice__(i, j) <==> x[i:j]
>>> 'ab'[-1]
'b'
>>> 'ab'.__getitem__(-1)
'b'
>>> 'ab'[-2:-1]
'a'
>>> 'ab'.__getslice__(-2,-1)
''

that is, __getslice__ does not add the length of the
sequence to its arguments when they are negative as
indexing with [:] does.
This contradicts the docstring which says __getslice__
is equivalent to indexing.
The same behavior applies to lists.

Note that __getitem__ does it right.

This bug may be linked to bug 473985 ('str, __getitem__
and slices') but should be much easier to fix.
msg10918 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-24 12:58
Logged In: YES 
user_id=80475

Tim,  I attached a tiny patch which fixes the behavior for 
strings.  I've also tried out the same change in listobject.c 
and unicodeobject.c.  Is this the right this to do or are 
there more subtleties to this than meet the eye?
msg10919 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-05 19:23
Logged In: YES 
user_id=6380

This should NOT be fixed this way, Raymond!

The docs are (subtly) wrong. For historical reasons, 
__getslice__ expects the Python VM to do the adjustment of 
negative indices. If you did another adjustment inside getslice, 
certain indices would start behaving differently (e.g. s[:-5] 
where len(s)==3).

Maybe the docstring should be fixed.
msg10920 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-17 02:19
Logged In: YES 
user_id=357491

If the docstring fix is still the best way to fix this I am willing to make the 
change.
msg10921 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-05-18 21:05
Logged In: YES 
user_id=6380

Yes, please fix the docstring.
msg10922 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-20 02:43
Logged In: YES 
user_id=357491

Fixed as revision 2.232 on HEAD (after getting it right; bah) and as 
2.126.4.35 on release22-maint.
History
Date User Action Args
2022-04-10 16:05:20adminsetgithub: 36642
2002-05-24 11:59:51douadycreate