@@ -480,19 +480,33 @@ MP_DEFINE_CONST_FUN_OBJ_1(int_bit_length_obj, int_bit_length);
480
480
#endif
481
481
482
482
// this is a classmethod
483
- STATIC mp_obj_t int_from_bytes (size_t n_args , const mp_obj_t * args ) {
483
+ STATIC mp_obj_t int_from_bytes (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
484
484
// TODO: Support signed param (assumes signed=False at the moment)
485
- (void )n_args ;
485
+
486
+ enum { ARG_bytes , ARG_byteorder , ARG_signed };
487
+ static const mp_arg_t allowed_args [] = {
488
+ { MP_QSTR_bytes , MP_ARG_REQUIRED | MP_ARG_OBJ },
489
+ { MP_QSTR_byteorder , MP_ARG_REQUIRED | MP_ARG_OBJ },
490
+ { MP_QSTR_signed , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
491
+ };
492
+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
493
+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
494
+
495
+ if (args [ARG_signed ].u_bool ) {
496
+ mp_raise_NotImplementedError_varg (MP_ERROR_TEXT ("%q=%q" ), MP_QSTR_signed , MP_QSTR_True );
497
+ }
486
498
487
499
// get the buffer info
488
500
mp_buffer_info_t bufinfo ;
489
- mp_get_buffer_raise (args [1 ] , & bufinfo , MP_BUFFER_READ );
501
+ mp_get_buffer_raise (args [ARG_bytes ]. u_obj , & bufinfo , MP_BUFFER_READ );
490
502
491
503
const byte * buf = (const byte * )bufinfo .buf ;
492
504
int delta = 1 ;
493
- if (args [2 ] == MP_OBJ_NEW_QSTR (MP_QSTR_little )) {
505
+ if (args [ARG_byteorder ]. u_obj == MP_OBJ_NEW_QSTR (MP_QSTR_little )) {
494
506
buf += bufinfo .len - 1 ;
495
507
delta = -1 ;
508
+ } else if (args [ARG_byteorder ].u_obj != MP_OBJ_NEW_QSTR (MP_QSTR_big )) {
509
+ mp_arg_error_invalid (MP_QSTR_byteorder );
496
510
}
497
511
498
512
mp_uint_t value = 0 ;
@@ -501,15 +515,15 @@ STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
501
515
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
502
516
if (value > (MP_SMALL_INT_MAX >> 8 )) {
503
517
// Result will overflow a small-int so construct a big-int
504
- return mp_obj_int_from_bytes_impl (args [2 ] != MP_OBJ_NEW_QSTR (MP_QSTR_little ), bufinfo .len , bufinfo .buf );
518
+ return mp_obj_int_from_bytes_impl (args [ARG_byteorder ]. u_obj != MP_OBJ_NEW_QSTR (MP_QSTR_little ), bufinfo .len , bufinfo .buf );
505
519
}
506
520
#endif
507
521
value = (value << 8 ) | * buf ;
508
522
}
509
523
return mp_obj_new_int_from_uint (value );
510
524
}
511
525
512
- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (int_from_bytes_fun_obj , 3 , 4 , int_from_bytes );
526
+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (int_from_bytes_fun_obj , 3 , int_from_bytes );
513
527
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ (int_from_bytes_obj , MP_ROM_PTR (& int_from_bytes_fun_obj ));
514
528
515
529
STATIC mp_obj_t int_to_bytes (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
0 commit comments