Skip to content

Commit 1d90a0e

Browse files
committed
py/objint.c: int.from_bytes and .to_bytes can omit byteorder arg
1 parent 57cf054 commit 1d90a0e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

py/objint.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t
490490
enum { ARG_bytes, ARG_byteorder, ARG_signed };
491491
static const mp_arg_t allowed_args[] = {
492492
{ MP_QSTR_bytes, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = NULL} },
493-
{ MP_QSTR_byteorder, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = NULL} },
493+
{ MP_QSTR_byteorder, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_big)} },
494494
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
495495
};
496496
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -527,15 +527,15 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t
527527
return mp_obj_new_int_from_uint(value);
528528
}
529529

530-
static MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 3, int_from_bytes);
530+
static MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 2, int_from_bytes);
531531
static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
532532

533533
// CIRCUITPY-CHANGE: supports signed
534534
static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
535535
enum { ARG_length, ARG_byteorder, ARG_signed };
536536
static const mp_arg_t allowed_args[] = {
537537
{ MP_QSTR_length, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
538-
{ MP_QSTR_byteorder, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
538+
{ MP_QSTR_byteorder, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_big)} },
539539
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
540540
};
541541
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -575,7 +575,7 @@ static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
575575

576576
return mp_obj_new_bytes_from_vstr(&vstr);
577577
}
578-
static MP_DEFINE_CONST_FUN_OBJ_KW(int_to_bytes_obj, 3, int_to_bytes);
578+
static MP_DEFINE_CONST_FUN_OBJ_KW(int_to_bytes_obj, 2, int_to_bytes);
579579

580580
static const mp_rom_map_elem_t int_locals_dict_table[] = {
581581
// CIRCUITPY-CHANGE

tests/basics/int_bytes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@
4343
(-256).to_bytes(2, "little", signed=False)
4444
except OverflowError:
4545
print("OverflowError")
46+
47+
# byteorder arg can be omitted; default is "big"
48+
print(int.from_bytes(b"\x01\0"))
49+
print((100).to_bytes(10))

0 commit comments

Comments
 (0)