Skip to content

Commit 491cbd6

Browse files
committed
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 adafruit#577.
1 parent 33b3a69 commit 491cbd6

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

py/argcheck.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,9 @@ void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_all
103103
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra keyword arguments given"));
104104
}
105105
}
106+
107+
void mp_arg_parse_all_kw_array(uint n_pos, uint n_kw, const mp_obj_t *args, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
108+
mp_map_t kw_args;
109+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos);
110+
mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
111+
}

py/objenumerate.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,23 @@ typedef struct _mp_obj_enumerate_t {
4141

4242
STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in);
4343

44-
/* TODO: enumerate is one of the ones that can take args or kwargs.
45-
Sticking to args for now */
44+
STATIC const mp_arg_t enumerate_make_new_args[] = {
45+
{ MP_QSTR_iterable, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
46+
{ MP_QSTR_start, MP_ARG_INT, {.u_int = 0} },
47+
};
48+
#define ENUMERATE_MAKE_NEW_NUM_ARGS ARRAY_SIZE(enumerate_make_new_args)
49+
4650
STATIC mp_obj_t enumerate_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
47-
assert(n_args > 0);
51+
// parse args
52+
mp_arg_val_t vals[ENUMERATE_MAKE_NEW_NUM_ARGS];
53+
mp_arg_parse_all_kw_array(n_args, n_kw, args, ENUMERATE_MAKE_NEW_NUM_ARGS, enumerate_make_new_args, vals);
54+
55+
// create enumerate object
4856
mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
4957
o->base.type = &mp_type_enumerate;
50-
o->iter = mp_getiter(args[0]);
51-
o->cur = n_args > 1 ? mp_obj_get_int(args[1]) : 0;
58+
o->iter = mp_getiter(vals[0].u_obj);
59+
o->cur = vals[1].u_int;
60+
5261
return o;
5362
}
5463

py/qstrdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ Q(startswith)
238238
Q(replace)
239239
Q(partition)
240240
Q(rpartition)
241+
Q(iterable)
242+
Q(start)
241243

242244
Q(bound_method)
243245
Q(closure)

py/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void mp_deinit(void);
5656

5757
void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw);
5858
void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
59+
void mp_arg_parse_all_kw_array(uint n_pos, uint n_kw, const mp_obj_t *args, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
5960

6061
mp_obj_dict_t *mp_locals_get(void);
6162
void mp_locals_set(mp_obj_dict_t *d);

stmhal/extint.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,8 @@ STATIC mp_obj_t extint_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
302302
// type_in == extint_obj_type
303303

304304
// parse args
305-
mp_map_t kw_args;
306-
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
307305
mp_arg_val_t vals[PYB_EXTINT_MAKE_NEW_NUM_ARGS];
308-
mp_arg_parse_all(n_args, args, &kw_args, PYB_EXTINT_MAKE_NEW_NUM_ARGS, pyb_extint_make_new_args, vals);
306+
mp_arg_parse_all_kw_array(n_args, n_kw, args, PYB_EXTINT_MAKE_NEW_NUM_ARGS, pyb_extint_make_new_args, vals);
309307

310308
extint_obj_t *self = m_new_obj(extint_obj_t);
311309
self->base.type = type_in;

tests/basics/enumerate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
print(list(enumerate([1, 2, 3], -5)))
55
print(list(enumerate(range(1000))))
66

7+
# specifying args with keywords
8+
print(list(enumerate([1, 2, 3], start=1)))
9+
print(list(enumerate(iterable=[1, 2, 3])))
10+
print(list(enumerate(iterable=[1, 2, 3], start=1)))

0 commit comments

Comments
 (0)