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

startswith error message is incomplete #51029

Closed
srid mannequin opened this issue Aug 24, 2009 · 9 comments
Closed

startswith error message is incomplete #51029

srid mannequin opened this issue Aug 24, 2009 · 9 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@srid
Copy link
Mannequin

srid mannequin commented Aug 24, 2009

BPO 6780
Nosy @vstinner, @ezio-melotti, @merwok, @bitdancer
Files
  • issue6780.diff: patch for trunk
  • issue6780-2.diff: Patch against 2.7
  • 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/ezio-melotti'
    closed_at = <Date 2011-04-26.03:46:41.569>
    created_at = <Date 2009-08-24.21:33:11.254>
    labels = ['interpreter-core', 'type-bug']
    title = 'startswith error message is incomplete'
    updated_at = <Date 2011-04-27.10:16:11.864>
    user = 'https://bugs.python.org/srid'

    bugs.python.org fields:

    activity = <Date 2011-04-27.10:16:11.864>
    actor = 'ezio.melotti'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2011-04-26.03:46:41.569>
    closer = 'ezio.melotti'
    components = ['Interpreter Core']
    creation = <Date 2009-08-24.21:33:11.254>
    creator = 'srid'
    dependencies = []
    files = ['16538', '21762']
    hgrepos = []
    issue_num = 6780
    keywords = ['patch', 'needs review']
    message_count = 9.0
    messages = ['91942', '91949', '101042', '123264', '124317', '134321', '134442', '134546', '134547']
    nosy_count = 7.0
    nosy_names = ['vstinner', 'ezio.melotti', 'eric.araujo', 'r.david.murray', 'srid', 'santoso.wijaya', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue6780'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2', 'Python 3.3']

    @srid
    Copy link
    Mannequin Author

    srid mannequin commented Aug 24, 2009

    The startswith method accepts both string and tuple (not list). Yet
    the error message suggests that it expects (only) a character buffer
    object.

    In Python-2.6:

    >>> "foo".startswith(['fo', 'df'])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected a character buffer object

    In Python-3.x, the error message is different:

    >>> "foo".startswith(["fo"])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Can't convert 'list' object to str implicitly

    Aside: why not try to convert 'list' object to tuple?

    @srid srid mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 24, 2009
    @ezio-melotti
    Copy link
    Member

    In the examples you used byte strings for Py2 and Unicode strings for
    Py3. On Py3 the same example with byte strings gives an error similar to
    the one raised by Py2:

    >>> b"foo".startswith([b"fo"])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected an object with the buffer interface
    (vs. Py2's expected a character buffer object)

    The error raised by Py2 with Unicode strings is more or less the same of
    Py3 too:

    >>> u"foo".startswith([u"fo", u"df"])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: coercing to Unicode: need string or buffer, list found
    (vs. Py3's Can't convert 'list' object to str implicitly)

    If I understood correctly the C code in /Objects/unicodeobject.c, this
    is because startswith checks if the 'prefix' is a tuple and, if not, it
    assumes that is a Unicode string. The 'prefix' is then converted to
    Unicode by PyUnicode_FromObject and if it's a list or some other object
    the error "Can't convert 'list' object to str implicitly" / "coercing to
    Unicode: need string or buffer, list found" is raised.

    I agree that a more explicit error message would be better, something
    like: "'prefix' must be a character buffer object or a tuple, not 'list'".

    Aside: why not try to convert 'list' object to tuple?

    If the support for lists is added, it should probably be extended to all
    the iterables, but strings are iterables too, so that will create some
    problem. It could be checked if 'prefix' is a string and if not assume
    that is an iterable of strings, but I don't know if it's worth doing it.

    @ezio-melotti ezio-melotti added interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed stdlib Python modules in the Lib dir labels Aug 25, 2009
    @ezio-melotti ezio-melotti self-assigned this Feb 27, 2010
    @ezio-melotti
    Copy link
    Member

    Here is a proof of concept that solves the problem for unicode strings and startswith/endswith.
    If during the conversion to Unicode a TypeError is raised (e.g. TypeError: Can't convert 'list' object to str implicitly), the error message is changed to "TypeError: startswith first arg must be str, unicode, or tuple, not list".
    If the error is not a TypeError (e.g. a UnicodeDecodeError) the behavior is unchanged.
    I haven't tested the patch thoroughly, but if this approach is OK I will prepare a complete patch.

    @vstinner
    Copy link
    Member

    vstinner commented Dec 3, 2010

    See also bpo-10616.

    @bitdancer
    Copy link
    Member

    The approach looks good to me. I think this issue is orthogonal to bpo-10616, since the message here needs to be modified anyway, regardless of what happens to the underlying issue.

    @ezio-melotti
    Copy link
    Member

    Attached an updated patch for 2.7 with tests that check that UnicodeErrors are still raised and that the error message mentions 'str', 'unicode' and 'tuple'.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 26, 2011

    New changeset 3ceeccbc2c3b by Ezio Melotti in branch '2.7':
    bpo-6780: fix starts/endswith error message to mention that tuples are accepted too.
    http://hg.python.org/cpython/rev/3ceeccbc2c3b

    New changeset bcbf8c3c4a88 by Ezio Melotti in branch '3.1':
    bpo-6780: fix starts/endswith error message to mention that tuples are accepted too.
    http://hg.python.org/cpython/rev/bcbf8c3c4a88

    New changeset f393c507717a by Ezio Melotti in branch '3.2':
    bpo-6780: merge with 3.1.
    http://hg.python.org/cpython/rev/f393c507717a

    New changeset a1a1296556d7 by Ezio Melotti in branch 'default':
    bpo-6780: merge with 3.2.
    http://hg.python.org/cpython/rev/a1a1296556d7

    @merwok
    Copy link
    Member

    merwok commented Apr 27, 2011

    I would have used with self.assertRaises to write the tests, thinking it would be less verbose/cumbersome.

    @ezio-melotti
    Copy link
    Member

    In 3.1 I had to use try/except because cm.exception is new in 2.7/3.2.
    I used assertRaises on the other branches.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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

    4 participants