py: Add keyword arg support to enumerate constructor.
Need to have a policy as to how far we go adding keyword support to
built ins. It's nice to have, and gets better CPython compatibility,
but hurts the micro nature of uPy.
Addresses issue #577.
diff --git a/py/objenumerate.c b/py/objenumerate.c
index fd428da..611b9b5 100644
--- a/py/objenumerate.c
+++ b/py/objenumerate.c
@@ -41,14 +41,23 @@
STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in);
-/* TODO: enumerate is one of the ones that can take args or kwargs.
- Sticking to args for now */
+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 ARRAY_SIZE(enumerate_make_new_args)
+
STATIC mp_obj_t enumerate_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
- assert(n_args > 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);
+
+ // create enumerate object
mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
o->base.type = &mp_type_enumerate;
- o->iter = mp_getiter(args[0]);
- o->cur = n_args > 1 ? mp_obj_get_int(args[1]) : 0;
+ o->iter = mp_getiter(vals[0].u_obj);
+ o->cur = vals[1].u_int;
+
return o;
}