Skip to content

Commit

Permalink
Merge pull request #9940 from dhalbert/from-bytes-to-bytes-omit-byteo…
Browse files Browse the repository at this point in the history
…rder
  • Loading branch information
jepler authored Jan 7, 2025
2 parents af2c232 + d0b8646 commit 26283dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions py/objint.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t
enum { ARG_bytes, ARG_byteorder, ARG_signed };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_bytes, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = NULL} },
{ MP_QSTR_byteorder, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = NULL} },
// CIRCUITPY-CHANGE: not required and given a default value.
{ MP_QSTR_byteorder, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_big)} },
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
Expand Down Expand Up @@ -527,15 +528,17 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t
return mp_obj_new_int_from_uint(value);
}

static MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 3, int_from_bytes);
// CIRCUITPY-CHANGE: only two required args.
static MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 2, int_from_bytes);
static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));

// CIRCUITPY-CHANGE: supports signed
static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_length, ARG_byteorder, ARG_signed };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_length, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_byteorder, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
// CIRCUITPY-CHANGE: not required and given a default value.
{ MP_QSTR_byteorder, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_big)} },
{ MP_QSTR_signed, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
Expand Down Expand Up @@ -575,7 +578,8 @@ static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *

return mp_obj_new_bytes_from_vstr(&vstr);
}
static MP_DEFINE_CONST_FUN_OBJ_KW(int_to_bytes_obj, 3, int_to_bytes);
// CIRCUITPY-CHANGE: only two required args.
static MP_DEFINE_CONST_FUN_OBJ_KW(int_to_bytes_obj, 2, int_to_bytes);

static const mp_rom_map_elem_t int_locals_dict_table[] = {
// CIRCUITPY-CHANGE
Expand Down
4 changes: 4 additions & 0 deletions tests/basics/int_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@
(-256).to_bytes(2, "little", signed=False)
except OverflowError:
print("OverflowError")

# byteorder arg can be omitted; default is "big"
print(int.from_bytes(b"\x01\0"))
print((100).to_bytes(10))

0 comments on commit 26283dc

Please sign in to comment.