Issue 24284: Inconsistency in startswith/endswith (original) (raw)
Created on 2015-05-25 11:41 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.
Messages (8)
Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2015-05-25 11:41
The behavior of startswith in corner case is inconsistent between str and bytes in Python 3, str and unicode in Python 2, and between str in Python 2 and Python 3.
Python 3:
''.startswith('', 1, 0) True b''.startswith(b'', 1, 0) False
Python 2:
''.startswith('', 1, 0) False u''.startswith(u'', 1, 0) True
If define s1.startswith(s2, start, end) for non-negative indices and non-tuple s2 as an equivalent to the expression start + len(s2) <= end and s2[start: start + len(s2)] == s2
or to s1.find(s2, start, end) == start
, "".startswith("", 1, 0) should be False.
The same issue exists for endswith. See for more detailed discussion.
Proposed patch fixes str.startswith and str.endswith.
Author: R. David Murray (r.david.murray) *
Date: 2015-05-25 14:02
I think this can only be applied in a feature release (and I think it should be, because of the backward-compatibility-with-python2 issue). However, since this is potentially controversial, we need some more opinions.
Author: Martin Panter (martin.panter) *
Date: 2015-05-26 00:19
I can’t imagine much code would rely on either old or new behaviour. If you only put it into a feature release, would you have to document it as a change in behaviour?
Author: R. David Murray (r.david.murray) *
Date: 2015-05-26 14:43
Just in the what's new porting section, I think. The fact that there "should" be very little to no code that relies on this is why I'd like to see it fixed. The fact that the report was a theoretical one, and not one that broke code, is why I think we should fix it only in 3.5.
Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2015-05-26 18:56
Could you please help me with wording?
Author: Martin Panter (martin.panter) *
Date: 2015-05-26 22:14
How about this for What’s New:
- The :meth:
str.startswith
and :meth:str.endswith
methods no longer returnTrue
when finding the empty string and the indexes are completely out of range. See :issue:24284
.
Perhaps that is good enough, but if you wanted to be more precise, I think the actual condition is if the start index is beyond the end of the string, or end is before start.
Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2015-05-31 06:09
Thank you Martin.
Author: Roundup Robot (python-dev)
Date: 2015-05-31 06:17
New changeset a82498f424fe by Serhiy Storchaka in branch '3.5': Issue #24284: The startswith and endswith methods of the str class no longer https://hg.python.org/cpython/rev/a82498f424fe
New changeset 04162e06323f by Serhiy Storchaka in branch 'default': Issue #24284: The startswith and endswith methods of the str class no longer https://hg.python.org/cpython/rev/04162e06323f
History
Date
User
Action
Args
2022-04-11 14:58:17
admin
set
github: 68472
2015-05-31 06:17:31
serhiy.storchaka
set
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2015-05-31 06:17:05
python-dev
set
nosy: + python-dev
messages: +
2015-05-31 06:09:05
serhiy.storchaka
set
assignee: serhiy.storchaka
messages: +
versions: + Python 3.6
2015-05-26 22:14:53
martin.panter
set
messages: +
2015-05-26 18:56:46
serhiy.storchaka
set
messages: +
2015-05-26 14:43:20
r.david.murray
set
messages: +
2015-05-26 00:19:28
martin.panter
set
nosy: + martin.panter
messages: +
2015-05-25 22:01:43
rhettinger
set
nosy: + rhettinger
2015-05-25 14:02:32
r.david.murray
set
nosy: + r.david.murray
messages: +
versions: - Python 2.7, Python 3.4, Python 3.6
2015-05-25 11:41:27
serhiy.storchaka
create