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

2021-10-04 Thread Lorenz Panny
Lorenz Panny added the comment: I would like to express my support for making length=None to automatically use the minimal possible length. It's true that this will rarely be needed in production-grade serialization code, but this functionality is worth its weight in gold for quickly

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

2016-07-28 Thread Mark Dickinson
Mark Dickinson added the comment: [Martin] > I don’t like special values. Agreed. If we wanted to add this, the obvious API would be to simply make the size optional (which would force passing the endianness by name or explicitly passing a default value of `None`, but that doesn't seem like

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

2016-07-28 Thread Марк Коренберг
Марк Коренберг added the comment: https://github.com/pyca/cryptography/issues/3064 -- ___ Python tracker ___

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

2016-07-28 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: This is rarely needed, mainly in general serializers like pickle. The code for determining the minimal number of bytes is not trivial, but it depends on the serializer. If you always serialize unsigned values and saves the sign separately, or use one's

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

2016-07-27 Thread Martin Panter
Martin Panter added the comment: I don’t like special values. A length of minus one makes no sense, so should trigger an exception, not some unexpected behaviour. A different data type like None would be a bit better. But I’m not sure this would be widely used. If you really need it you could

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

2016-07-27 Thread Марк Коренберг
Марк Коренберг added the comment: Oops. def serialize(value: int, signed=True) -> bytes: x = value.to_bytes(-1, 'big', signed=signed) l = len(x).to_bytes(4, 'big', signed=False) return l + x assert len(serialize(0)) == 4 + 0 # see Issue27623 assert len(serialize(120)) == 4 + 1

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

2016-07-27 Thread Марк Коренберг
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 =