Skip to content

Commit bec7bfb

Browse files
committed
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().
1 parent 1b42f52 commit bec7bfb

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

py/objint.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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_
374374

375375
// this is a classmethod
376376
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
377-
// TODO: Support long ints
378-
// TODO: Support byteorder param
379377
// TODO: Support signed param (assumes signed=False at the moment)
380378
(void)n_args;
381379

382-
if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
383-
mp_not_implemented("");
384-
}
385-
386380
// get the buffer info
387381
mp_buffer_info_t bufinfo;
388382
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
389383

390-
// convert the bytes to an integer
391-
mp_uint_t value = 0;
392-
for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
393-
value = (value << 8) | *buf;
394-
}
384+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
385+
// If result guaranteedly fits in small int, use that
386+
if (!MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
387+
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
388+
} else
389+
#endif
390+
{
391+
const byte* buf = (const byte*)bufinfo.buf;
392+
int delta = 1;
393+
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
394+
buf += bufinfo.len - 1;
395+
delta = -1;
396+
}
395397

396-
return mp_obj_new_int_from_uint(value);
398+
mp_uint_t value = 0;
399+
for (; bufinfo.len--; buf += delta) {
400+
value = (value << 8) | *buf;
401+
}
402+
return mp_obj_new_int_from_uint(value);
403+
}
397404
}
398405

399406
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);

py/objint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co
5959
char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
6060
int base, const char *prefix, char base_char, char comma);
6161
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
62+
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf);
6263
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf);
6364
int mp_obj_int_sign(mp_obj_t self_in);
6465
mp_obj_t mp_obj_int_abs(mp_obj_t self_in);

py/objint_mpz.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size,
107107
return str;
108108
}
109109

110+
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
111+
mp_obj_int_t *o = mp_obj_int_new_mpz();
112+
mpz_set_from_bytes(&o->mpz, big_endian, len, buf);
113+
return MP_OBJ_FROM_PTR(o);
114+
}
115+
110116
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
111117
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
112118
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);

0 commit comments

Comments
 (0)