Check parameters of int.from_bytes
This commit is contained in:
parent
c1b00442a1
commit
ac9cb9389c
@ -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 ""
|
||||
|
24
py/objint.c
24
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user