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