Skip to content

Commit cb7e990

Browse files
AmirHmZzdpgeorge
authored andcommitted
py/objint: Make byteorder argument optional in int.from_bytes() method.
This was made optional in CPython 3.11. Signed-off-by: Amirreza Hamzavi <amirrezahamzavi2000@gmail.com>
1 parent 0b432b3 commit cb7e990

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

py/objint.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,15 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp
390390
// this is a classmethod
391391
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
392392
// TODO: Support signed param (assumes signed=False at the moment)
393-
(void)n_args;
394393

395394
// get the buffer info
396395
mp_buffer_info_t bufinfo;
397396
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
398397

399398
const byte *buf = (const byte *)bufinfo.buf;
400399
int delta = 1;
401-
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
400+
bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
401+
if (!big_endian) {
402402
buf += bufinfo.len - 1;
403403
delta = -1;
404404
}
@@ -409,15 +409,15 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
409409
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
410410
if (value > (MP_SMALL_INT_MAX >> 8)) {
411411
// Result will overflow a small-int so construct a big-int
412-
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
412+
return mp_obj_int_from_bytes_impl(big_endian, bufinfo.len, bufinfo.buf);
413413
}
414414
#endif
415415
value = (value << 8) | *buf;
416416
}
417417
return mp_obj_new_int_from_uint(value);
418418
}
419419

420-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
420+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, int_from_bytes);
421421
static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
422422

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

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