This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ezio.melotti
Recipients ezio.melotti, srid
Date 2009-08-25.09:23:28
SpamBayes Score 5.551115e-17
Marked as misclassified No
Message-id <1251192212.35.0.362418346989.issue6780@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2009-08-25 09:23:32ezio.melottisetrecipients: + ezio.melotti, srid
2009-08-25 09:23:32ezio.melottisetmessageid: <1251192212.35.0.362418346989.issue6780@psf.upfronthosting.co.za>
2009-08-25 09:23:30ezio.melottilinkissue6780 messages
2009-08-25 09:23:29ezio.melotticreate