Skip to content

int2byte does not match documentation on Python 3 #163

@benjaminp

Description

@benjaminp

Originally reported by: Evan (Bitbucket: evan_, GitHub: Unknown)


The documentation states that int2byte is "equivalent to [...] bytes((i,)) in Python 3", which is not true for invalid values.

#!python
>>> bytes(('a',))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>> int2byte('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: required argument is not an integer
>>>
>>> bytes((256,))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: bytes must be in range(0, 256)
>>> int2byte(256)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: ubyte format requires 0 <= number <= 255

From #122 I can see that performance is a concern. Personally I'd like to see this implemented as described in the documentation, but alternatively the documentation could be updated to mention the inconsistency.


Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions