Commit 9472907a authored by Damien George's avatar Damien George
Browse files

py: Fix handling of negative numbers in struct.pack of q/Q.

parent ae2c81ff
......@@ -291,6 +291,10 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
#endif
{
val = mp_obj_get_int(val_in);
// sign extend if needed
if (BYTES_PER_WORD < 8 && size > sizeof(val) && is_signed(val_type) && (mp_int_t)val < 0) {
memset(p + sizeof(val), 0xff, size - sizeof(val));
}
}
}
......
......@@ -1441,8 +1441,8 @@ void mpz_as_bytes(const mpz_t *z, bool big_endian, mp_uint_t len, byte *buf) {
for (; bits >= 8; bits -= 8, d >>= 8) {
mpz_dig_t val = d;
if (z->neg) {
d = (~d & 0xff) + carry;
carry = d >> 8;
val = (~val & 0xff) + carry;
carry = val >> 8;
}
if (big_endian) {
*--b = val;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment