From 8c5632a869f10445dc5091c39ec73ca49cf83454 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 15 Jun 2017 13:56:21 +1000 Subject: [PATCH] py/objint: Support "big" byte-order in int.to_bytes(). --- py/objint.c | 11 ++++------- tests/basics/int_bytes.py | 6 ++++++ tests/basics/int_bytes_intbig.py | 2 ++ tests/basics/int_bytes_notimpl.py | 4 ---- tests/basics/int_bytes_notimpl.py.exp | 1 - 5 files changed, 12 insertions(+), 12 deletions(-) delete mode 100644 tests/basics/int_bytes_notimpl.py delete mode 100644 tests/basics/int_bytes_notimpl.py.exp diff --git a/py/objint.c b/py/objint.c index bda9c46cf0..4801baa85e 100644 --- a/py/objint.c +++ b/py/objint.c @@ -432,15 +432,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_fro STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj)); STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) { - // TODO: Support byteorder param // TODO: Support signed param (assumes signed=False) (void)n_args; - if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) { - mp_not_implemented(""); - } - mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]); + bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little); vstr_t vstr; vstr_init_len(&vstr, len); @@ -449,12 +445,13 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) { #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE if (!MP_OBJ_IS_SMALL_INT(args[0])) { - mp_obj_int_to_bytes_impl(args[0], false, len, data); + mp_obj_int_to_bytes_impl(args[0], big_endian, len, data); } else #endif { mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]); - mp_binary_set_int(MIN((size_t)len, sizeof(val)), false, data, val); + size_t l = MIN((size_t)len, sizeof(val)); + mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val); } return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); diff --git a/tests/basics/int_bytes.py b/tests/basics/int_bytes.py index 93c00bba10..5198792625 100644 --- a/tests/basics/int_bytes.py +++ b/tests/basics/int_bytes.py @@ -8,3 +8,9 @@ print(int.from_bytes(b"\x00\x01\0\0\0\0\0\0", "little")) # check that extra zero bytes don't change the internal int value print(int.from_bytes(bytes(20), "little") == 0) print(int.from_bytes(b"\x01" + bytes(20), "little") == 1) + +# big-endian conversion +print((10).to_bytes(1, "big")) +print((100).to_bytes(10, "big")) +print(int.from_bytes(b"\0\0\0\0\0\0\0\0\0\x01", "big")) +print(int.from_bytes(b"\x01\0", "big")) diff --git a/tests/basics/int_bytes_intbig.py b/tests/basics/int_bytes_intbig.py index 0e0ad1cbb7..147362bef1 100644 --- a/tests/basics/int_bytes_intbig.py +++ b/tests/basics/int_bytes_intbig.py @@ -1,4 +1,5 @@ print((2**64).to_bytes(9, "little")) +print((2**64).to_bytes(9, "big")) b = bytes(range(20)) @@ -7,6 +8,7 @@ ib = int.from_bytes(b, "big") print(il) print(ib) print(il.to_bytes(20, "little")) +print(ib.to_bytes(20, "big")) # check that extra zero bytes don't change the internal int value print(int.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little")) diff --git a/tests/basics/int_bytes_notimpl.py b/tests/basics/int_bytes_notimpl.py deleted file mode 100644 index b149f44966..0000000000 --- a/tests/basics/int_bytes_notimpl.py +++ /dev/null @@ -1,4 +0,0 @@ -try: - print((10).to_bytes(1, "big")) -except Exception as e: - print(type(e)) diff --git a/tests/basics/int_bytes_notimpl.py.exp b/tests/basics/int_bytes_notimpl.py.exp deleted file mode 100644 index 606649a693..0000000000 --- a/tests/basics/int_bytes_notimpl.py.exp +++ /dev/null @@ -1 +0,0 @@ -