Skip to content

Commit

Permalink
py/objint: Make byteorder argument optional in int.from_bytes() method.
Browse files Browse the repository at this point in the history
This was made optional in CPython 3.11.

Signed-off-by: Amirreza Hamzavi <[email protected]>
  • Loading branch information
AmirHmZz authored and dpgeorge committed Sep 2, 2024
1 parent 0b432b3 commit cb7e990
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions py/objint.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
// this is a classmethod
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
// TODO: Support signed param (assumes signed=False at the moment)
(void)n_args;

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

const byte *buf = (const byte *)bufinfo.buf;
int delta = 1;
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
bool big_endian = n_args < 3 || args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
if (!big_endian) {
buf += bufinfo.len - 1;
delta = -1;
}
Expand All @@ -409,15 +409,15 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if (value > (MP_SMALL_INT_MAX >> 8)) {
// Result will overflow a small-int so construct a big-int
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
return mp_obj_int_from_bytes_impl(big_endian, bufinfo.len, bufinfo.buf);
}
#endif
value = (value << 8) | *buf;
}
return mp_obj_new_int_from_uint(value);
}

static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 4, 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

0 comments on commit cb7e990

Please sign in to comment.