From 9d787de2a17f99cfeb715507857b92d54b023715 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 9 Dec 2016 21:15:16 +0300 Subject: [PATCH] py/objint: from_bytes, to_bytes: Require byteorder arg, require "little". CPython requires byteorder arg, make uPy compatible. As we support only "little", error out on anything else. --- py/objint.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/py/objint.c b/py/objint.c index f8988d6c94..3af509b535 100644 --- a/py/objint.c +++ b/py/objint.c @@ -383,10 +383,14 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_ // this is a classmethod STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { // TODO: Support long ints - // TODO: Support byteorder param (assumes 'little' at the moment) + // TODO: Support byteorder param // TODO: Support signed param (assumes signed=False at the moment) (void)n_args; + if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) { + mp_not_implemented(""); + } + // get the buffer info mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); @@ -400,14 +404,18 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int_from_uint(value); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 3, int_from_bytes); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes); 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 (assumes 'little') + // 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]); vstr_t vstr; @@ -427,7 +435,7 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) { return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 2, 4, int_to_bytes); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes); STATIC const mp_rom_map_elem_t int_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },