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

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy