Message 305881 - Python tracker

Message305881

Author Ronan.Lamy
Recipients Ronan.Lamy
Date 2017-11-08.16:59:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1510160383.84.0.213398074469.issue31984@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-11-08 16:59:43Ronan.Lamysetrecipients: + Ronan.Lamy
2017-11-08 16:59:43Ronan.Lamysetmessageid: <1510160383.84.0.213398074469.issue31984@psf.upfronthosting.co.za>
2017-11-08 16:59:43Ronan.Lamylinkissue31984 messages
2017-11-08 16:59:43Ronan.Lamycreate