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.

classification
Title: bytes() TypeError message is misleadingly narrow
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: eric.araujo, ezio.melotti, martin.panter, python-dev, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2013-03-02 19:58 by terry.reedy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bytes_from_object_error_msg.patch serhiy.storchaka, 2016-04-09 12:36 review
Messages (10)
msg183345 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-03-02 19:58
If an object ob does not have a __bytes__ method or buffer interface and is not a string, integer, or iterable, bytes(ob) fails with 

TypeError: 'object' object is not iterable

This is misleadingly narror (similar to #17032). We should either list *all* the things that ob is not

'object' object is not a string, integer, or iterable and does not have a __bytes__ method or buffer interface

or simply say

'object' object cannot be converted to bytes

and let the programmer recheck what can be.
msg183348 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-02 20:33
See also #16518.
msg183350 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-03-02 22:36
Do you prefer the long or short form?
msg183355 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-03-03 02:21
Short form for me.
msg183474 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-04 16:43
> 'object' object cannot be converted to bytes

This is not entirely accurate for the bytes(int) case though, since it's not converting the int to bytes.  OTOH if we want to keep the message short we have to make compromises about accuracy and completeness.
msg263088 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-09 12:36
Here is a patch.
msg263098 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-04-09 14:03
I think this patch is okay.

I would have suggested “Cannot construct bytes from [. . .]” to avoid the problem with “convert”, but this message is produced in places other than the constructor, e.g.

>>> int.from_bytes("str", "little")
TypeError: cannot convert unicode object to bytes
msg263101 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-09 16:10
Thank you Martin.

Interesting, int.from_bytes() accepts lists (and other iterables):

>>> int.from_bytes([1, 2, 3], "little")
197121

Is it intentional?
msg263131 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-04-10 11:45
New changeset 6b16eec56854 by Serhiy Storchaka in branch 'default':
Issue #17339: Improved TypeError message in bytes constructor.
https://hg.python.org/cpython/rev/6b16eec56854
msg263398 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-04-14 12:17
The int.from_bytes() behaviour seems to have been documented from the beginning, so maybe it was intended.
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61541
2016-04-14 12:17:57martin.pantersetmessages: + msg263398
2016-04-10 11:51:45serhiy.storchakasetversions: - Python 3.5
2016-04-10 11:51:37serhiy.storchakasetstatus: open -> closed
assignee: serhiy.storchaka
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.5
2016-04-10 11:45:44python-devsetnosy: + python-dev
messages: + msg263131
2016-04-09 16:10:30serhiy.storchakasetmessages: + msg263101
2016-04-09 14:03:02martin.pantersetnosy: + martin.panter
messages: + msg263098
2016-04-09 12:36:02serhiy.storchakasetfiles: + bytes_from_object_error_msg.patch

versions: + Python 3.6, - Python 3.4
keywords: + patch
nosy: + serhiy.storchaka

messages: + msg263088
stage: needs patch -> patch review
2013-03-04 16:43:40ezio.melottisetmessages: + msg183474
2013-03-03 02:21:31eric.araujosetnosy: + eric.araujo
messages: + msg183355
2013-03-02 22:36:17terry.reedysetmessages: + msg183350
2013-03-02 20:33:42ezio.melottisetnosy: + ezio.melotti
messages: + msg183348
2013-03-02 19:58:34terry.reedycreate