Skip to content

Commit

Permalink
py/objint.c: Use kw args in int.from_bytes().
Browse files Browse the repository at this point in the history
Add `length`, `byteorder`, `signed`
according to the micropython#16311.

Signed-off-by: Ihor Nehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa committed Feb 26, 2025
1 parent 6d5e840 commit 4977a78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
29 changes: 21 additions & 8 deletions py/objint.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,31 @@ mp_obj_t mp_obj_integer_from_bytes_impl(bool big_endian, bool is_signed, size_t
}

// this is a classmethod
// result = int.from_bytes(bytearray(), order='big', signed=False)
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
// result = int.from_bytes(bytearray(), [[length=,] byteorder='big',] signed=False)
static mp_obj_t int_from_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_INT, { .u_int = 0 } },
{ MP_QSTR_byteorder, MP_ARG_OBJ, { .u_rom_obj = MP_ROM_QSTR(MP_QSTR_big) } },
{ MP_QSTR_signed, MP_ARG_BOOL, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

// get the buffer info
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
bool is_signed = (n_args > 3) && mp_obj_is_true(args[3]);
mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_READ);

return mp_obj_integer_from_bytes_impl(big_endian, is_signed, bufinfo.len, bufinfo.buf);
}
size_t len = args[ARG_length].u_int;
bool big_endian = args[ARG_byteorder].u_obj != MP_OBJ_NEW_QSTR(MP_QSTR_little);
bool is_signed = args[ARG_signed].u_bool;

static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, int_from_bytes);
if ((len <= 0) || (len > bufinfo.len)) {
len = bufinfo.len;
}
return mp_obj_integer_from_bytes_impl(big_endian, is_signed, len, bufinfo.buf);
}
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));

static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
Expand Down
4 changes: 2 additions & 2 deletions tests/basics/int_from_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_int(x, order, signed):
print(f"x:{x}, x:0x{x:X}, size:{size}", end="")
b = x.to_bytes(size, order, signed)
print(f", b:{b}")
y = int.from_bytes(b, order, signed)
y = int.from_bytes(b, byteorder=order, signed=signed)
print(x, y, x == y, b)
print()
assert x == y
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_bytes(b, uint, order, signed):
if order == "little":
b = reverse(b)
print(f"reverse:{b}, ", end="")
i = int.from_bytes(b, order, signed)
i = int.from_bytes(b, byteorder=order, signed=signed)
if signed:
print(f"sint:{sint}, from_bytes:{i}, {i == sint}, ", end="")
else:
Expand Down

0 comments on commit 4977a78

Please sign in to comment.