New submission from Ronan Lamy <[email protected]>:
One would think that u.startswith(v, start, end) would be equivalent to
u[start: end].startswith(v), but one would be wrong. And the same goes for
endswith(). Here is the actual spec (for bytes, but str and bytearray are the
same), in the form of passing pytest+hypothesis tests:
from hypothesis import strategies as st, given
def adjust_indices(u, start, end):
if end < 0:
end = max(end + len(u), 0)
else:
end = min(end, len(u))
if start < 0:
start = max(start + len(u), 0)
return start, end
@given(st.binary(), st.binary(), st.integers(), st.integers())
def test_startswith_3(u, v, start, end):
if v:
expected = u[start:end].startswith(v)
else:
start0, end0 = adjust_indices(u, start, end)
expected = start0 <= len(u) and start0 <= end0
assert u.startswith(v, start, end) is expected
@given(st.binary(), st.binary(), st.integers(), st.integers())
def test_endswith_3(u, v, start, end):
if v:
expected = u[start:end].endswith(v)
else:
start0, end0 = adjust_indices(u, start, end)
expected = start0 <= len(u) and start0 <= end0
assert u.endswith(v, start, end) is expected
Fixing this behaviour to work in the "obvious" way would be simple: just add a
check for len(v) == 0 and always return True in that case.
----------
messages: 305881
nosy: Ronan.Lamy
priority: normal
severity: normal
status: open
title: startswith and endswith leak implementation details
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue31984>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com