[Python-bugs-list] [ python-Bugs-493951 ] string.{starts,ends}with vs slices

noreply@sourceforge.net noreply@sourceforge.net
Mon, 22 Apr 2002 07:31:01 -0700


Bugs item #493951, was opened at 2001-12-16 14:23
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=493951&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: Remind
Priority: 5
Submitted By: Tim Peters (tim_one)
Assigned to: Barry Warsaw (bwarsaw)
Summary: string.{starts,ends}with vs slices

Initial Comment:
The attached test shows many cases where 
string.startswith() and string.endswith() fail to 
handle negative slice indices correctly.  The failures 
are in both the start and end indices.  I stumbled 
into this when tracking down why (what turned out to 
be) this didn't work:

>>> file = 'difflib.pyc'
>>> file.endswith('.py', 0, -1) # should be 1
0
>>> file[0:-1].endswith('.py')  # like this is
1
>>>

Can we risk changing this for 2.2?  Don't know.  If 
not, reduce the priority.

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

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-04-22 10:31

Message:
Logged In: YES 
user_id=6380

We should fix this in 2.3 at the very least. Leave 2.2.x
alone.

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

Comment By: Barry Warsaw (bwarsaw)
Date: 2002-04-22 10:11

Message:
Logged In: YES 
user_id=12800

The comment in the code reminds me that we adopted the
semantics for Java's String.startsWith() and String.endsWith(): 

http://java.sun.com/products/jdk/1.2/docs/api/java/lang/String.html#startsWith(java.lang.String,%20int)

which treats negative indices as errors (at least for the
start parameter -- which we generalized to include the end
parameter).  Changing this is a semantic change to the
string methods, so I don't think it should be undertaken
lightly (i.e. as a bug in the current implementation).

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

Comment By: Tim Peters (tim_one)
Date: 2001-12-16 15:03

Message:
Logged In: YES 
user_id=31435

I can't guess what the docs intended to say (they're vague -
- I can read them several ways).

In all other cases where optional string-method args 
named "start" and "end" are accepted (find(), rfind(), count
(), index(), rindex()), the docs are clear that they're 
interpreted as slice notation.  It doesn't make sense for 
the two newest methods to behave differently in this 
respect.

The unit tests aren't evidence one way or the other to me --
 the behavior of negative indices should be covered by the 
tests no matter what the intended semantics.

Overall, I expect the behavior here is an accident (as 
opposed to intended).

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-12-16 14:45

Message:
Logged In: YES 
user_id=6380

Hm, the docs don't exactly suggest that negative indices
would be supported. While that would be nice, it's
definitely a feature request, so no, please don't do this in
2.2. (The absent of unit tests for negative indices also
suggests this was not an oversight. :-)

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

Comment By: Tim Peters (tim_one)
Date: 2001-12-16 14:24

Message:
Logged In: YES 
user_id=31435

Attached the test case.

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

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