From ac9cb9389c8d2290f9404c9d1008a7491d09f721 Mon Sep 17 00:00:00 2001 From: Isaac Benitez Date: Tue, 5 Jul 2022 22:31:33 -0700 Subject: [PATCH] Check parameters of int.from_bytes --- locale/circuitpython.pot | 8 ++++++++ py/objint.c | 24 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 5740e1e288..1cf3725cd2 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -2450,6 +2450,10 @@ msgstr "" msgid "byteorder is not a string" msgstr "" +#: py/objint.c +msgid "byteorder must be 'little' or 'big'" +msgstr "" + #: py/objarray.c msgid "bytes length not a multiple of item size" msgstr "" @@ -2953,6 +2957,10 @@ msgstr "" msgid "frequency is read-only for this board" msgstr "" +#: py/objint.c +msgid "from_bytes() does not implement signed=True" +msgstr "" + #: py/objdeque.c msgid "full" msgstr "" diff --git a/py/objint.c b/py/objint.c index 98178e7ffa..fa8bc7985c 100644 --- a/py/objint.c +++ b/py/objint.c @@ -480,19 +480,31 @@ MP_DEFINE_CONST_FUN_OBJ_1(int_bit_length_obj, int_bit_length); #endif // this is a classmethod -STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { // TODO: Support signed param (assumes signed=False at the moment) - (void)n_args; + + enum { ARG_signed }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 3, pos_args + 3, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_signed].u_bool) { + mp_raise_msg(&mp_type_NotImplementedError, MP_ERROR_TEXT("from_bytes() does not implement signed=True")); + } // get the buffer info mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); + mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_READ); const byte *buf = (const byte *)bufinfo.buf; int delta = 1; - if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) { + if (pos_args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) { buf += bufinfo.len - 1; delta = -1; + } else if (pos_args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_big)) { + mp_raise_ValueError(MP_ERROR_TEXT("byteorder must be 'little' or 'big'")); } mp_uint_t value = 0; @@ -501,7 +513,7 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE if (value > (MP_SMALL_INT_MAX >> 8)) { // Result will overflow a small-int so construct a big-int - return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf); + return mp_obj_int_from_bytes_impl(pos_args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf); } #endif value = (value << 8) | *buf; @@ -509,7 +521,7 @@ 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, 3, 4, int_from_bytes); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 3, 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 *pos_args, mp_map_t *kw_args) {