Skip to content

Commit ac9cb93

Browse files
committed
Check parameters of int.from_bytes
1 parent c1b0044 commit ac9cb93

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

locale/circuitpython.pot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,10 @@ msgstr ""
24502450
msgid "byteorder is not a string"
24512451
msgstr ""
24522452

2453+
#: py/objint.c
2454+
msgid "byteorder must be 'little' or 'big'"
2455+
msgstr ""
2456+
24532457
#: py/objarray.c
24542458
msgid "bytes length not a multiple of item size"
24552459
msgstr ""
@@ -2953,6 +2957,10 @@ msgstr ""
29532957
msgid "frequency is read-only for this board"
29542958
msgstr ""
29552959

2960+
#: py/objint.c
2961+
msgid "from_bytes() does not implement signed=True"
2962+
msgstr ""
2963+
29562964
#: py/objdeque.c
29572965
msgid "full"
29582966
msgstr ""

py/objint.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,19 +480,31 @@ MP_DEFINE_CONST_FUN_OBJ_1(int_bit_length_obj, int_bit_length);
480480
#endif
481481

482482
// this is a classmethod
483-
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
483+
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
484484
// TODO: Support signed param (assumes signed=False at the moment)
485-
(void)n_args;
485+
486+
enum { ARG_signed };
487+
static const mp_arg_t allowed_args[] = {
488+
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
489+
};
490+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
491+
mp_arg_parse_all(n_args - 3, pos_args + 3, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
492+
493+
if (args[ARG_signed].u_bool) {
494+
mp_raise_msg(&mp_type_NotImplementedError, MP_ERROR_TEXT("from_bytes() does not implement signed=True"));
495+
}
486496

487497
// get the buffer info
488498
mp_buffer_info_t bufinfo;
489-
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
499+
mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_READ);
490500

491501
const byte *buf = (const byte *)bufinfo.buf;
492502
int delta = 1;
493-
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
503+
if (pos_args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
494504
buf += bufinfo.len - 1;
495505
delta = -1;
506+
} else if (pos_args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_big)) {
507+
mp_raise_ValueError(MP_ERROR_TEXT("byteorder must be 'little' or 'big'"));
496508
}
497509

498510
mp_uint_t value = 0;
@@ -501,15 +513,15 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
501513
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
502514
if (value > (MP_SMALL_INT_MAX >> 8)) {
503515
// Result will overflow a small-int so construct a big-int
504-
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
516+
return mp_obj_int_from_bytes_impl(pos_args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
505517
}
506518
#endif
507519
value = (value << 8) | *buf;
508520
}
509521
return mp_obj_new_int_from_uint(value);
510522
}
511523

512-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
524+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 3, int_from_bytes);
513525
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
514526

515527
STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {

0 commit comments

Comments
 (0)