From 22d85ec5be77e7ee6b703221d51113f7f21a995b Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Jan 2016 15:47:56 +0000 Subject: [PATCH] py: Use new code pattern for parsing kw args with mp_arg_parse_all. Makes code easier to read and more maintainable. --- py/objenumerate.c | 22 ++++++++++++---------- py/objlist.c | 11 +++++++---- py/objstr.c | 9 ++++++--- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/py/objenumerate.c b/py/objenumerate.c index 26a5740a1..2b646ca45 100644 --- a/py/objenumerate.c +++ b/py/objenumerate.c @@ -39,23 +39,25 @@ typedef struct _mp_obj_enumerate_t { STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in); -STATIC const mp_arg_t enumerate_make_new_args[] = { - { MP_QSTR_iterable, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_start, MP_ARG_INT, {.u_int = 0} }, -}; -#define ENUMERATE_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(enumerate_make_new_args) - STATIC mp_obj_t enumerate_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { #if MICROPY_CPYTHON_COMPAT + static const mp_arg_t allowed_args[] = { + { MP_QSTR_iterable, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_start, MP_ARG_INT, {.u_int = 0} }, + }; + // parse args - mp_arg_val_t vals[ENUMERATE_MAKE_NEW_NUM_ARGS]; - mp_arg_parse_all_kw_array(n_args, n_kw, args, ENUMERATE_MAKE_NEW_NUM_ARGS, enumerate_make_new_args, vals); + struct { + mp_arg_val_t iterable, start; + } arg_vals; + mp_arg_parse_all_kw_array(n_args, n_kw, args, + MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&arg_vals); // create enumerate object mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t); o->base.type = type; - o->iter = mp_getiter(vals[0].u_obj); - o->cur = vals[1].u_int; + o->iter = mp_getiter(arg_vals.iterable.u_obj); + o->cur = arg_vals.start.u_int; #else (void)n_kw; mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t); diff --git a/py/objlist.c b/py/objlist.c index 62928af06..df6b1bc99 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -318,16 +318,19 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ }; // parse args - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + struct { + mp_arg_val_t key, reverse; + } args; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, + MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args); assert(MP_OBJ_IS_TYPE(pos_args[0], &mp_type_list)); mp_obj_list_t *self = MP_OBJ_TO_PTR(pos_args[0]); if (self->len > 1) { mp_quicksort(self->items, self->items + self->len - 1, - args[0].u_obj == mp_const_none ? MP_OBJ_NULL : args[0].u_obj, - args[1].u_bool ? mp_const_false : mp_const_true); + args.key.u_obj == mp_const_none ? MP_OBJ_NULL : args.key.u_obj, + args.reverse.u_bool ? mp_const_false : mp_const_true); } return mp_const_none; diff --git a/py/objstr.c b/py/objstr.c index 8a0a19de1..73de7b5da 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -557,11 +557,14 @@ STATIC mp_obj_t str_splitlines(size_t n_args, const mp_obj_t *pos_args, mp_map_t }; // parse args - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + struct { + mp_arg_val_t keepends; + } args; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, + MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args); mp_obj_t new_args[2] = {pos_args[0], MP_OBJ_NEW_QSTR(MP_QSTR__backslash_n)}; - return str_split_internal(2, new_args, SPLITLINES | (args[0].u_bool ? KEEP : 0)); + return str_split_internal(2, new_args, SPLITLINES | (args.keepends.u_bool ? KEEP : 0)); } #endif