Issue 34955: passing a dict to bytes() gives unhelpful error message (original) (raw)

Created on 2018-10-10 19:55 by marnanel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg327490 - (view) Author: Marnanel Thurman (marnanel) Date: 2018-10-10 19:55
bytes() doesn't accept a dict as parameter. If you attempt to pass one, you receive a TypeError with the baffling message "'str' object cannot be interpreted as an integer". >> bytes({'a':1}) Traceback (most recent call last): File "", line 1, in TypeError: 'str' object cannot be interpreted as an integer
msg327492 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-10 20:55
bytes() accepts: 1. An integer. 2. An object supporting the buffer protocol. 3. An iterable of integers in the range 0 to 255. Dict is an iterable. But iterating it produces string object which cannot be interpreted as an integer.
msg327501 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-10-10 22:22
You can in fact pass a dict to bytes(), as long as the keys are ints in the correct range: >>> bytes({0:10, 1:20}) b'\x00\x01' I'm not claiming it's very useful, but it does conform to the docs. I'm not sure the error message can be improved, so I suggest closing this issue.
msg327514 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-10-11 04:35
> I'm not sure the error message can be improved, so I suggest closing this issue. I concur.
History
Date User Action Args
2022-04-11 14:59:07 admin set github: 79136
2018-10-11 05:49:25 rhettinger set status: open -> closedstage: resolved
2018-10-11 04:35:20 rhettinger set nosy: + rhettingermessages: + assignee: rhettingerresolution: not a bug
2018-10-10 22:22:11 eric.smith set nosy: + eric.smithmessages: +
2018-10-10 20:55:14 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2018-10-10 19:55:15 marnanel create