[Python-bugs-list] [ python-Bugs-560064 ] inconsistent behavior of __getslice__

noreply@sourceforge.net noreply@sourceforge.net
Wed, 19 Jun 2002 23:22:52 -0700


Bugs item #560064, was opened at 2002-05-24 06:59
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=560064&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Cesar Douady (douady)
>Assigned to: Nobody/Anonymous (nobody)
Summary: inconsistent behavior of __getslice__

Initial Comment:
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.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-06-05 14:23

Message:
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.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-24 07:58

Message:
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?


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=560064&group_id=5470