blob: e97f30e7a229631774fabfedc1ad891b022c2669 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damiend99b0522013-12-21 18:17:45 +000027#include <string.h>
28#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/obj.h"
32#include "py/runtime0.h"
33#include "py/runtime.h"
34#include "py/builtin.h"
Damiend99b0522013-12-21 18:17:45 +000035
Damien George93965e72014-08-30 13:23:35 +010036STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
John R. Lentona41fe312014-01-06 17:17:02 +000037
Damien George8a9b9992014-09-17 15:53:03 +010038// This is a helper function to iterate through a dictionary. The state of
39// the iteration is held in *cur and should be initialised with zero for the
40// first call. Will return NULL when no more elements are available.
41STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, mp_uint_t *cur) {
42 mp_uint_t max = dict->map.alloc;
43 mp_map_t *map = &dict->map;
44
45 for (mp_uint_t i = *cur; i < max; i++) {
46 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
47 *cur = i + 1;
48 return &(map->table[i]);
49 }
50 }
51
52 return NULL;
53}
54
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020055STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000056 mp_obj_dict_t *self = self_in;
57 bool first = true;
Damien George612045f2014-09-17 22:56:34 +010058 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
59 kind = PRINT_REPR;
60 }
Damiend99b0522013-12-21 18:17:45 +000061 print(env, "{");
Damien George8a9b9992014-09-17 15:53:03 +010062 mp_uint_t cur = 0;
John R. Lentona41fe312014-01-06 17:17:02 +000063 mp_map_elem_t *next = NULL;
Damien George8a9b9992014-09-17 15:53:03 +010064 while ((next = dict_iter_next(self, &cur)) != NULL) {
John R. Lentona41fe312014-01-06 17:17:02 +000065 if (!first) {
66 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000067 }
John R. Lentona41fe312014-01-06 17:17:02 +000068 first = false;
Damien George612045f2014-09-17 22:56:34 +010069 mp_obj_print_helper(print, env, next->key, kind);
John R. Lentona41fe312014-01-06 17:17:02 +000070 print(env, ": ");
Damien George612045f2014-09-17 22:56:34 +010071 mp_obj_print_helper(print, env, next->value, kind);
Damiend99b0522013-12-21 18:17:45 +000072 }
73 print(env, "}");
74}
75
Damien Georgeecc88e92014-08-30 00:35:11 +010076STATIC mp_obj_t dict_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000077 (void)type_in;
Damien Georgebcb6ca42014-06-03 12:53:44 +010078 mp_obj_t dict = mp_obj_new_dict(0);
79 if (n_args > 0 || n_kw > 0) {
80 mp_obj_t args2[2] = {dict, args[0]}; // args[0] is always valid, even if it's not a positional arg
81 mp_map_t kwargs;
82 mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
83 dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
Damien Georged7aadcf2014-04-04 15:08:00 +010084 }
Damien Georged7aadcf2014-04-04 15:08:00 +010085 return dict;
Damien George71c51812014-01-04 20:21:15 +000086}
87
Damien Georgeecc88e92014-08-30 00:35:11 +010088STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000089 mp_obj_dict_t *self = self_in;
90 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010091 case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +010092 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
Damien George6ac5dce2014-05-21 19:42:43 +010093 default: return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +000094 }
95}
96
Damien Georgeecc88e92014-08-30 00:35:11 +010097STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000098 mp_obj_dict_t *o = lhs_in;
99 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +0100100 case MP_BINARY_OP_IN: {
John R. Lentonc1bef212014-01-11 12:39:33 +0000101 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +0000102 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000103 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300104 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300105 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
106 mp_obj_dict_t *rhs = rhs_in;
107 if (o->map.used != rhs->map.used) {
108 return mp_const_false;
109 }
110
Damien George8a9b9992014-09-17 15:53:03 +0100111 mp_uint_t cur = 0;
112 mp_map_elem_t *next = NULL;
113 while ((next = dict_iter_next(o, &cur)) != NULL) {
114 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
115 if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
116 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300117 }
118 }
119 return mp_const_true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000120 } else {
121 // dict is not equal to instance of any other type
122 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300123 }
124 }
Damiend99b0522013-12-21 18:17:45 +0000125 default:
126 // op not supported
Damien George6ac5dce2014-05-21 19:42:43 +0100127 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000128 }
129}
130
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300131// TODO: Make sure this is inlined in dict_subscr() below.
132mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
133 mp_obj_dict_t *self = self_in;
134 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
135 if (elem == NULL) {
136 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
137 } else {
138 return elem->value;
139 }
140}
141
Damien George729f7b42014-04-17 22:10:53 +0100142STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100143 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100144 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100145 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100146 return mp_const_none;
147 } else if (value == MP_OBJ_SENTINEL) {
148 // load
149 mp_obj_dict_t *self = self_in;
150 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
151 if (elem == NULL) {
152 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
153 } else {
154 return elem->value;
155 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100156 } else {
Damien George729f7b42014-04-17 22:10:53 +0100157 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100158 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100159 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100160 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100161}
John R. Lentona41fe312014-01-06 17:17:02 +0000162
163/******************************************************************************/
164/* dict iterator */
165
166typedef struct _mp_obj_dict_it_t {
167 mp_obj_base_t base;
168 mp_obj_dict_t *dict;
Damien George40f3c022014-07-03 13:25:24 +0100169 mp_uint_t cur;
John R. Lentona41fe312014-01-06 17:17:02 +0000170} mp_obj_dict_it_t;
171
Damien George8a9b9992014-09-17 15:53:03 +0100172STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000173 mp_obj_dict_it_t *self = self_in;
Damien George8a9b9992014-09-17 15:53:03 +0100174 mp_map_elem_t *next = dict_iter_next(self->dict, &self->cur);
John R. Lentona41fe312014-01-06 17:17:02 +0000175
Damien George8a9b9992014-09-17 15:53:03 +0100176 if (next == NULL) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100177 return MP_OBJ_STOP_ITERATION;
Damien George8a9b9992014-09-17 15:53:03 +0100178 } else {
179 return next->key;
John R. Lentona41fe312014-01-06 17:17:02 +0000180 }
181}
182
Damien George3e1a5c12014-03-29 13:43:38 +0000183STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000184 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000185 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300186 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000187 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000188};
189
Damien George8a9b9992014-09-17 15:53:03 +0100190STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000191 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000192 o->base.type = &mp_type_dict_it;
Damien George8a9b9992014-09-17 15:53:03 +0100193 o->dict = o_in;
194 o->cur = 0;
John R. Lentona41fe312014-01-06 17:17:02 +0000195 return o;
196}
197
John R. Lentona41fe312014-01-06 17:17:02 +0000198/******************************************************************************/
199/* dict methods */
200
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200201STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000202 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000203 mp_obj_dict_t *self = self_in;
204
205 mp_map_clear(&self->map);
206
207 return mp_const_none;
208}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200209STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000210
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200211STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000212 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000213 mp_obj_dict_t *self = self_in;
214 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
215 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300216 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
217 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000218 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
219 return other;
220}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200221STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000222
Damien Georgeeae16442014-01-11 19:22:29 +0000223// this is a classmethod
Damien George93965e72014-08-30 13:23:35 +0100224STATIC mp_obj_t dict_fromkeys(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000225 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100226 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000227 mp_obj_t len = mp_obj_len_maybe(iter);
228 mp_obj_t value = mp_const_none;
229 mp_obj_t next = NULL;
230 mp_obj_dict_t *self = NULL;
231
232 if (n_args > 2) {
233 value = args[2];
234 }
235
236 if (len == MP_OBJ_NULL) {
237 /* object's type doesn't have a __len__ slot */
238 self = mp_obj_new_dict(0);
239 } else {
240 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
241 }
242
Damien Georgeea8d06c2014-04-17 23:19:36 +0100243 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000244 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
245 }
246
247 return self;
248}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200249STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
250STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000251
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200252STATIC mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, mp_map_lookup_kind_t lookup_kind) {
Damien George38a2da62014-01-08 17:33:12 +0000253 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000254 mp_obj_t value;
Damien George8a9b9992014-09-17 15:53:03 +0100255 if (elem == NULL || elem->value == MP_OBJ_NULL) {
256 if (deflt == MP_OBJ_NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000257 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100258 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000259 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000260 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000261 }
262 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000263 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000264 }
Damien George8a9b9992014-09-17 15:53:03 +0100265 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
266 elem->value = value;
267 }
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000268 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000269 value = elem->value;
Damien George8a9b9992014-09-17 15:53:03 +0100270 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
271 elem->value = MP_OBJ_NULL; // so that GC can collect the deleted value
272 }
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000273 }
274 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000275}
276
Damien George93965e72014-08-30 13:23:35 +0100277STATIC mp_obj_t dict_get(mp_uint_t n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000278 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000279 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000280
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000281 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
282 args[1],
Damien George8a9b9992014-09-17 15:53:03 +0100283 n_args == 3 ? args[2] : MP_OBJ_NULL,
Damien George38a2da62014-01-08 17:33:12 +0000284 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000285}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200286STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000287
Damien George93965e72014-08-30 13:23:35 +0100288STATIC mp_obj_t dict_pop(mp_uint_t n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000289 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000290 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000291
292 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
293 args[1],
Damien George8a9b9992014-09-17 15:53:03 +0100294 n_args == 3 ? args[2] : MP_OBJ_NULL,
Damien George38a2da62014-01-08 17:33:12 +0000295 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000296}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200297STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000298
299
Damien George93965e72014-08-30 13:23:35 +0100300STATIC mp_obj_t dict_setdefault(mp_uint_t n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000301 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000302 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000303
304 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
305 args[1],
Damien George8a9b9992014-09-17 15:53:03 +0100306 n_args == 3 ? args[2] : MP_OBJ_NULL,
Damien George38a2da62014-01-08 17:33:12 +0000307 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000308}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200309STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000310
John R. Lentonf77dce82014-01-06 20:08:52 +0000311
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200312STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000313 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000314 mp_obj_dict_t *self = self_in;
Damien George8a9b9992014-09-17 15:53:03 +0100315 mp_uint_t cur = 0;
316 mp_map_elem_t *next = dict_iter_next(self, &cur);
317 if (next == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100318 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000319 }
John R. Lentonf77dce82014-01-06 20:08:52 +0000320 self->map.used--;
321 mp_obj_t items[] = {next->key, next->value};
Damien George8a9b9992014-09-17 15:53:03 +0100322 next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
323 next->value = MP_OBJ_NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000324 mp_obj_t tuple = mp_obj_new_tuple(2, items);
325
326 return tuple;
327}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200328STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000329
Damien George93965e72014-08-30 13:23:35 +0100330STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100331 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
332 mp_obj_dict_t *self = args[0];
333
334 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
335
336 if (n_args == 2) {
337 // given a positional argument
338
339 if (MP_OBJ_IS_TYPE(args[1], &mp_type_dict)) {
340 // update from other dictionary (make sure other is not self)
341 if (args[1] != self) {
Damien George8a9b9992014-09-17 15:53:03 +0100342 mp_uint_t cur = 0;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100343 mp_map_elem_t *elem = NULL;
Damien George8a9b9992014-09-17 15:53:03 +0100344 while ((elem = dict_iter_next((mp_obj_dict_t*)args[1], &cur)) != NULL) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100345 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
346 }
347 }
John R. Lenton88f30432014-01-06 22:58:17 +0000348 } else {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100349 // update from a generic iterable of pairs
350 mp_obj_t iter = mp_getiter(args[1]);
351 mp_obj_t next = NULL;
352 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
353 mp_obj_t inneriter = mp_getiter(next);
354 mp_obj_t key = mp_iternext(inneriter);
355 mp_obj_t value = mp_iternext(inneriter);
356 mp_obj_t stop = mp_iternext(inneriter);
357 if (key == MP_OBJ_STOP_ITERATION
358 || value == MP_OBJ_STOP_ITERATION
359 || stop != MP_OBJ_STOP_ITERATION) {
360 nlr_raise(mp_obj_new_exception_msg(
361 &mp_type_ValueError,
362 "dictionary update sequence has the wrong length"));
363 } else {
364 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
365 }
366 }
367 }
368 }
369
370 // update the dict with any keyword args
Damien George40f3c022014-07-03 13:25:24 +0100371 for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100372 if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
373 mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
John R. Lenton88f30432014-01-06 22:58:17 +0000374 }
375 }
376
377 return mp_const_none;
378}
Damien Georgebcb6ca42014-06-03 12:53:44 +0100379STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000380
John R. Lentonf77dce82014-01-06 20:08:52 +0000381
John R. Lentona41fe312014-01-06 17:17:02 +0000382/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000383/* dict views */
384
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200385STATIC const mp_obj_type_t dict_view_type;
386STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000387
388typedef enum _mp_dict_view_kind_t {
389 MP_DICT_VIEW_ITEMS,
390 MP_DICT_VIEW_KEYS,
391 MP_DICT_VIEW_VALUES,
392} mp_dict_view_kind_t;
393
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200394STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000395
396typedef struct _mp_obj_dict_view_it_t {
397 mp_obj_base_t base;
398 mp_dict_view_kind_t kind;
Damien George8a9b9992014-09-17 15:53:03 +0100399 mp_obj_dict_t *dict;
Damien George40f3c022014-07-03 13:25:24 +0100400 mp_uint_t cur;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000401} mp_obj_dict_view_it_t;
402
403typedef struct _mp_obj_dict_view_t {
404 mp_obj_base_t base;
405 mp_obj_dict_t *dict;
406 mp_dict_view_kind_t kind;
407} mp_obj_dict_view_t;
408
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200409STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000410 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
411 mp_obj_dict_view_it_t *self = self_in;
Damien George8a9b9992014-09-17 15:53:03 +0100412 mp_map_elem_t *next = dict_iter_next(self->dict, &self->cur);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000413
Damien George8a9b9992014-09-17 15:53:03 +0100414 if (next == NULL) {
415 return MP_OBJ_STOP_ITERATION;
416 } else {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000417 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000418 case MP_DICT_VIEW_ITEMS:
Damien Georged2d64f02015-01-14 21:32:42 +0000419 default: {
Damien Georgeeae16442014-01-11 19:22:29 +0000420 mp_obj_t items[] = {next->key, next->value};
421 return mp_obj_new_tuple(2, items);
422 }
423 case MP_DICT_VIEW_KEYS:
424 return next->key;
425 case MP_DICT_VIEW_VALUES:
426 return next->value;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000427 }
John R. Lenton9ec3a872014-01-10 01:00:20 +0000428 }
429}
430
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200431STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000432 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000433 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300434 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000435 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000436};
437
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200438STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000439 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
440 mp_obj_dict_view_t *view = view_in;
441 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
442 o->base.type = &dict_view_it_type;
443 o->kind = view->kind;
Damien George8a9b9992014-09-17 15:53:03 +0100444 o->dict = view->dict;
445 o->cur = 0;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000446 return o;
447}
448
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200449STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000450 (void)kind;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000451 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
452 mp_obj_dict_view_t *self = self_in;
453 bool first = true;
454 print(env, mp_dict_view_names[self->kind]);
455 print(env, "([");
456 mp_obj_t *self_iter = dict_view_getiter(self);
457 mp_obj_t *next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100458 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000459 if (!first) {
460 print(env, ", ");
461 }
462 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200463 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000464 }
465 print(env, "])");
466}
467
Damien Georgeecc88e92014-08-30 00:35:11 +0100468STATIC mp_obj_t dict_view_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100469 // only supported for the 'keys' kind until sets and dicts are refactored
John R. Lentonc1bef212014-01-11 12:39:33 +0000470 mp_obj_dict_view_t *o = lhs_in;
Damien Georged0a5bf32014-05-10 13:55:11 +0100471 if (o->kind != MP_DICT_VIEW_KEYS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100472 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100473 }
474 if (op != MP_BINARY_OP_IN) {
Damien George6ac5dce2014-05-21 19:42:43 +0100475 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100476 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000477 return dict_binary_op(op, o->dict, rhs_in);
478}
479
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200480STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000481 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000482 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000483 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000484 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000485 .getiter = dict_view_getiter,
486};
487
Damien George969a6b32014-12-10 22:07:04 +0000488STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000489 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
490 o->base.type = &dict_view_type;
491 o->dict = dict;
492 o->kind = kind;
493 return o;
494}
495
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200496STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000497 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000498 mp_obj_dict_t *self = self_in;
499 return mp_obj_new_dict_view(self, kind);
500}
501
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200502STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000503 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
504}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200505STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000506
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200507STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000508 return dict_view(self_in, MP_DICT_VIEW_KEYS);
509}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200510STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000511
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200512STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000513 return dict_view(self_in, MP_DICT_VIEW_VALUES);
514}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200515STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000516
John R. Lenton4bee76e2014-01-10 11:25:03 +0000517/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000518/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000519
Damien George9b196cd2014-03-26 21:47:19 +0000520STATIC const mp_map_elem_t dict_locals_dict_table[] = {
521 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
522 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
523 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
524 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
525 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
526 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
527 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
528 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
529 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
530 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
531 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
Paul Sokolovsky68e7c512014-04-13 11:53:15 +0300532 { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
Paul Sokolovskycd94b382014-04-13 23:41:49 +0300533 { MP_OBJ_NEW_QSTR(MP_QSTR___setitem__), (mp_obj_t)&mp_op_setitem_obj },
Paul Sokolovsky14de1142014-04-13 23:55:59 +0300534 { MP_OBJ_NEW_QSTR(MP_QSTR___delitem__), (mp_obj_t)&mp_op_delitem_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000535};
536
Damien George9b196cd2014-03-26 21:47:19 +0000537STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
538
Damien George3e1a5c12014-03-29 13:43:38 +0000539const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000540 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000541 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200542 .print = dict_print,
543 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000544 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200545 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100546 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000547 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000548 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000549};
550
Damien George93965e72014-08-30 13:23:35 +0100551void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args) {
Damien George8b0535e2014-04-05 21:53:54 +0100552 dict->base.type = &mp_type_dict;
553 mp_map_init(&dict->map, n_args);
554}
555
Damien George93965e72014-08-30 13:23:35 +0100556mp_obj_t mp_obj_new_dict(mp_uint_t n_args) {
Damiend99b0522013-12-21 18:17:45 +0000557 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100558 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000559 return o;
560}
Damiendae7eb72013-12-29 22:32:51 +0000561
Damien George93965e72014-08-30 13:23:35 +0100562mp_uint_t mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000563 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000564}
565
566mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000567 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000568 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000569 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000570 return self_in;
571}
Damien George062478e2014-01-09 20:57:50 +0000572
Damien George66edc5d2014-04-05 13:25:13 +0100573mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
574 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
575 mp_obj_dict_t *self = self_in;
Damien George8a9b9992014-09-17 15:53:03 +0100576 dict_get_helper(&self->map, key, MP_OBJ_NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
Damien George66edc5d2014-04-05 13:25:13 +0100577 return self_in;
578}
579
Damien George062478e2014-01-09 20:57:50 +0000580mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000581 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000582 mp_obj_dict_t *self = self_in;
583 return &self->map;
584}