summaryrefslogtreecommitdiffstatshomepage
path: root/py/objint.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objint.c')
-rw-r--r--py/objint.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/py/objint.c b/py/objint.c
index 69954168de..2eabff8d4e 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -265,18 +265,27 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_
return MP_OBJ_NULL;
}
+// this is a classmethod
STATIC mp_obj_t int_from_bytes(uint n_args, const mp_obj_t *args) {
+ // TODO: Support long ints
+ // TODO: Support byteorder param (assumes 'little' at the moment)
+ // TODO: Support signed param (assumes signed=False at the moment)
+
+ // get the buffer info
buffer_info_t bufinfo;
- mp_get_buffer_raise(args[0], &bufinfo);
+ mp_get_buffer_raise(args[1], &bufinfo);
- assert(bufinfo.len >= sizeof(machine_int_t));
- // TODO: Support long ints
- // TODO: Support byteorder param
- // TODO: Support signed param
- return mp_obj_new_int_from_uint(*(machine_uint_t*)bufinfo.buf);
+ // convert the bytes to an integer
+ machine_uint_t value = 0;
+ for (const byte* buf = bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
+ value = (value << 8) | *buf;
+ }
+
+ return mp_obj_new_int_from_uint(value);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_obj, 1, 3, int_from_bytes);
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 3, int_from_bytes);
+STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, (const mp_obj_t)&int_from_bytes_fun_obj);
STATIC mp_obj_t int_to_bytes(uint n_args, const mp_obj_t *args) {
machine_int_t val = mp_obj_int_get_checked(args[0]);