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: string.{starts,ends}with vs slices
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: nnorwitz Nosy List: barry, gvanrossum, nnorwitz, tim.peters
Priority: normal Keywords:

Created on 2001-12-16 19:23 by tim.peters, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
check.py tim.peters, 2001-12-16 19:24
str-meth.diff nnorwitz, 2002-06-12 22:08 impl patch for stringobject.c
str-meth.diff nnorwitz, 2002-06-14 00:51 impl patch for stringobject.c
Messages (10)
msg8260 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-12-16 19:23
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.
msg8261 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-12-16 19:24
Logged In: YES 
user_id=31435

Attached the test case.
msg8262 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-12-16 19:45
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. :-)
msg8263 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-12-16 20:03
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).
msg8264 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2002-04-22 14:11
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).
msg8265 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-04-22 14:31
Logged In: YES 
user_id=6380

We should fix this in 2.3 at the very least. Leave 2.2.x
alone.
msg8266 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-06-12 22:08
Logged In: YES 
user_id=33168

Attached is a patch which implements this behaviour.
Still need a patch for tests and the doc.
This passes all regressions and Tim's test.
msg8267 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-06-13 16:23
Logged In: YES 
user_id=6380

Check in in, Neal. (Barry is too busy to watch SF.)

Would you mind adding unit tests?
msg8268 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-06-13 19:07
Logged In: YES 
user_id=33168

I'll update the tests & doc before checking in.
msg8269 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-06-14 00:51
Logged In: YES 
user_id=33168

Checked in as:
  Doc/whatsnew/whatsnew23.tex 1.25
  Lib/test/string_tests.py 1.18
  Objects/stringobject.c 2.168
  Misc/NEWS 1.427
History
Date User Action Args
2022-04-10 16:04:46adminsetgithub: 35757
2001-12-16 19:23:40tim.peterscreate