diff options
author | John R. Lenton <jlenton@gmail.com> | 2014-01-06 18:11:20 +0000 |
---|---|---|
committer | John R. Lenton <jlenton@gmail.com> | 2014-01-07 22:51:08 +0000 |
commit | d90b19eca56a361d432ab6af9c2305bb3f708e1c (patch) | |
tree | 88880f24309ceb7ddccae716847e81d7a0042822 | |
parent | 7d21d516d2eb77242c8ade4ae83f0d438e86f072 (diff) | |
download | micropython-d90b19eca56a361d432ab6af9c2305bb3f708e1c.tar.gz micropython-d90b19eca56a361d432ab6af9c2305bb3f708e1c.zip |
Added dict.copy
-rw-r--r-- | py/objdict.c | 10 | ||||
-rw-r--r-- | tests/basics/tests/dict_copy.py | 5 |
2 files changed, 15 insertions, 0 deletions
diff --git a/py/objdict.c b/py/objdict.c index 951e1ec068..7567a612cd 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -130,6 +130,15 @@ static mp_obj_t dict_clear(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear); +static mp_obj_t dict_copy(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &dict_type)); + mp_obj_dict_t *self = self_in; + mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc); + other->map.used = self->map.used; + memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t)); + return other; +} +static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); /******************************************************************************/ /* dict constructors & etc */ @@ -143,6 +152,7 @@ const mp_obj_type_t dict_type = { .getiter = dict_getiter, .methods = { { "clear", &dict_clear_obj }, + { "copy", &dict_copy_obj }, { NULL, NULL }, // end-of-list sentinel }, }; diff --git a/tests/basics/tests/dict_copy.py b/tests/basics/tests/dict_copy.py new file mode 100644 index 0000000000..c3eb7ffc18 --- /dev/null +++ b/tests/basics/tests/dict_copy.py @@ -0,0 +1,5 @@ +a = {i: 2*i for i in range(1000)} +b = a.copy() +for i in range(1000): + print(i, b[i]) +print(len(b)) |