diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-04 15:08:00 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-04 15:08:00 +0100 |
commit | d7aadcfe1b35a54ea873c3b052abbeeb268a7202 (patch) | |
tree | 6d70583e7f9ea098fa01147f74920c55d0dfaaf4 /py/objdict.c | |
parent | 36f0ee1a5469cf4cb1d76f2613476644625cbc05 (diff) | |
download | micropython-d7aadcfe1b35a54ea873c3b052abbeeb268a7202.tar.gz micropython-d7aadcfe1b35a54ea873c3b052abbeeb268a7202.zip |
py: Allow dict constructor to take keyword arguments.
Diffstat (limited to 'py/objdict.c')
-rw-r--r-- | py/objdict.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/py/objdict.c b/py/objdict.c index afd1d0da0f..ad8c137489 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -32,8 +32,26 @@ STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env } STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { - // TODO create from an iterable! - return mp_obj_new_dict(0); + mp_obj_t dict; + switch (n_args) { + case 0: + dict = mp_obj_new_dict(0); + break; + + case 1: + // TODO create dict from an iterable! + assert(false); + + default: + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument")); + } + + // add to the new dict any keyword args + for (const mp_obj_t *a = args + n_args; n_kw > 0; n_kw--, a += 2) { + mp_obj_dict_store(dict, a[0], a[1]); + } + + return dict; } STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) { |