Skip to content

passing a dict to bytes() gives unhelpful error message #79136

Closed

Description

@marnanel
mannequin
BPO 34955
Nosy @rhettinger, @ericvsmith, @serhiy-storchaka, @marnanel

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/rhettinger'
closed_at = <Date 2018-10-11.05:49:25.798>
created_at = <Date 2018-10-10.19:55:15.519>
labels = ['interpreter-core', 'invalid']
title = 'passing a dict to bytes() gives unhelpful error message'
updated_at = <Date 2018-10-11.05:49:25.798>
user = 'https://github.com/marnanel'

bugs.python.org fields:

activity = <Date 2018-10-11.05:49:25.798>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2018-10-11.05:49:25.798>
closer = 'rhettinger'
components = ['Interpreter Core']
creation = <Date 2018-10-10.19:55:15.519>
creator = 'marnanel'
dependencies = []
files = []
hgrepos = []
issue_num = 34955
keywords = []
message_count = 4.0
messages = ['327490', '327492', '327501', '327514']
nosy_count = 4.0
nosy_names = ['rhettinger', 'eric.smith', 'serhiy.storchaka', 'marnanel']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue34955'
versions = ['Python 3.5']

Activity

  1. marnanel commented on Oct 10, 2018

    marnanelmannequin
    MannequinAuthor

    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 "<stdin>", line 1, in <module>
     TypeError: 'str' object cannot be interpreted as an integer
  2. added
    interpreter-core
    on Oct 10, 2018
  3. serhiy-storchaka commented on Oct 10, 2018

    @serhiy-storchaka
    Member

    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.

  4. ericvsmith commented on Oct 10, 2018

    @ericvsmith
    Member

    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.

  5. rhettinger commented on Oct 11, 2018

    @rhettinger
    Contributor

    I'm not sure the error message can be improved, so I suggest closing this issue.

    I concur.

  6. self-assigned this
    on Oct 11, 2018
  7. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions