[issue27637] int.to_bytes(-1, ...) should automatically choose required count of bytes

Марк Коренберг report at bugs.python.org
Wed Jul 27 23:15:12 EDT 2016


New submission from Марк Коренберг:

It will be nice if `int.to_bytes` be able to automatically choose number of bytes to serialize. If so, I could write serialisation code:

def serialize(value: int, signed=True) -> bytes:
    x = value.to_bytes(-1, 'big', signed=signed)
    l = value.to_bytes(4, 'big', signed=False)
    return l + x

assert len(serialize(0)) == 4 + 0 # see Issue27623
assert len(serialize(120)) == 4 + 1
assert len(serialize(130)) == 4 + 2
assert len(serialize(130), False) == 4 + 1

----------
components: Library (Lib)
messages: 271488
nosy: mmarkk
priority: normal
severity: normal
status: open
title: int.to_bytes(-1, ...) should automatically choose required count of bytes
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27637>
_______________________________________


More information about the Python-bugs-list mailing list