Commit cb7e9909 authored by Amirreza Hamzavi's avatar Amirreza Hamzavi Committed by Damien George

py/objint: Make byteorder argument optional in int.from_bytes() method.

This was made optional in CPython 3.11.
Signed-off-by: default avatarAmirreza Hamzavi <amirrezahamzavi2000@gmail.com>
parent 0b432b33
...@@ -390,7 +390,6 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp ...@@ -390,7 +390,6 @@ 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 // this is a classmethod
static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { 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) // TODO: Support signed param (assumes signed=False at the moment)
(void)n_args;
// get the buffer info // get the buffer info
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
...@@ -398,7 +397,8 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { ...@@ -398,7 +397,8 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
const byte *buf = (const byte *)bufinfo.buf; const byte *buf = (const byte *)bufinfo.buf;
int delta = 1; 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; buf += bufinfo.len - 1;
delta = -1; delta = -1;
} }
...@@ -409,7 +409,7 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { ...@@ -409,7 +409,7 @@ 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 MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if (value > (MP_SMALL_INT_MAX >> 8)) { if (value > (MP_SMALL_INT_MAX >> 8)) {
// Result will overflow a small-int so construct a big-int // 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 #endif
value = (value << 8) | *buf; value = (value << 8) | *buf;
...@@ -417,7 +417,7 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { ...@@ -417,7 +417,7 @@ static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
return mp_obj_new_int_from_uint(value); 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_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) { static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment