1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 * Copyright (c) 2014-2017 Paul Sokolovsky
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 #include <string.h>
29 #include <assert.h>
30
31 #include "py/runtime.h"
32 #include "py/builtin.h"
33 #include "py/objtype.h"
34 #include "py/objstr.h"
35
36 const mp_obj_dict_t mp_const_empty_dict_obj = {
37 .base = { .type = &mp_type_dict },
38 .map = {
39 .all_keys_are_qstrs = 0,
40 .is_fixed = 1,
41 .is_ordered = 1,
42 .used = 0,
43 .alloc = 0,
44 .table = NULL,
45 }
46 };
47
48 STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
49
50 // This is a helper function to iterate through a dictionary. The state of
51 // the iteration is held in *cur and should be initialised with zero for the
52 // first call. Will return NULL when no more elements are available.
dict_iter_next(mp_obj_dict_t * dict,size_t * cur)53 STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
54 size_t max = dict->map.alloc;
55 mp_map_t *map = &dict->map;
56
57 size_t i = *cur;
58 for (; i < max; i++) {
59 if (mp_map_slot_is_filled(map, i)) {
60 *cur = i + 1;
61 return &(map->table[i]);
62 }
63 }
64
65 assert(map->used == 0 || i == max);
66 return NULL;
67 }
68
dict_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)69 STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
70 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
71 bool first = true;
72 const char *item_separator = ", ";
73 const char *key_separator = ": ";
74 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
75 kind = PRINT_REPR;
76 } else {
77 #if MICROPY_PY_UJSON_SEPARATORS
78 item_separator = MP_PRINT_GET_EXT(print)->item_separator;
79 key_separator = MP_PRINT_GET_EXT(print)->key_separator;
80 #endif
81 }
82 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) {
83 mp_printf(print, "%q(", self->base.type->name);
84 }
85 mp_print_str(print, "{");
86 size_t cur = 0;
87 mp_map_elem_t *next = NULL;
88 while ((next = dict_iter_next(self, &cur)) != NULL) {
89 if (!first) {
90 mp_print_str(print, item_separator);
91 }
92 first = false;
93 bool add_quote = MICROPY_PY_UJSON && kind == PRINT_JSON && !mp_obj_is_str_or_bytes(next->key);
94 if (add_quote) {
95 mp_print_str(print, "\"");
96 }
97 mp_obj_print_helper(print, next->key, kind);
98 if (add_quote) {
99 mp_print_str(print, "\"");
100 }
101 mp_print_str(print, key_separator);
102 mp_obj_print_helper(print, next->value, kind);
103 }
104 mp_print_str(print, "}");
105 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) {
106 mp_print_str(print, ")");
107 }
108 }
109
mp_obj_dict_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)110 mp_obj_t mp_obj_dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
111 mp_obj_t dict_out = mp_obj_new_dict(0);
112 mp_obj_dict_t *dict = MP_OBJ_TO_PTR(dict_out);
113 dict->base.type = type;
114 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
115 if (type == &mp_type_ordereddict) {
116 dict->map.is_ordered = 1;
117 }
118 #endif
119 if (n_args > 0 || n_kw > 0) {
120 mp_obj_t args2[2] = {dict_out, args[0]}; // args[0] is always valid, even if it's not a positional arg
121 mp_map_t kwargs;
122 mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
123 dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
124 }
125 return dict_out;
126 }
127
dict_unary_op(mp_unary_op_t op,mp_obj_t self_in)128 STATIC mp_obj_t dict_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
129 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
130 switch (op) {
131 case MP_UNARY_OP_BOOL:
132 return mp_obj_new_bool(self->map.used != 0);
133 case MP_UNARY_OP_LEN:
134 return MP_OBJ_NEW_SMALL_INT(self->map.used);
135 #if MICROPY_PY_SYS_GETSIZEOF
136 case MP_UNARY_OP_SIZEOF: {
137 size_t sz = sizeof(*self) + sizeof(*self->map.table) * self->map.alloc;
138 return MP_OBJ_NEW_SMALL_INT(sz);
139 }
140 #endif
141 default:
142 return MP_OBJ_NULL; // op not supported
143 }
144 }
145
dict_binary_op(mp_binary_op_t op,mp_obj_t lhs_in,mp_obj_t rhs_in)146 STATIC mp_obj_t dict_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
147 mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
148 switch (op) {
149 case MP_BINARY_OP_CONTAINS: {
150 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
151 return mp_obj_new_bool(elem != NULL);
152 }
153 case MP_BINARY_OP_EQUAL: {
154 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
155 if (MP_UNLIKELY(mp_obj_is_type(lhs_in, &mp_type_ordereddict) && mp_obj_is_type(rhs_in, &mp_type_ordereddict))) {
156 // Iterate through both dictionaries simultaneously and compare keys and values.
157 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
158 size_t c1 = 0, c2 = 0;
159 mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
160 for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
161 if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
162 return mp_const_false;
163 }
164 }
165 return e1 == NULL && e2 == NULL ? mp_const_true : mp_const_false;
166 }
167 #endif
168
169 if (mp_obj_is_type(rhs_in, &mp_type_dict)) {
170 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
171 if (o->map.used != rhs->map.used) {
172 return mp_const_false;
173 }
174
175 size_t cur = 0;
176 mp_map_elem_t *next = NULL;
177 while ((next = dict_iter_next(o, &cur)) != NULL) {
178 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
179 if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
180 return mp_const_false;
181 }
182 }
183 return mp_const_true;
184 } else {
185 // dict is not equal to instance of any other type
186 return mp_const_false;
187 }
188 }
189 default:
190 // op not supported
191 return MP_OBJ_NULL;
192 }
193 }
194
195 // Note: Make sure this is inlined in load part of dict_subscr() below.
mp_obj_dict_get(mp_obj_t self_in,mp_obj_t index)196 mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
197 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
198 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
199 if (elem == NULL) {
200 mp_raise_type_arg(&mp_type_KeyError, index);
201 } else {
202 return elem->value;
203 }
204 }
205
dict_subscr(mp_obj_t self_in,mp_obj_t index,mp_obj_t value)206 STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
207 if (value == MP_OBJ_NULL) {
208 // delete
209 mp_obj_dict_delete(self_in, index);
210 return mp_const_none;
211 } else if (value == MP_OBJ_SENTINEL) {
212 // load
213 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
214 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
215 if (elem == NULL) {
216 mp_raise_type_arg(&mp_type_KeyError, index);
217 } else {
218 return elem->value;
219 }
220 } else {
221 // store
222 mp_obj_dict_store(self_in, index, value);
223 return mp_const_none;
224 }
225 }
226
227 /******************************************************************************/
228 /* dict methods */
229
mp_ensure_not_fixed(const mp_obj_dict_t * dict)230 STATIC void mp_ensure_not_fixed(const mp_obj_dict_t *dict) {
231 if (dict->map.is_fixed) {
232 mp_raise_TypeError(NULL);
233 }
234 }
235
dict_clear(mp_obj_t self_in)236 STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
237 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
238 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
239 mp_ensure_not_fixed(self);
240
241 mp_map_clear(&self->map);
242
243 return mp_const_none;
244 }
245 STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
246
mp_obj_dict_copy(mp_obj_t self_in)247 mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) {
248 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
249 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
250 mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
251 mp_obj_dict_t *other = MP_OBJ_TO_PTR(other_out);
252 other->base.type = self->base.type;
253 other->map.used = self->map.used;
254 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
255 other->map.is_fixed = 0;
256 other->map.is_ordered = self->map.is_ordered;
257 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
258 return other_out;
259 }
260 STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy);
261
262 #if MICROPY_PY_BUILTINS_DICT_FROMKEYS
263 // this is a classmethod
dict_fromkeys(size_t n_args,const mp_obj_t * args)264 STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
265 mp_obj_t iter = mp_getiter(args[1], NULL);
266 mp_obj_t value = mp_const_none;
267 mp_obj_t next = MP_OBJ_NULL;
268
269 if (n_args > 2) {
270 value = args[2];
271 }
272
273 // optimisation to allocate result based on len of argument
274 mp_obj_t self_out;
275 mp_obj_t len = mp_obj_len_maybe(args[1]);
276 if (len == MP_OBJ_NULL) {
277 /* object's type doesn't have a __len__ slot */
278 self_out = mp_obj_new_dict(0);
279 } else {
280 self_out = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
281 }
282
283 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_out);
284 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
285 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
286 }
287
288 return self_out;
289 }
290 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
291 STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, MP_ROM_PTR(&dict_fromkeys_fun_obj));
292 #endif
293
dict_get_helper(size_t n_args,const mp_obj_t * args,mp_map_lookup_kind_t lookup_kind)294 STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_lookup_kind_t lookup_kind) {
295 mp_check_self(mp_obj_is_dict_or_ordereddict(args[0]));
296 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
297 if (lookup_kind != MP_MAP_LOOKUP) {
298 mp_ensure_not_fixed(self);
299 }
300 mp_map_elem_t *elem = mp_map_lookup(&self->map, args[1], lookup_kind);
301 mp_obj_t value;
302 if (elem == NULL || elem->value == MP_OBJ_NULL) {
303 if (n_args == 2) {
304 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
305 mp_raise_type_arg(&mp_type_KeyError, args[1]);
306 } else {
307 value = mp_const_none;
308 }
309 } else {
310 value = args[2];
311 }
312 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
313 elem->value = value;
314 }
315 } else {
316 value = elem->value;
317 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
318 elem->value = MP_OBJ_NULL; // so that GC can collect the deleted value
319 }
320 }
321 return value;
322 }
323
dict_get(size_t n_args,const mp_obj_t * args)324 STATIC mp_obj_t dict_get(size_t n_args, const mp_obj_t *args) {
325 return dict_get_helper(n_args, args, MP_MAP_LOOKUP);
326 }
327 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
328
dict_pop(size_t n_args,const mp_obj_t * args)329 STATIC mp_obj_t dict_pop(size_t n_args, const mp_obj_t *args) {
330 return dict_get_helper(n_args, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
331 }
332 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
333
dict_setdefault(size_t n_args,const mp_obj_t * args)334 STATIC mp_obj_t dict_setdefault(size_t n_args, const mp_obj_t *args) {
335 return dict_get_helper(n_args, args, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
336 }
337 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
338
dict_popitem(mp_obj_t self_in)339 STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
340 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
341 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
342 mp_ensure_not_fixed(self);
343 if (self->map.used == 0) {
344 mp_raise_msg(&mp_type_KeyError, MP_ERROR_TEXT("popitem(): dictionary is empty"));
345 }
346 size_t cur = 0;
347 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
348 if (self->map.is_ordered) {
349 cur = self->map.used - 1;
350 }
351 #endif
352 mp_map_elem_t *next = dict_iter_next(self, &cur);
353 assert(next);
354 self->map.used--;
355 mp_obj_t items[] = {next->key, next->value};
356 next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
357 next->value = MP_OBJ_NULL;
358 mp_obj_t tuple = mp_obj_new_tuple(2, items);
359
360 return tuple;
361 }
362 STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
363
dict_update(size_t n_args,const mp_obj_t * args,mp_map_t * kwargs)364 STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
365 mp_check_self(mp_obj_is_dict_or_ordereddict(args[0]));
366 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
367 mp_ensure_not_fixed(self);
368
369 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
370
371 if (n_args == 2) {
372 // given a positional argument
373
374 if (mp_obj_is_dict_or_ordereddict(args[1])) {
375 // update from other dictionary (make sure other is not self)
376 if (args[1] != args[0]) {
377 size_t cur = 0;
378 mp_map_elem_t *elem = NULL;
379 while ((elem = dict_iter_next((mp_obj_dict_t *)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
380 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
381 }
382 }
383 } else {
384 // update from a generic iterable of pairs
385 mp_obj_t iter = mp_getiter(args[1], NULL);
386 mp_obj_t next = MP_OBJ_NULL;
387 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
388 mp_obj_t inneriter = mp_getiter(next, NULL);
389 mp_obj_t key = mp_iternext(inneriter);
390 mp_obj_t value = mp_iternext(inneriter);
391 mp_obj_t stop = mp_iternext(inneriter);
392 if (key == MP_OBJ_STOP_ITERATION
393 || value == MP_OBJ_STOP_ITERATION
394 || stop != MP_OBJ_STOP_ITERATION) {
395 mp_raise_ValueError(MP_ERROR_TEXT("dict update sequence has wrong length"));
396 } else {
397 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
398 }
399 }
400 }
401 }
402
403 // update the dict with any keyword args
404 for (size_t i = 0; i < kwargs->alloc; i++) {
405 if (mp_map_slot_is_filled(kwargs, i)) {
406 mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
407 }
408 }
409
410 return mp_const_none;
411 }
412 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
413
414
415 /******************************************************************************/
416 /* dict views */
417
418 STATIC const mp_obj_type_t mp_type_dict_view;
419 STATIC const mp_obj_type_t mp_type_dict_view_it;
420
421 typedef enum _mp_dict_view_kind_t {
422 MP_DICT_VIEW_ITEMS,
423 MP_DICT_VIEW_KEYS,
424 MP_DICT_VIEW_VALUES,
425 } mp_dict_view_kind_t;
426
427 STATIC const char *const mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
428
429 typedef struct _mp_obj_dict_view_it_t {
430 mp_obj_base_t base;
431 mp_dict_view_kind_t kind;
432 mp_obj_t dict;
433 size_t cur;
434 } mp_obj_dict_view_it_t;
435
436 typedef struct _mp_obj_dict_view_t {
437 mp_obj_base_t base;
438 mp_obj_t dict;
439 mp_dict_view_kind_t kind;
440 } mp_obj_dict_view_t;
441
dict_view_it_iternext(mp_obj_t self_in)442 STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
443 mp_check_self(mp_obj_is_type(self_in, &mp_type_dict_view_it));
444 mp_obj_dict_view_it_t *self = MP_OBJ_TO_PTR(self_in);
445 mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
446
447 if (next == NULL) {
448 return MP_OBJ_STOP_ITERATION;
449 } else {
450 switch (self->kind) {
451 case MP_DICT_VIEW_ITEMS:
452 default: {
453 mp_obj_t items[] = {next->key, next->value};
454 return mp_obj_new_tuple(2, items);
455 }
456 case MP_DICT_VIEW_KEYS:
457 return next->key;
458 case MP_DICT_VIEW_VALUES:
459 return next->value;
460 }
461 }
462 }
463
464 STATIC const mp_obj_type_t mp_type_dict_view_it = {
465 { &mp_type_type },
466 .name = MP_QSTR_iterator,
467 .getiter = mp_identity_getiter,
468 .iternext = dict_view_it_iternext,
469 };
470
dict_view_getiter(mp_obj_t view_in,mp_obj_iter_buf_t * iter_buf)471 STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) {
472 assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t));
473 mp_check_self(mp_obj_is_type(view_in, &mp_type_dict_view));
474 mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in);
475 mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf;
476 o->base.type = &mp_type_dict_view_it;
477 o->kind = view->kind;
478 o->dict = view->dict;
479 o->cur = 0;
480 return MP_OBJ_FROM_PTR(o);
481 }
482
dict_view_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)483 STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
484 (void)kind;
485 mp_check_self(mp_obj_is_type(self_in, &mp_type_dict_view));
486 mp_obj_dict_view_t *self = MP_OBJ_TO_PTR(self_in);
487 bool first = true;
488 mp_print_str(print, mp_dict_view_names[self->kind]);
489 mp_print_str(print, "([");
490 mp_obj_iter_buf_t iter_buf;
491 mp_obj_t self_iter = dict_view_getiter(self_in, &iter_buf);
492 mp_obj_t next = MP_OBJ_NULL;
493 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
494 if (!first) {
495 mp_print_str(print, ", ");
496 }
497 first = false;
498 mp_obj_print_helper(print, next, PRINT_REPR);
499 }
500 mp_print_str(print, "])");
501 }
502
dict_view_binary_op(mp_binary_op_t op,mp_obj_t lhs_in,mp_obj_t rhs_in)503 STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
504 // only supported for the 'keys' kind until sets and dicts are refactored
505 mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
506 if (o->kind != MP_DICT_VIEW_KEYS) {
507 return MP_OBJ_NULL; // op not supported
508 }
509 if (op != MP_BINARY_OP_CONTAINS) {
510 return MP_OBJ_NULL; // op not supported
511 }
512 return dict_binary_op(op, o->dict, rhs_in);
513 }
514
515 STATIC const mp_obj_type_t mp_type_dict_view = {
516 { &mp_type_type },
517 .name = MP_QSTR_dict_view,
518 .print = dict_view_print,
519 .binary_op = dict_view_binary_op,
520 .getiter = dict_view_getiter,
521 };
522
mp_obj_new_dict_view(mp_obj_t dict,mp_dict_view_kind_t kind)523 STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) {
524 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
525 o->base.type = &mp_type_dict_view;
526 o->dict = dict;
527 o->kind = kind;
528 return MP_OBJ_FROM_PTR(o);
529 }
530
dict_view(mp_obj_t self_in,mp_dict_view_kind_t kind)531 STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
532 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
533 return mp_obj_new_dict_view(self_in, kind);
534 }
535
dict_items(mp_obj_t self_in)536 STATIC mp_obj_t dict_items(mp_obj_t self_in) {
537 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
538 }
539 STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
540
dict_keys(mp_obj_t self_in)541 STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
542 return dict_view(self_in, MP_DICT_VIEW_KEYS);
543 }
544 STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
545
dict_values(mp_obj_t self_in)546 STATIC mp_obj_t dict_values(mp_obj_t self_in) {
547 return dict_view(self_in, MP_DICT_VIEW_VALUES);
548 }
549 STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
550
551 /******************************************************************************/
552 /* dict iterator */
553
dict_getiter(mp_obj_t self_in,mp_obj_iter_buf_t * iter_buf)554 STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
555 assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t));
556 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
557 mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf;
558 o->base.type = &mp_type_dict_view_it;
559 o->kind = MP_DICT_VIEW_KEYS;
560 o->dict = self_in;
561 o->cur = 0;
562 return MP_OBJ_FROM_PTR(o);
563 }
564
565 /******************************************************************************/
566 /* dict constructors & public C API */
567
568 STATIC const mp_rom_map_elem_t dict_locals_dict_table[] = {
569 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&dict_clear_obj) },
570 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&dict_copy_obj) },
571 #if MICROPY_PY_BUILTINS_DICT_FROMKEYS
572 { MP_ROM_QSTR(MP_QSTR_fromkeys), MP_ROM_PTR(&dict_fromkeys_obj) },
573 #endif
574 { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&dict_get_obj) },
575 { MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&dict_items_obj) },
576 { MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&dict_keys_obj) },
577 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&dict_pop_obj) },
578 { MP_ROM_QSTR(MP_QSTR_popitem), MP_ROM_PTR(&dict_popitem_obj) },
579 { MP_ROM_QSTR(MP_QSTR_setdefault), MP_ROM_PTR(&dict_setdefault_obj) },
580 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&dict_update_obj) },
581 { MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&dict_values_obj) },
582 { MP_ROM_QSTR(MP_QSTR___getitem__), MP_ROM_PTR(&mp_op_getitem_obj) },
583 { MP_ROM_QSTR(MP_QSTR___setitem__), MP_ROM_PTR(&mp_op_setitem_obj) },
584 { MP_ROM_QSTR(MP_QSTR___delitem__), MP_ROM_PTR(&mp_op_delitem_obj) },
585 };
586
587 STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
588
589 const mp_obj_type_t mp_type_dict = {
590 { &mp_type_type },
591 .name = MP_QSTR_dict,
592 .print = dict_print,
593 .make_new = mp_obj_dict_make_new,
594 .unary_op = dict_unary_op,
595 .binary_op = dict_binary_op,
596 .subscr = dict_subscr,
597 .getiter = dict_getiter,
598 .locals_dict = (mp_obj_dict_t *)&dict_locals_dict,
599 };
600
601 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
602 const mp_obj_type_t mp_type_ordereddict = {
603 { &mp_type_type },
604 .name = MP_QSTR_OrderedDict,
605 .print = dict_print,
606 .make_new = mp_obj_dict_make_new,
607 .unary_op = dict_unary_op,
608 .binary_op = dict_binary_op,
609 .subscr = dict_subscr,
610 .getiter = dict_getiter,
611 .parent = &mp_type_dict,
612 .locals_dict = (mp_obj_dict_t *)&dict_locals_dict,
613 };
614 #endif
615
mp_obj_dict_init(mp_obj_dict_t * dict,size_t n_args)616 void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args) {
617 dict->base.type = &mp_type_dict;
618 mp_map_init(&dict->map, n_args);
619 }
620
mp_obj_new_dict(size_t n_args)621 mp_obj_t mp_obj_new_dict(size_t n_args) {
622 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
623 mp_obj_dict_init(o, n_args);
624 return MP_OBJ_FROM_PTR(o);
625 }
626
mp_obj_dict_len(mp_obj_t self_in)627 size_t mp_obj_dict_len(mp_obj_t self_in) {
628 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
629 return self->map.used;
630 }
631
mp_obj_dict_store(mp_obj_t self_in,mp_obj_t key,mp_obj_t value)632 mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
633 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
634 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
635 mp_ensure_not_fixed(self);
636 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
637 return self_in;
638 }
639
mp_obj_dict_delete(mp_obj_t self_in,mp_obj_t key)640 mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
641 mp_obj_t args[2] = {self_in, key};
642 dict_get_helper(2, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
643 return self_in;
644 }
645