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>
This commit is contained in:
parent
0b432b3306
commit
cb7e99098e
@ -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
|
||||
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;
|
||||
@ -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;
|
||||
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;
|
||||
}
|
||||
@ -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 (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;
|
||||
@ -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);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user