summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-11 07:01:15 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-11 07:01:15 +0300
commitbe019ce0636398d1cbdee007d5238160dce8eda7 (patch)
treefd1bc73036d5097310313397df84a42cd7408eb0 /py
parent12a04392b9f3d6a7321f505309f4c7bcf1a6ee2f (diff)
downloadmicropython-be019ce0636398d1cbdee007d5238160dce8eda7.tar.gz
micropython-be019ce0636398d1cbdee007d5238160dce8eda7.zip
objdict: Implement construction from iterable of pairs.
Pairs are limited to tuples so far.
Diffstat (limited to 'py')
-rw-r--r--py/objdict.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/py/objdict.c b/py/objdict.c
index 170724de18..4dffa53da9 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -7,6 +7,7 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
+#include "objtuple.h"
#include "runtime0.h"
#include "runtime.h"
@@ -39,13 +40,22 @@ STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
dict = mp_obj_new_dict(0);
break;
- case 1:
+ case 1: {
if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) {
return dict_copy(args[0]);
}
// TODO create dict from an arbitrary mapping!
- // TODO create dict from an iterable!
- assert(false);
+
+ // Make dict from iterable of pairs
+ mp_obj_t iterable = mp_getiter(args[0]);
+ mp_obj_t dict = mp_obj_new_dict(0);
+ // TODO: support arbitrary seq as a pair
+ mp_obj_tuple_t *item;
+ while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
+ mp_obj_dict_store(dict, item->items[0], item->items[1]);
+ }
+ return dict;
+ }
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument"));