@@ -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_
265265 return MP_OBJ_NULL ;
266266}
267267
268+ // this is a classmethod
268269STATIC mp_obj_t int_from_bytes (uint n_args , const mp_obj_t * args ) {
270+ // TODO: Support long ints
271+ // TODO: Support byteorder param (assumes 'little' at the moment)
272+ // TODO: Support signed param (assumes signed=False at the moment)
273+
274+ // get the buffer info
269275 buffer_info_t bufinfo ;
270- mp_get_buffer_raise (args [0 ], & bufinfo );
276+ mp_get_buffer_raise (args [1 ], & bufinfo );
271277
272- assert (bufinfo .len >= sizeof (machine_int_t ));
273- // TODO: Support long ints
274- // TODO: Support byteorder param
275- // TODO: Support signed param
276- return mp_obj_new_int_from_uint (* (machine_uint_t * )bufinfo .buf );
278+ // convert the bytes to an integer
279+ machine_uint_t value = 0 ;
280+ for (uint i = 0 ; i < bufinfo .len ; i ++ ) {
281+ value += ((byte * )bufinfo .buf )[i ];
282+ }
283+
284+ return mp_obj_new_int_from_uint (value );
277285}
278286
279- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (int_from_bytes_obj , 1 , 3 , int_from_bytes );
287+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (int_from_bytes_fun_obj , 2 , 3 , int_from_bytes );
288+ STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ (int_from_bytes_obj , (const mp_obj_t )& int_from_bytes_fun_obj );
280289
281290STATIC mp_obj_t int_to_bytes (uint n_args , const mp_obj_t * args ) {
282291 machine_int_t val = mp_obj_int_get_checked (args [0 ]);
0 commit comments