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
Comments
The 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? |
In the examples you used byte strings for Py2 and Unicode strings for >>> 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 >>> 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 I agree that a more explicit error message would be better, something
If the support for lists is added, it should probably be extended to all |
Here is a proof of concept that solves the problem for unicode strings and startswith/endswith. |
See also bpo-10616. |
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. |
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'. |
New changeset 3ceeccbc2c3b by Ezio Melotti in branch '2.7': New changeset bcbf8c3c4a88 by Ezio Melotti in branch '3.1': New changeset f393c507717a by Ezio Melotti in branch '3.2': New changeset a1a1296556d7 by Ezio Melotti in branch 'default': |
I would have used with self.assertRaises to write the tests, thinking it would be less verbose/cumbersome. |
In 3.1 I had to use try/except because cm.exception is new in 2.7/3.2. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: