Skip to content

to_bytes() doesn't handle negative numbers properly #2579

@dhalbert

Description

@dhalbert
>>> (-2).to_bytes(2,'big',True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: value must fit in 2 byte(s)

Works properly in CPython, except the third argument is a keyword-only argument in CPython, so you have to use .to_bytes(2, 'big', signed=True). That should be fixed too.

The workaround is to use struct.pack(), which does not throw an error.

Thanks to @jasonp for catching this.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions