Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in startswith/endswith #68472

Closed
serhiy-storchaka opened this issue May 25, 2015 · 8 comments
Closed

Inconsistency in startswith/endswith #68472

serhiy-storchaka opened this issue May 25, 2015 · 8 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 24284
Nosy @rhettinger, @bitdancer, @vadmium, @serhiy-storchaka
Files
  • str_tailmatch.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-05-31.06:17:31.577>
    created_at = <Date 2015-05-25.11:41:27.565>
    labels = ['interpreter-core', 'type-bug']
    title = 'Inconsistency in startswith/endswith'
    updated_at = <Date 2015-05-31.06:17:31.577>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-05-31.06:17:31.577>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-05-31.06:17:31.577>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2015-05-25.11:41:27.565>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['39492']
    hgrepos = []
    issue_num = 24284
    keywords = ['patch']
    message_count = 8.0
    messages = ['244027', '244036', '244068', '244106', '244124', '244139', '244520', '244521']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'r.david.murray', 'python-dev', 'martin.panter', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24284'
    versions = ['Python 3.5', 'Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

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

    Proposed patch fixes str.startswith and str.endswith.

    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels May 25, 2015
    @bitdancer
    Copy link
    Member

    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.

    @vadmium
    Copy link
    Member

    vadmium commented May 26, 2015

    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?

    @bitdancer
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member Author

    Could you please help me with wording?

    @vadmium
    Copy link
    Member

    vadmium commented May 26, 2015

    How about this for What’s New:

    • The :meth:`str.startswith` and :meth:`str.endswith` methods no longer return ``True`` 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*.

    @serhiy-storchaka
    Copy link
    Member Author

    Thank you Martin.

    @serhiy-storchaka serhiy-storchaka self-assigned this May 31, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 31, 2015

    New changeset a82498f424fe by Serhiy Storchaka in branch '3.5':
    Issue bpo-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 bpo-24284: The startswith and endswith methods of the str class no longer
    https://hg.python.org/cpython/rev/04162e06323f

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants