Skip to content

Commit 44e5e3c

Browse files
committed
py/objint.c: Use kw args in int.from_bytes().
Add `length`, `byteorder`, `signed` according to the #16311. Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent 735435a commit 44e5e3c

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

py/objint.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,18 +491,31 @@ mp_obj_t mp_obj_integer_from_bytes_impl(bool big_endian, bool is_signed, size_t
491491
}
492492

493493
// this is a classmethod
494-
// result = int.from_bytes(bytearray(), order='big', signed=False)
495-
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
494+
// result = int.from_bytes(bytearray(), [[length=,] byteorder='big',] signed=False)
495+
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
496+
enum { ARG_length, ARG_byteorder, ARG_signed };
497+
static const mp_arg_t allowed_args[] = {
498+
{ MP_QSTR_length, MP_ARG_INT, { .u_int = 0 } },
499+
{ MP_QSTR_byteorder, MP_ARG_OBJ, { .u_rom_obj = MP_ROM_QSTR(MP_QSTR_big) } },
500+
{ MP_QSTR_signed, MP_ARG_BOOL, {.u_bool = false} },
501+
};
502+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
503+
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
504+
496505
// get the buffer info
497506
mp_buffer_info_t bufinfo;
498-
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
499-
bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
500-
bool is_signed = (n_args > 3) && mp_obj_is_true(args[3]);
507+
mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_READ);
501508

502-
return mp_obj_integer_from_bytes_impl(big_endian, is_signed, bufinfo.len, bufinfo.buf);
503-
}
509+
size_t len = args[ARG_length].u_int;
510+
bool big_endian = args[ARG_byteorder].u_obj != MP_OBJ_NEW_QSTR(MP_QSTR_little);
511+
bool is_signed = args[ARG_signed].u_bool;
504512

505-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, int_from_bytes);
513+
if ((len <= 0) || (len > bufinfo.len)) {
514+
len = bufinfo.len;
515+
}
516+
return mp_obj_integer_from_bytes_impl(big_endian, is_signed, len, bufinfo.buf);
517+
}
518+
static MP_DEFINE_CONST_FUN_OBJ_KW(int_from_bytes_fun_obj, 2, int_from_bytes);
506519
static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
507520

508521
static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {

tests/basics/int_from_bytes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_int(x, order, signed):
2020
print(f"x:{x}, x:0x{x:X}, size:{size}", end="")
2121
b = x.to_bytes(size, order, signed)
2222
print(f", b:{b}")
23-
y = int.from_bytes(b, order, signed)
23+
y = int.from_bytes(b, byteorder=order, signed=signed)
2424
print(x, y, x == y, b)
2525
print()
2626
assert x == y
@@ -144,7 +144,7 @@ def test_bytes(b, uint, order, signed):
144144
if order == "little":
145145
b = reverse(b)
146146
print(f"reverse:{b}, ", end="")
147-
i = int.from_bytes(b, order, signed)
147+
i = int.from_bytes(b, byteorder=order, signed=signed)
148148
if signed:
149149
print(f"sint:{sint}, from_bytes:{i}, {i == sint}, ", end="")
150150
else:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy