[New-bugs-announce] [issue24284] Inconsistency in startswith/endswith

Serhiy Storchaka report at bugs.python.org
Mon May 25 13:41:27 CEST 2015


New submission from Serhiy Storchaka:

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 issue24243 for more detailed discussion.

Proposed patch fixes str.startswith and str.endswith.

----------
components: Interpreter Core
files: str_tailmatch.patch
keywords: patch
messages: 244027
nosy: serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Inconsistency in startswith/endswith
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file39492/str_tailmatch.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24284>
_______________________________________


More information about the New-bugs-announce mailing list