Commit 58bb73e0 authored by Damien George's avatar Damien George
Browse files

py/objint: In int.from_bytes, only create big-int if really needed.

This patch ensures that int.from_bytes only creates a big-int if necessary,
by checking the value for a small-int overflow as it's being parsed.
parent 288ea06e
......@@ -407,26 +407,25 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
// If result guaranteedly fits in small int, use that
if (bufinfo.len >= sizeof(mp_uint_t) || !MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
} else
#endif
{
const byte* buf = (const byte*)bufinfo.buf;
int delta = 1;
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
buf += bufinfo.len - 1;
delta = -1;
}
const byte* buf = (const byte*)bufinfo.buf;
int delta = 1;
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
buf += bufinfo.len - 1;
delta = -1;
}
mp_uint_t value = 0;
for (; bufinfo.len--; buf += delta) {
value = (value << 8) | *buf;
mp_uint_t value = 0;
size_t len = bufinfo.len;
for (; len--; buf += delta) {
#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_new_int_from_uint(value);
#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);
......
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