py/mpz: Do sign extension in mpz_as_bytes for negative values.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2020-11-05 22:35:02 +11:00
parent 7789cd5f16
commit 1fef5662ab
3 changed files with 17 additions and 2 deletions

View File

@@ -36,3 +36,11 @@ print(struct.unpack("<Q", b"\xff\xff\xff\xff\xff\xff\xff\xff"))
# check small int overflow
print(struct.unpack("<i", b'\xff\xff\xff\x7f'))
print(struct.unpack("<q", b'\xff\xff\xff\xff\xff\xff\xff\x7f'))
# test with negative big integers that are actually small in magnitude
bigzero = (1 << 70) - (1 << 70)
for endian in "<>":
for type_ in "bhiq":
fmt = endian + type_
b = struct.pack(fmt, -2 + bigzero)
print(fmt, b, struct.unpack(fmt, b))