blob: aea5952d3ddad3a12fbf477b172d65a6e3e393ba [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovsky016d9a42019-05-14 15:51:57 +03007 * Copyright (c) 2014-2017 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
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
Damiend99b0522013-12-21 18:17:45 +000028#include <string.h>
29#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/runtime.h"
32#include "py/builtin.h"
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020033#include "py/objtype.h"
Eric Poulsen01054f22019-07-16 14:29:22 -070034#include "py/objstr.h"
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020035
Jim Mussared880875b2020-10-09 17:10:29 +110036const 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
Damien George4b72b3a2016-01-03 14:21:40 +000048STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
John R. Lentona41fe312014-01-06 17:17:02 +000049
Damien George8a9b9992014-09-17 15:53:03 +010050// 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.
Damien George1ea2f7a2017-02-16 16:15:04 +110053STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
54 size_t max = dict->map.alloc;
Damien George8a9b9992014-09-17 15:53:03 +010055 mp_map_t *map = &dict->map;
56
Jim Mussared57fce3b2020-04-23 01:10:30 +100057 size_t i = *cur;
58 for (; i < max; i++) {
Damien George054dd332019-01-30 21:57:29 +110059 if (mp_map_slot_is_filled(map, i)) {
Damien George8a9b9992014-09-17 15:53:03 +010060 *cur = i + 1;
61 return &(map->table[i]);
62 }
63 }
64
Jim Mussared57fce3b2020-04-23 01:10:30 +100065 assert(map->used == 0 || i == max);
Damien George8a9b9992014-09-17 15:53:03 +010066 return NULL;
67}
68
Damien George7f9d1d62015-04-09 23:56:15 +010069STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George999cedb2015-11-27 17:01:44 +000070 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damiend99b0522013-12-21 18:17:45 +000071 bool first = true;
Damien George612045f2014-09-17 22:56:34 +010072 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
73 kind = PRINT_REPR;
74 }
Andrew Leech1e87f112019-11-11 15:44:04 +110075 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) {
Damien George044c4732015-04-11 13:03:37 +010076 mp_printf(print, "%q(", self->base.type->name);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020077 }
Damien George7f9d1d62015-04-09 23:56:15 +010078 mp_print_str(print, "{");
Damien George1ea2f7a2017-02-16 16:15:04 +110079 size_t cur = 0;
John R. Lentona41fe312014-01-06 17:17:02 +000080 mp_map_elem_t *next = NULL;
Damien George8a9b9992014-09-17 15:53:03 +010081 while ((next = dict_iter_next(self, &cur)) != NULL) {
John R. Lentona41fe312014-01-06 17:17:02 +000082 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +010083 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +000084 }
John R. Lentona41fe312014-01-06 17:17:02 +000085 first = false;
Eric Poulsen01054f22019-07-16 14:29:22 -070086 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 }
Damien George7f9d1d62015-04-09 23:56:15 +010090 mp_obj_print_helper(print, next->key, kind);
Eric Poulsen01054f22019-07-16 14:29:22 -070091 if (add_quote) {
92 mp_print_str(print, "\"");
93 }
Damien George7f9d1d62015-04-09 23:56:15 +010094 mp_print_str(print, ": ");
95 mp_obj_print_helper(print, next->value, kind);
Damiend99b0522013-12-21 18:17:45 +000096 }
Damien George7f9d1d62015-04-09 23:56:15 +010097 mp_print_str(print, "}");
Andrew Leech1e87f112019-11-11 15:44:04 +110098 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) {
Damien George7f9d1d62015-04-09 23:56:15 +010099 mp_print_str(print, ")");
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200100 }
Damiend99b0522013-12-21 18:17:45 +0000101}
102
Damien George456a3ab2020-06-21 17:10:20 +1000103mp_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) {
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200104 mp_obj_t dict_out = mp_obj_new_dict(0);
Damien George999cedb2015-11-27 17:01:44 +0000105 mp_obj_dict_t *dict = MP_OBJ_TO_PTR(dict_out);
Damien George5b3f0b72016-01-03 15:55:55 +0000106 dict->base.type = type;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200107 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Damien George5b3f0b72016-01-03 15:55:55 +0000108 if (type == &mp_type_ordereddict) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200109 dict->map.is_ordered = 1;
110 }
111 #endif
Damien Georgebcb6ca42014-06-03 12:53:44 +0100112 if (n_args > 0 || n_kw > 0) {
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200113 mp_obj_t args2[2] = {dict_out, args[0]}; // args[0] is always valid, even if it's not a positional arg
Damien Georgebcb6ca42014-06-03 12:53:44 +0100114 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
Damien Georged7aadcf2014-04-04 15:08:00 +0100117 }
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200118 return dict_out;
Damien George71c51812014-01-04 20:21:15 +0000119}
120
Damien George58321dd2017-08-29 13:04:01 +1000121STATIC mp_obj_t dict_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000122 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George4e8dc8c2014-01-27 23:15:32 +0000123 switch (op) {
Damien George69661f32020-02-27 15:36:53 +1100124 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);
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300128 #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
Damien George69661f32020-02-27 15:36:53 +1100134 default:
135 return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +0000136 }
137}
138
Damien George58321dd2017-08-29 13:04:01 +1000139STATIC mp_obj_t dict_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George999cedb2015-11-27 17:01:44 +0000140 mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
Damiend99b0522013-12-21 18:17:45 +0000141 switch (op) {
Damien George5e34a112017-11-24 13:04:24 +1100142 case MP_BINARY_OP_CONTAINS: {
John R. Lentonc1bef212014-01-11 12:39:33 +0000143 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300144 return mp_obj_new_bool(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000145 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300146 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200147 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Damien Georgeeee1e882019-01-30 18:49:52 +1100148 if (MP_UNLIKELY(mp_obj_is_type(lhs_in, &mp_type_ordereddict) && mp_obj_is_type(rhs_in, &mp_type_ordereddict))) {
Mark Anthony Palomer31310532016-06-05 18:48:25 -0700149 // Iterate through both dictionaries simultaneously and compare keys and values.
150 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
Damien George1ea2f7a2017-02-16 16:15:04 +1100151 size_t c1 = 0, c2 = 0;
Mark Anthony Palomer31310532016-06-05 18:48:25 -0700152 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;
Damien Georgea1b18b32020-02-20 00:17:13 +1100159 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200160 #endif
Damien Georgea1b18b32020-02-20 00:17:13 +1100161
Damien Georgeeee1e882019-01-30 18:49:52 +1100162 if (mp_obj_is_type(rhs_in, &mp_type_dict)) {
Damien George999cedb2015-11-27 17:01:44 +0000163 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300164 if (o->map.used != rhs->map.used) {
165 return mp_const_false;
166 }
167
Damien George1ea2f7a2017-02-16 16:15:04 +1100168 size_t cur = 0;
Damien George8a9b9992014-09-17 15:53:03 +0100169 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;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300174 }
175 }
176 return mp_const_true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000177 } else {
178 // dict is not equal to instance of any other type
179 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300180 }
181 }
Damiend99b0522013-12-21 18:17:45 +0000182 default:
183 // op not supported
Damien George6ac5dce2014-05-21 19:42:43 +0100184 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000185 }
186}
187
Damien George814f17a2018-09-27 15:14:12 +1000188// Note: Make sure this is inlined in load part of dict_subscr() below.
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300189mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
Damien George999cedb2015-11-27 17:01:44 +0000190 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300191 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
192 if (elem == NULL) {
Damien George2750a7b2016-10-17 12:00:19 +1100193 nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300194 } else {
195 return elem->value;
196 }
197}
198
Damien George729f7b42014-04-17 22:10:53 +0100199STATIC 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 +0100200 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100201 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100202 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100203 return mp_const_none;
204 } else if (value == MP_OBJ_SENTINEL) {
205 // load
Damien George999cedb2015-11-27 17:01:44 +0000206 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George729f7b42014-04-17 22:10:53 +0100207 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
208 if (elem == NULL) {
Damien George2750a7b2016-10-17 12:00:19 +1100209 nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
Damien George729f7b42014-04-17 22:10:53 +0100210 } else {
211 return elem->value;
212 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100213 } else {
Damien George729f7b42014-04-17 22:10:53 +0100214 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100215 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100216 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100217 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100218}
John R. Lentona41fe312014-01-06 17:17:02 +0000219
220/******************************************************************************/
John R. Lentona41fe312014-01-06 17:17:02 +0000221/* dict methods */
222
Mike Wadstena3e01d32018-02-18 21:51:04 -0600223STATIC 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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200229STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George456a3ab2020-06-21 17:10:20 +1000230 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000231 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Mike Wadstena3e01d32018-02-18 21:51:04 -0600232 mp_ensure_not_fixed(self);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000233
234 mp_map_clear(&self->map);
235
236 return mp_const_none;
237}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200238STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000239
Andrew Leech28370c02020-06-10 10:45:24 +1000240mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) {
Damien George456a3ab2020-06-21 17:10:20 +1000241 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000242 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200243 mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
Damien George999cedb2015-11-27 17:01:44 +0000244 mp_obj_dict_t *other = MP_OBJ_TO_PTR(other_out);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200245 other->base.type = self->base.type;
John R. Lentond90b19e2014-01-06 18:11:20 +0000246 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300247 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200248 other->map.is_fixed = 0;
249 other->map.is_ordered = self->map.is_ordered;
John R. Lentond90b19e2014-01-06 18:11:20 +0000250 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200251 return other_out;
John R. Lentond90b19e2014-01-06 18:11:20 +0000252}
Andrew Leech28370c02020-06-10 10:45:24 +1000253STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000254
Paul Sokolovskyfbb83352018-08-06 01:25:41 +0300255#if MICROPY_PY_BUILTINS_DICT_FROMKEYS
Damien Georgeeae16442014-01-11 19:22:29 +0000256// this is a classmethod
Damien George4b72b3a2016-01-03 14:21:40 +0000257STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
Damien Georgee6003f42017-02-13 15:44:31 +1100258 mp_obj_t iter = mp_getiter(args[1], NULL);
Damien Georgeeae16442014-01-11 19:22:29 +0000259 mp_obj_t value = mp_const_none;
Damien George83229d32015-11-20 14:09:20 +0000260 mp_obj_t next = MP_OBJ_NULL;
Damien Georgeeae16442014-01-11 19:22:29 +0000261
262 if (n_args > 2) {
263 value = args[2];
264 }
265
Damien Georgea3edeb92016-10-17 11:58:57 +1100266 // 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]);
Damien Georgeeae16442014-01-11 19:22:29 +0000269 if (len == MP_OBJ_NULL) {
270 /* object's type doesn't have a __len__ slot */
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200271 self_out = mp_obj_new_dict(0);
Damien Georgeeae16442014-01-11 19:22:29 +0000272 } else {
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200273 self_out = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
Damien Georgeeae16442014-01-11 19:22:29 +0000274 }
275
Damien George999cedb2015-11-27 17:01:44 +0000276 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_out);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100277 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000278 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
279 }
280
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200281 return self_out;
Damien Georgeeae16442014-01-11 19:22:29 +0000282}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200283STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
Damien Georgecbf76742015-11-27 13:38:15 +0000284STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, MP_ROM_PTR(&dict_fromkeys_fun_obj));
Paul Sokolovskyfbb83352018-08-06 01:25:41 +0300285#endif
Damien Georgeeae16442014-01-11 19:22:29 +0000286
Damien George8b84b8a2017-07-04 23:24:59 +1000287STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_lookup_kind_t lookup_kind) {
Damien George456a3ab2020-06-21 17:10:20 +1000288 mp_check_self(mp_obj_is_dict_or_ordereddict(args[0]));
Damien George8b84b8a2017-07-04 23:24:59 +1000289 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
Mike Wadstena3e01d32018-02-18 21:51:04 -0600290 if (lookup_kind != MP_MAP_LOOKUP) {
291 mp_ensure_not_fixed(self);
292 }
Damien George8b84b8a2017-07-04 23:24:59 +1000293 mp_map_elem_t *elem = mp_map_lookup(&self->map, args[1], lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000294 mp_obj_t value;
Damien George8a9b9992014-09-17 15:53:03 +0100295 if (elem == NULL || elem->value == MP_OBJ_NULL) {
Damien George8b84b8a2017-07-04 23:24:59 +1000296 if (n_args == 2) {
Damien George38a2da62014-01-08 17:33:12 +0000297 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George8b84b8a2017-07-04 23:24:59 +1000298 nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, args[1]));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000299 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000300 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000301 }
302 } else {
Damien George8b84b8a2017-07-04 23:24:59 +1000303 value = args[2];
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000304 }
Damien George8a9b9992014-09-17 15:53:03 +0100305 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
306 elem->value = value;
307 }
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000308 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000309 value = elem->value;
Damien George8a9b9992014-09-17 15:53:03 +0100310 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
311 elem->value = MP_OBJ_NULL; // so that GC can collect the deleted value
312 }
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000313 }
314 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000315}
316
Damien George4b72b3a2016-01-03 14:21:40 +0000317STATIC mp_obj_t dict_get(size_t n_args, const mp_obj_t *args) {
Damien George8b84b8a2017-07-04 23:24:59 +1000318 return dict_get_helper(n_args, args, MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000319}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200320STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000321
Damien George4b72b3a2016-01-03 14:21:40 +0000322STATIC mp_obj_t dict_pop(size_t n_args, const mp_obj_t *args) {
Damien George8b84b8a2017-07-04 23:24:59 +1000323 return dict_get_helper(n_args, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000324}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200325STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000326
Damien George4b72b3a2016-01-03 14:21:40 +0000327STATIC mp_obj_t dict_setdefault(size_t n_args, const mp_obj_t *args) {
Damien George8b84b8a2017-07-04 23:24:59 +1000328 return dict_get_helper(n_args, args, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000329}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200330STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000331
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200332STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George456a3ab2020-06-21 17:10:20 +1000333 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000334 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Mike Wadstena3e01d32018-02-18 21:51:04 -0600335 mp_ensure_not_fixed(self);
Jim Mussared57fce3b2020-04-23 01:10:30 +1000336 if (self->map.used == 0) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100337 mp_raise_msg(&mp_type_KeyError, MP_ERROR_TEXT("popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000338 }
Jim Mussared57fce3b2020-04-23 01:10:30 +1000339 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);
John R. Lentonf77dce82014-01-06 20:08:52 +0000347 self->map.used--;
348 mp_obj_t items[] = {next->key, next->value};
Damien George8a9b9992014-09-17 15:53:03 +0100349 next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
350 next->value = MP_OBJ_NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000351 mp_obj_t tuple = mp_obj_new_tuple(2, items);
352
353 return tuple;
354}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200355STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000356
Damien George4b72b3a2016-01-03 14:21:40 +0000357STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George456a3ab2020-06-21 17:10:20 +1000358 mp_check_self(mp_obj_is_dict_or_ordereddict(args[0]));
Damien George999cedb2015-11-27 17:01:44 +0000359 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
Mike Wadstena3e01d32018-02-18 21:51:04 -0600360 mp_ensure_not_fixed(self);
Damien Georgebcb6ca42014-06-03 12:53:44 +0100361
362 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
363
364 if (n_args == 2) {
365 // given a positional argument
366
Damien George456a3ab2020-06-21 17:10:20 +1000367 if (mp_obj_is_dict_or_ordereddict(args[1])) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100368 // update from other dictionary (make sure other is not self)
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200369 if (args[1] != args[0]) {
Damien George1ea2f7a2017-02-16 16:15:04 +1100370 size_t cur = 0;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100371 mp_map_elem_t *elem = NULL;
Damien George69661f32020-02-27 15:36:53 +1100372 while ((elem = dict_iter_next((mp_obj_dict_t *)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100373 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
374 }
375 }
John R. Lenton88f30432014-01-06 22:58:17 +0000376 } else {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100377 // update from a generic iterable of pairs
Damien Georgee6003f42017-02-13 15:44:31 +1100378 mp_obj_t iter = mp_getiter(args[1], NULL);
Damien George83229d32015-11-20 14:09:20 +0000379 mp_obj_t next = MP_OBJ_NULL;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100380 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgee6003f42017-02-13 15:44:31 +1100381 mp_obj_t inneriter = mp_getiter(next, NULL);
Damien Georgebcb6ca42014-06-03 12:53:44 +0100382 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) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100388 mp_raise_ValueError(MP_ERROR_TEXT("dict update sequence has wrong length"));
Damien Georgebcb6ca42014-06-03 12:53:44 +0100389 } 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
Damien George1ea2f7a2017-02-16 16:15:04 +1100397 for (size_t i = 0; i < kwargs->alloc; i++) {
Damien George054dd332019-01-30 21:57:29 +1100398 if (mp_map_slot_is_filled(kwargs, i)) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100399 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 +0000400 }
401 }
402
403 return mp_const_none;
404}
Damien Georgebcb6ca42014-06-03 12:53:44 +0100405STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000406
John R. Lentonf77dce82014-01-06 20:08:52 +0000407
John R. Lentona41fe312014-01-06 17:17:02 +0000408/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000409/* dict views */
410
Damien George9fef1c02021-03-26 13:48:34 +1100411STATIC const mp_obj_type_t mp_type_dict_view;
412STATIC const mp_obj_type_t mp_type_dict_view_it;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000413
414typedef 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
Damien George3ff16ff2016-05-20 12:38:15 +0100420STATIC const char *const mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000421
422typedef struct _mp_obj_dict_view_it_t {
423 mp_obj_base_t base;
424 mp_dict_view_kind_t kind;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200425 mp_obj_t dict;
Damien George1ea2f7a2017-02-16 16:15:04 +1100426 size_t cur;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000427} mp_obj_dict_view_it_t;
428
429typedef struct _mp_obj_dict_view_t {
430 mp_obj_base_t base;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200431 mp_obj_t dict;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000432 mp_dict_view_kind_t kind;
433} mp_obj_dict_view_t;
434
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200435STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
Damien George9fef1c02021-03-26 13:48:34 +1100436 mp_check_self(mp_obj_is_type(self_in, &mp_type_dict_view_it));
Damien George999cedb2015-11-27 17:01:44 +0000437 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);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000439
Damien George8a9b9992014-09-17 15:53:03 +0100440 if (next == NULL) {
441 return MP_OBJ_STOP_ITERATION;
442 } else {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000443 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000444 case MP_DICT_VIEW_ITEMS:
Damien Georged2d64f02015-01-14 21:32:42 +0000445 default: {
Damien Georgeeae16442014-01-11 19:22:29 +0000446 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;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000453 }
John R. Lenton9ec3a872014-01-10 01:00:20 +0000454 }
455}
456
Damien George9fef1c02021-03-26 13:48:34 +1100457STATIC const mp_obj_type_t mp_type_dict_view_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000458 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000459 .name = MP_QSTR_iterator,
Damien Georgeae8d8672016-01-09 23:14:54 +0000460 .getiter = mp_identity_getiter,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000461 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000462};
463
Damien Georgeae8d8672016-01-09 23:14:54 +0000464STATIC 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));
Damien George9fef1c02021-03-26 13:48:34 +1100466 mp_check_self(mp_obj_is_type(view_in, &mp_type_dict_view));
Damien George999cedb2015-11-27 17:01:44 +0000467 mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in);
Damien George69661f32020-02-27 15:36:53 +1100468 mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf;
Damien George9fef1c02021-03-26 13:48:34 +1100469 o->base.type = &mp_type_dict_view_it;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000470 o->kind = view->kind;
Damien George8a9b9992014-09-17 15:53:03 +0100471 o->dict = view->dict;
472 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000473 return MP_OBJ_FROM_PTR(o);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000474}
475
Damien George7f9d1d62015-04-09 23:56:15 +0100476STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000477 (void)kind;
Damien George9fef1c02021-03-26 13:48:34 +1100478 mp_check_self(mp_obj_is_type(self_in, &mp_type_dict_view));
Damien George999cedb2015-11-27 17:01:44 +0000479 mp_obj_dict_view_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000480 bool first = true;
Damien George7f9d1d62015-04-09 23:56:15 +0100481 mp_print_str(print, mp_dict_view_names[self->kind]);
482 mp_print_str(print, "([");
Damien Georgeae8d8672016-01-09 23:14:54 +0000483 mp_obj_iter_buf_t iter_buf;
484 mp_obj_t self_iter = dict_view_getiter(self_in, &iter_buf);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200485 mp_obj_t next = MP_OBJ_NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100486 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000487 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +0100488 mp_print_str(print, ", ");
John R. Lenton9ec3a872014-01-10 01:00:20 +0000489 }
490 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +0100491 mp_obj_print_helper(print, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000492 }
Damien George7f9d1d62015-04-09 23:56:15 +0100493 mp_print_str(print, "])");
John R. Lenton9ec3a872014-01-10 01:00:20 +0000494}
495
Damien George58321dd2017-08-29 13:04:01 +1000496STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100497 // only supported for the 'keys' kind until sets and dicts are refactored
Damien George999cedb2015-11-27 17:01:44 +0000498 mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
Damien Georged0a5bf32014-05-10 13:55:11 +0100499 if (o->kind != MP_DICT_VIEW_KEYS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100500 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100501 }
Damien George5e34a112017-11-24 13:04:24 +1100502 if (op != MP_BINARY_OP_CONTAINS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100503 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100504 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000505 return dict_binary_op(op, o->dict, rhs_in);
506}
507
Damien George9fef1c02021-03-26 13:48:34 +1100508STATIC const mp_obj_type_t mp_type_dict_view = {
Damien Georgec5966122014-02-15 16:10:44 +0000509 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000510 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000511 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000512 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000513 .getiter = dict_view_getiter,
514};
515
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200516STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) {
Damien George999cedb2015-11-27 17:01:44 +0000517 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
Damien George9fef1c02021-03-26 13:48:34 +1100518 o->base.type = &mp_type_dict_view;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000519 o->dict = dict;
520 o->kind = kind;
Damien George999cedb2015-11-27 17:01:44 +0000521 return MP_OBJ_FROM_PTR(o);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000522}
523
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200524STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George456a3ab2020-06-21 17:10:20 +1000525 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200526 return mp_obj_new_dict_view(self_in, kind);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000527}
528
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200529STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000530 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
531}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200532STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000533
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200534STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000535 return dict_view(self_in, MP_DICT_VIEW_KEYS);
536}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200537STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000538
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200539STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000540 return dict_view(self_in, MP_DICT_VIEW_VALUES);
541}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200542STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000543
John R. Lenton4bee76e2014-01-10 11:25:03 +0000544/******************************************************************************/
Damien George2161d6b2017-11-27 23:40:31 +1100545/* dict iterator */
546
547STATIC 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));
Damien George456a3ab2020-06-21 17:10:20 +1000549 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
Damien George69661f32020-02-27 15:36:53 +1100550 mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf;
Damien George9fef1c02021-03-26 13:48:34 +1100551 o->base.type = &mp_type_dict_view_it;
Damien George2161d6b2017-11-27 23:40:31 +1100552 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/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000559/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000560
Damien Georgecbf76742015-11-27 13:38:15 +0000561STATIC 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) },
Paul Sokolovskyfbb83352018-08-06 01:25:41 +0300564 #if MICROPY_PY_BUILTINS_DICT_FROMKEYS
Damien Georgecbf76742015-11-27 13:38:15 +0000565 { MP_ROM_QSTR(MP_QSTR_fromkeys), MP_ROM_PTR(&dict_fromkeys_obj) },
Paul Sokolovskyfbb83352018-08-06 01:25:41 +0300566 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000567 { 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) },
John R. Lentonbaa66542014-01-07 23:18:25 +0000578};
579
Damien George9b196cd2014-03-26 21:47:19 +0000580STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
581
Damien George3e1a5c12014-03-29 13:43:38 +0000582const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000583 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000584 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200585 .print = dict_print,
Damien George456a3ab2020-06-21 17:10:20 +1000586 .make_new = mp_obj_dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000587 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200588 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100589 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000590 .getiter = dict_getiter,
Damien George69661f32020-02-27 15:36:53 +1100591 .locals_dict = (mp_obj_dict_t *)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000592};
593
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200594#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200595const mp_obj_type_t mp_type_ordereddict = {
596 { &mp_type_type },
597 .name = MP_QSTR_OrderedDict,
598 .print = dict_print,
Damien George456a3ab2020-06-21 17:10:20 +1000599 .make_new = mp_obj_dict_make_new,
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200600 .unary_op = dict_unary_op,
601 .binary_op = dict_binary_op,
602 .subscr = dict_subscr,
603 .getiter = dict_getiter,
Damien George816413e2017-04-06 12:09:01 +1000604 .parent = &mp_type_dict,
Damien George69661f32020-02-27 15:36:53 +1100605 .locals_dict = (mp_obj_dict_t *)&dict_locals_dict,
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200606};
607#endif
608
Damien George1ea2f7a2017-02-16 16:15:04 +1100609void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args) {
Damien George8b0535e2014-04-05 21:53:54 +0100610 dict->base.type = &mp_type_dict;
611 mp_map_init(&dict->map, n_args);
612}
613
Damien George1ea2f7a2017-02-16 16:15:04 +1100614mp_obj_t mp_obj_new_dict(size_t n_args) {
Damien George999cedb2015-11-27 17:01:44 +0000615 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100616 mp_obj_dict_init(o, n_args);
Damien George999cedb2015-11-27 17:01:44 +0000617 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000618}
Damiendae7eb72013-12-29 22:32:51 +0000619
Damien George1ea2f7a2017-02-16 16:15:04 +1100620size_t mp_obj_dict_len(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000621 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200622 return self->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000623}
624
625mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
Damien George456a3ab2020-06-21 17:10:20 +1000626 mp_check_self(mp_obj_is_dict_or_ordereddict(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000627 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Mike Wadstena3e01d32018-02-18 21:51:04 -0600628 mp_ensure_not_fixed(self);
Damien George38a2da62014-01-08 17:33:12 +0000629 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000630 return self_in;
631}
Damien George062478e2014-01-09 20:57:50 +0000632
Damien George66edc5d2014-04-05 13:25:13 +0100633mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
Damien George8b84b8a2017-07-04 23:24:59 +1000634 mp_obj_t args[2] = {self_in, key};
635 dict_get_helper(2, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
Damien George66edc5d2014-04-05 13:25:13 +0100636 return self_in;
637}