py: Support conversion of bignum to bytes.

This gets int.to_bytes working for bignum, and also struct.pack with 'q'
and 'Q' args on 32-bit machines.

Addresses issue #1155.
This commit is contained in:
Damien George
2015-04-25 23:16:39 +01:00
parent 7c8b4c1a8b
commit 271d18eb08
9 changed files with 89 additions and 15 deletions

View File

@@ -32,6 +32,7 @@
#include "py/binary.h"
#include "py/smallint.h"
#include "py/objint.h"
// Helpers to work with binary-encoded data
@@ -282,10 +283,13 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
}
#endif
default:
// we handle large ints here by calling the truncated accessor
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
val = mp_obj_int_get_truncated(val_in);
} else {
mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
return;
} else
#endif
{
val = mp_obj_get_int(val_in);
}
}