py/objint: from_bytes(): Implement "byteorder" param and arbitrary precision.

If result guaranteedly fits in a small int, it is handled in objint.c.
Otherwise, it is delegated to mp_obj_int_from_bytes_impl(), which should
be implemented by individual objint_*.c, similar to
mp_obj_int_to_bytes_impl().
This commit is contained in:
Paul Sokolovsky
2017-01-21 20:14:18 +03:00
parent 1b42f5251f
commit bec7bfb29d
3 changed files with 26 additions and 12 deletions

View File

@@ -374,26 +374,33 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_
// this is a classmethod
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
// TODO: Support long ints
// TODO: Support byteorder param
// TODO: Support signed param (assumes signed=False at the moment)
(void)n_args;
if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
mp_not_implemented("");
}
// get the buffer info
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
// convert the bytes to an integer
mp_uint_t value = 0;
for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
value = (value << 8) | *buf;
}
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
// If result guaranteedly fits in small int, use that
if (!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;
}
return mp_obj_new_int_from_uint(value);
mp_uint_t value = 0;
for (; bufinfo.len--; buf += delta) {
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);