2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2019-05-14 08:51:57 -04:00
|
|
|
* Copyright (c) 2014-2017 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/builtin.h"
|
2015-03-17 19:25:04 -04:00
|
|
|
#include "py/objtype.h"
|
2019-07-16 17:29:22 -04:00
|
|
|
#include "py/objstr.h"
|
2015-03-17 19:25:04 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
#define mp_obj_is_dict_type(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == dict_make_new)
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
|
2014-01-06 12:17:02 -05:00
|
|
|
|
2014-09-17 10:53:03 -04:00
|
|
|
// This is a helper function to iterate through a dictionary. The state of
|
|
|
|
// the iteration is held in *cur and should be initialised with zero for the
|
|
|
|
// first call. Will return NULL when no more elements are available.
|
2017-02-16 00:15:04 -05:00
|
|
|
STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
|
|
|
|
size_t max = dict->map.alloc;
|
2014-09-17 10:53:03 -04:00
|
|
|
mp_map_t *map = &dict->map;
|
|
|
|
|
2020-04-22 11:10:30 -04:00
|
|
|
size_t i = *cur;
|
|
|
|
for (; i < max; i++) {
|
2019-01-30 05:57:29 -05:00
|
|
|
if (mp_map_slot_is_filled(map, i)) {
|
2014-09-17 10:53:03 -04:00
|
|
|
*cur = i + 1;
|
|
|
|
return &(map->table[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:10:30 -04:00
|
|
|
assert(map->used == 0 || i == max);
|
2014-09-17 10:53:03 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2013-12-21 13:17:45 -05:00
|
|
|
bool first = true;
|
2014-09-17 17:56:34 -04:00
|
|
|
if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
|
|
|
|
kind = PRINT_REPR;
|
|
|
|
}
|
2019-11-10 23:44:04 -05:00
|
|
|
if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) {
|
2015-04-11 08:03:37 -04:00
|
|
|
mp_printf(print, "%q(", self->base.type->name);
|
2015-03-17 19:25:04 -04:00
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "{");
|
2017-02-16 00:15:04 -05:00
|
|
|
size_t cur = 0;
|
2014-01-06 12:17:02 -05:00
|
|
|
mp_map_elem_t *next = NULL;
|
2014-09-17 10:53:03 -04:00
|
|
|
while ((next = dict_iter_next(self, &cur)) != NULL) {
|
2014-01-06 12:17:02 -05:00
|
|
|
if (!first) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, ", ");
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
2014-01-06 12:17:02 -05:00
|
|
|
first = false;
|
2019-07-16 17:29:22 -04:00
|
|
|
bool add_quote = MICROPY_PY_UJSON && kind == PRINT_JSON && !mp_obj_is_str_or_bytes(next->key);
|
|
|
|
if (add_quote) {
|
|
|
|
mp_print_str(print, "\"");
|
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_obj_print_helper(print, next->key, kind);
|
2019-07-16 17:29:22 -04:00
|
|
|
if (add_quote) {
|
|
|
|
mp_print_str(print, "\"");
|
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, ": ");
|
|
|
|
mp_obj_print_helper(print, next->value, kind);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "}");
|
2019-11-10 23:44:04 -05:00
|
|
|
if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict && kind != PRINT_JSON) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, ")");
|
2015-03-17 19:25:04 -04:00
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
STATIC mp_obj_t dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-03-26 06:28:31 -04:00
|
|
|
mp_obj_t dict_out = mp_obj_new_dict(0);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *dict = MP_OBJ_TO_PTR(dict_out);
|
2016-01-03 10:55:55 -05:00
|
|
|
dict->base.type = type;
|
2015-03-17 19:25:04 -04:00
|
|
|
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
|
2016-01-03 10:55:55 -05:00
|
|
|
if (type == &mp_type_ordereddict) {
|
2015-03-17 19:25:04 -04:00
|
|
|
dict->map.is_ordered = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-03 07:53:44 -04:00
|
|
|
if (n_args > 0 || n_kw > 0) {
|
2015-03-26 06:28:31 -04:00
|
|
|
mp_obj_t args2[2] = {dict_out, args[0]}; // args[0] is always valid, even if it's not a positional arg
|
2014-06-03 07:53:44 -04:00
|
|
|
mp_map_t kwargs;
|
|
|
|
mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
|
|
|
|
dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
|
2014-04-04 10:08:00 -04:00
|
|
|
}
|
2015-03-26 06:28:31 -04:00
|
|
|
return dict_out;
|
2014-01-04 15:21:15 -05:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t dict_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-27 18:15:32 -05:00
|
|
|
switch (op) {
|
2020-02-26 23:36:53 -05:00
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_obj_new_bool(self->map.used != 0);
|
|
|
|
case MP_UNARY_OP_LEN:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->map.used);
|
2017-08-11 02:42:39 -04:00
|
|
|
#if MICROPY_PY_SYS_GETSIZEOF
|
|
|
|
case MP_UNARY_OP_SIZEOF: {
|
|
|
|
size_t sz = sizeof(*self) + sizeof(*self->map.table) * self->map.alloc;
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(sz);
|
|
|
|
}
|
|
|
|
#endif
|
2020-02-26 23:36:53 -05:00
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-01-27 18:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t dict_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
|
2013-12-21 13:17:45 -05:00
|
|
|
switch (op) {
|
2017-11-23 21:04:24 -05:00
|
|
|
case MP_BINARY_OP_CONTAINS: {
|
2014-01-11 07:39:33 -05:00
|
|
|
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(elem != NULL);
|
2014-01-11 07:39:33 -05:00
|
|
|
}
|
2014-04-06 14:20:52 -04:00
|
|
|
case MP_BINARY_OP_EQUAL: {
|
2015-03-17 19:25:04 -04:00
|
|
|
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
|
2019-01-30 02:49:52 -05:00
|
|
|
if (MP_UNLIKELY(mp_obj_is_type(lhs_in, &mp_type_ordereddict) && mp_obj_is_type(rhs_in, &mp_type_ordereddict))) {
|
2016-06-05 21:48:25 -04:00
|
|
|
// Iterate through both dictionaries simultaneously and compare keys and values.
|
|
|
|
mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
|
2017-02-16 00:15:04 -05:00
|
|
|
size_t c1 = 0, c2 = 0;
|
2016-06-05 21:48:25 -04:00
|
|
|
mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
|
|
|
|
for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
|
|
|
|
if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e1 == NULL && e2 == NULL ? mp_const_true : mp_const_false;
|
2020-02-19 08:17:13 -05:00
|
|
|
}
|
2015-03-17 19:25:04 -04:00
|
|
|
#endif
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_type(rhs_in, &mp_type_dict)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
|
2014-04-06 14:20:52 -04:00
|
|
|
if (o->map.used != rhs->map.used) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:15:04 -05:00
|
|
|
size_t cur = 0;
|
2014-09-17 10:53:03 -04:00
|
|
|
mp_map_elem_t *next = NULL;
|
|
|
|
while ((next = dict_iter_next(o, &cur)) != NULL) {
|
|
|
|
mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
|
|
|
|
if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
|
|
|
|
return mp_const_false;
|
2014-04-06 14:20:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_true;
|
2014-04-11 06:52:06 -04:00
|
|
|
} else {
|
|
|
|
// dict is not equal to instance of any other type
|
|
|
|
return mp_const_false;
|
2014-04-06 14:20:52 -04:00
|
|
|
}
|
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
default:
|
|
|
|
// op not supported
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL;
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:14:12 -04:00
|
|
|
// Note: Make sure this is inlined in load part of dict_subscr() below.
|
2014-06-05 13:02:15 -04:00
|
|
|
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-05 13:02:15 -04:00
|
|
|
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
|
|
|
|
if (elem == NULL) {
|
2016-10-16 21:00:19 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
|
2014-06-05 13:02:15 -04:00
|
|
|
} else {
|
|
|
|
return elem->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 17:10:53 -04:00
|
|
|
STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
2014-04-08 16:32:29 -04:00
|
|
|
if (value == MP_OBJ_NULL) {
|
2014-04-17 17:10:53 -04:00
|
|
|
// delete
|
2014-04-08 16:32:29 -04:00
|
|
|
mp_obj_dict_delete(self_in, index);
|
2014-04-17 17:10:53 -04:00
|
|
|
return mp_const_none;
|
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
|
|
|
// load
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-17 17:10:53 -04:00
|
|
|
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
|
|
|
|
if (elem == NULL) {
|
2016-10-16 21:00:19 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
|
2014-04-17 17:10:53 -04:00
|
|
|
} else {
|
|
|
|
return elem->value;
|
|
|
|
}
|
2014-04-08 16:32:29 -04:00
|
|
|
} else {
|
2014-04-17 17:10:53 -04:00
|
|
|
// store
|
2014-04-08 16:32:29 -04:00
|
|
|
mp_obj_dict_store(self_in, index, value);
|
2014-04-17 17:10:53 -04:00
|
|
|
return mp_const_none;
|
2014-04-08 16:32:29 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 12:17:02 -05:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* dict methods */
|
|
|
|
|
2018-02-18 22:51:04 -05:00
|
|
|
STATIC void mp_ensure_not_fixed(const mp_obj_dict_t *dict) {
|
|
|
|
if (dict->map.is_fixed) {
|
|
|
|
mp_raise_TypeError(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(self_in));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-02-18 22:51:04 -05:00
|
|
|
mp_ensure_not_fixed(self);
|
2014-01-06 12:38:47 -05:00
|
|
|
|
|
|
|
mp_map_clear(&self->map);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
|
2014-01-06 12:54:04 -05:00
|
|
|
|
2020-06-09 20:45:24 -04:00
|
|
|
mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(self_in));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-03-26 06:28:31 -04:00
|
|
|
mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *other = MP_OBJ_TO_PTR(other_out);
|
2015-03-17 19:25:04 -04:00
|
|
|
other->base.type = self->base.type;
|
2014-01-06 13:11:20 -05:00
|
|
|
other->map.used = self->map.used;
|
2014-04-06 14:00:58 -04:00
|
|
|
other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
|
2015-03-17 19:25:04 -04:00
|
|
|
other->map.is_fixed = 0;
|
|
|
|
other->map.is_ordered = self->map.is_ordered;
|
2014-01-06 13:11:20 -05:00
|
|
|
memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
|
2015-03-26 06:28:31 -04:00
|
|
|
return other_out;
|
2014-01-06 13:11:20 -05:00
|
|
|
}
|
2020-06-09 20:45:24 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy);
|
2014-01-06 12:38:47 -05:00
|
|
|
|
2018-08-05 18:25:41 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_DICT_FROMKEYS
|
2014-01-11 14:22:29 -05:00
|
|
|
// this is a classmethod
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iter = mp_getiter(args[1], NULL);
|
2014-01-11 14:22:29 -05:00
|
|
|
mp_obj_t value = mp_const_none;
|
2015-11-20 09:09:20 -05:00
|
|
|
mp_obj_t next = MP_OBJ_NULL;
|
2014-01-11 14:22:29 -05:00
|
|
|
|
|
|
|
if (n_args > 2) {
|
|
|
|
value = args[2];
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:58:57 -04:00
|
|
|
// optimisation to allocate result based on len of argument
|
|
|
|
mp_obj_t self_out;
|
|
|
|
mp_obj_t len = mp_obj_len_maybe(args[1]);
|
2014-01-11 14:22:29 -05:00
|
|
|
if (len == MP_OBJ_NULL) {
|
|
|
|
/* object's type doesn't have a __len__ slot */
|
2015-03-26 06:28:31 -04:00
|
|
|
self_out = mp_obj_new_dict(0);
|
2014-01-11 14:22:29 -05:00
|
|
|
} else {
|
2015-03-26 06:28:31 -04:00
|
|
|
self_out = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
|
2014-01-11 14:22:29 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_out);
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-11 14:22:29 -05:00
|
|
|
mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:28:31 -04:00
|
|
|
return self_out;
|
2014-01-11 14:22:29 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, MP_ROM_PTR(&dict_fromkeys_fun_obj));
|
2018-08-05 18:25:41 -04:00
|
|
|
#endif
|
2014-01-11 14:22:29 -05:00
|
|
|
|
2017-07-04 09:24:59 -04:00
|
|
|
STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_lookup_kind_t lookup_kind) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(args[0]));
|
2017-07-04 09:24:59 -04:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
|
2018-02-18 22:51:04 -05:00
|
|
|
if (lookup_kind != MP_MAP_LOOKUP) {
|
|
|
|
mp_ensure_not_fixed(self);
|
|
|
|
}
|
2017-07-04 09:24:59 -04:00
|
|
|
mp_map_elem_t *elem = mp_map_lookup(&self->map, args[1], lookup_kind);
|
2014-01-06 15:25:51 -05:00
|
|
|
mp_obj_t value;
|
2014-09-17 10:53:03 -04:00
|
|
|
if (elem == NULL || elem->value == MP_OBJ_NULL) {
|
2017-07-04 09:24:59 -04:00
|
|
|
if (n_args == 2) {
|
2014-01-08 12:33:12 -05:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
|
2017-07-04 09:24:59 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, args[1]));
|
2014-01-06 14:48:34 -05:00
|
|
|
} else {
|
2014-01-06 15:25:51 -05:00
|
|
|
value = mp_const_none;
|
2014-01-06 14:48:34 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-04 09:24:59 -04:00
|
|
|
value = args[2];
|
2014-01-06 14:48:34 -05:00
|
|
|
}
|
2014-09-17 10:53:03 -04:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
|
|
|
|
elem->value = value;
|
|
|
|
}
|
2014-01-06 14:48:34 -05:00
|
|
|
} else {
|
2014-01-06 15:25:51 -05:00
|
|
|
value = elem->value;
|
2014-09-17 10:53:03 -04:00
|
|
|
if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
|
|
|
|
elem->value = MP_OBJ_NULL; // so that GC can collect the deleted value
|
|
|
|
}
|
2014-01-06 15:25:51 -05:00
|
|
|
}
|
|
|
|
return value;
|
2014-01-06 14:48:34 -05:00
|
|
|
}
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t dict_get(size_t n_args, const mp_obj_t *args) {
|
2017-07-04 09:24:59 -04:00
|
|
|
return dict_get_helper(n_args, args, MP_MAP_LOOKUP);
|
2014-01-06 13:53:16 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
|
2014-01-06 13:53:16 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t dict_pop(size_t n_args, const mp_obj_t *args) {
|
2017-07-04 09:24:59 -04:00
|
|
|
return dict_get_helper(n_args, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
2014-01-06 14:48:34 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
|
2014-01-06 14:48:34 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t dict_setdefault(size_t n_args, const mp_obj_t *args) {
|
2017-07-04 09:24:59 -04:00
|
|
|
return dict_get_helper(n_args, args, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
2014-01-06 15:25:51 -05:00
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
|
2014-01-06 15:25:51 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(self_in));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-02-18 22:51:04 -05:00
|
|
|
mp_ensure_not_fixed(self);
|
2020-04-22 11:10:30 -04:00
|
|
|
if (self->map.used == 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_KeyError, MP_ERROR_TEXT("popitem(): dictionary is empty"));
|
2014-01-06 15:08:52 -05:00
|
|
|
}
|
2020-04-22 11:10:30 -04:00
|
|
|
size_t cur = 0;
|
|
|
|
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
|
|
|
|
if (self->map.is_ordered) {
|
|
|
|
cur = self->map.used - 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mp_map_elem_t *next = dict_iter_next(self, &cur);
|
|
|
|
assert(next);
|
2014-01-06 15:08:52 -05:00
|
|
|
self->map.used--;
|
|
|
|
mp_obj_t items[] = {next->key, next->value};
|
2014-09-17 10:53:03 -04:00
|
|
|
next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
|
|
|
|
next->value = MP_OBJ_NULL;
|
2014-01-06 15:08:52 -05:00
|
|
|
mp_obj_t tuple = mp_obj_new_tuple(2, items);
|
|
|
|
|
|
|
|
return tuple;
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
|
2014-01-06 15:08:52 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(args[0]));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
|
2018-02-18 22:51:04 -05:00
|
|
|
mp_ensure_not_fixed(self);
|
2014-06-03 07:53:44 -04:00
|
|
|
|
|
|
|
mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
|
|
|
|
|
|
|
|
if (n_args == 2) {
|
|
|
|
// given a positional argument
|
|
|
|
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_dict_type(args[1])) {
|
2014-06-03 07:53:44 -04:00
|
|
|
// update from other dictionary (make sure other is not self)
|
2015-03-26 06:28:31 -04:00
|
|
|
if (args[1] != args[0]) {
|
2017-02-16 00:15:04 -05:00
|
|
|
size_t cur = 0;
|
2014-06-03 07:53:44 -04:00
|
|
|
mp_map_elem_t *elem = NULL;
|
2020-02-26 23:36:53 -05:00
|
|
|
while ((elem = dict_iter_next((mp_obj_dict_t *)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
|
2014-06-03 07:53:44 -04:00
|
|
|
mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 17:58:17 -05:00
|
|
|
} else {
|
2014-06-03 07:53:44 -04:00
|
|
|
// update from a generic iterable of pairs
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t iter = mp_getiter(args[1], NULL);
|
2015-11-20 09:09:20 -05:00
|
|
|
mp_obj_t next = MP_OBJ_NULL;
|
2014-06-03 07:53:44 -04:00
|
|
|
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
|
2017-02-12 23:44:31 -05:00
|
|
|
mp_obj_t inneriter = mp_getiter(next, NULL);
|
2014-06-03 07:53:44 -04:00
|
|
|
mp_obj_t key = mp_iternext(inneriter);
|
|
|
|
mp_obj_t value = mp_iternext(inneriter);
|
|
|
|
mp_obj_t stop = mp_iternext(inneriter);
|
|
|
|
if (key == MP_OBJ_STOP_ITERATION
|
|
|
|
|| value == MP_OBJ_STOP_ITERATION
|
|
|
|
|| stop != MP_OBJ_STOP_ITERATION) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("dict update sequence has wrong length"));
|
2014-06-03 07:53:44 -04:00
|
|
|
} else {
|
|
|
|
mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the dict with any keyword args
|
2017-02-16 00:15:04 -05:00
|
|
|
for (size_t i = 0; i < kwargs->alloc; i++) {
|
2019-01-30 05:57:29 -05:00
|
|
|
if (mp_map_slot_is_filled(kwargs, i)) {
|
2014-06-03 07:53:44 -04:00
|
|
|
mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
|
2014-01-06 17:58:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-06-03 07:53:44 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
|
2014-01-06 17:58:17 -05:00
|
|
|
|
2014-01-06 15:08:52 -05:00
|
|
|
|
2014-01-09 20:00:20 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* dict views */
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC const mp_obj_type_t dict_view_type;
|
|
|
|
STATIC const mp_obj_type_t dict_view_it_type;
|
2014-01-09 20:00:20 -05:00
|
|
|
|
|
|
|
typedef enum _mp_dict_view_kind_t {
|
|
|
|
MP_DICT_VIEW_ITEMS,
|
|
|
|
MP_DICT_VIEW_KEYS,
|
|
|
|
MP_DICT_VIEW_VALUES,
|
|
|
|
} mp_dict_view_kind_t;
|
|
|
|
|
2016-05-20 07:38:15 -04:00
|
|
|
STATIC const char *const mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
|
2014-01-09 20:00:20 -05:00
|
|
|
|
|
|
|
typedef struct _mp_obj_dict_view_it_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_dict_view_kind_t kind;
|
2015-03-26 06:28:31 -04:00
|
|
|
mp_obj_t dict;
|
2017-02-16 00:15:04 -05:00
|
|
|
size_t cur;
|
2014-01-09 20:00:20 -05:00
|
|
|
} mp_obj_dict_view_it_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_dict_view_t {
|
|
|
|
mp_obj_base_t base;
|
2015-03-26 06:28:31 -04:00
|
|
|
mp_obj_t dict;
|
2014-01-09 20:00:20 -05:00
|
|
|
mp_dict_view_kind_t kind;
|
|
|
|
} mp_obj_dict_view_t;
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_type(self_in, &dict_view_it_type));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_view_it_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
|
2014-01-09 20:00:20 -05:00
|
|
|
|
2014-09-17 10:53:03 -04:00
|
|
|
if (next == NULL) {
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
|
|
|
} else {
|
2014-01-09 20:00:20 -05:00
|
|
|
switch (self->kind) {
|
2014-01-11 14:22:29 -05:00
|
|
|
case MP_DICT_VIEW_ITEMS:
|
2015-01-14 16:32:42 -05:00
|
|
|
default: {
|
2014-01-11 14:22:29 -05:00
|
|
|
mp_obj_t items[] = {next->key, next->value};
|
|
|
|
return mp_obj_new_tuple(2, items);
|
|
|
|
}
|
|
|
|
case MP_DICT_VIEW_KEYS:
|
|
|
|
return next->key;
|
|
|
|
case MP_DICT_VIEW_VALUES:
|
|
|
|
return next->value;
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC const mp_obj_type_t dict_view_it_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_iterator,
|
2016-01-09 18:14:54 -05:00
|
|
|
.getiter = mp_identity_getiter,
|
2014-01-09 20:00:20 -05:00
|
|
|
.iternext = dict_view_it_iternext,
|
|
|
|
};
|
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t));
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_type(view_in, &dict_view_type));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in);
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf;
|
2014-01-09 20:00:20 -05:00
|
|
|
o->base.type = &dict_view_it_type;
|
|
|
|
o->kind = view->kind;
|
2014-09-17 10:53:03 -04:00
|
|
|
o->dict = view->dict;
|
|
|
|
o->cur = 0;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_type(self_in, &dict_view_type));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_view_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-09 20:00:20 -05:00
|
|
|
bool first = true;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, mp_dict_view_names[self->kind]);
|
|
|
|
mp_print_str(print, "([");
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_iter_buf_t iter_buf;
|
|
|
|
mp_obj_t self_iter = dict_view_getiter(self_in, &iter_buf);
|
2015-03-26 06:28:31 -04:00
|
|
|
mp_obj_t next = MP_OBJ_NULL;
|
2014-04-17 18:19:36 -04:00
|
|
|
while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-09 20:00:20 -05:00
|
|
|
if (!first) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, ", ");
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
|
|
|
first = false;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_obj_print_helper(print, next, PRINT_REPR);
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "])");
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2014-05-21 14:42:43 -04:00
|
|
|
// only supported for the 'keys' kind until sets and dicts are refactored
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
|
2014-05-10 08:55:11 -04:00
|
|
|
if (o->kind != MP_DICT_VIEW_KEYS) {
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-05-10 08:55:11 -04:00
|
|
|
}
|
2017-11-23 21:04:24 -05:00
|
|
|
if (op != MP_BINARY_OP_CONTAINS) {
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-05-10 08:55:11 -04:00
|
|
|
}
|
2014-01-11 07:39:33 -05:00
|
|
|
return dict_binary_op(op, o->dict, rhs_in);
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC const mp_obj_type_t dict_view_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_dict_view,
|
2014-01-09 20:00:20 -05:00
|
|
|
.print = dict_view_print,
|
2014-01-11 07:39:33 -05:00
|
|
|
.binary_op = dict_view_binary_op,
|
2014-01-09 20:00:20 -05:00
|
|
|
.getiter = dict_view_getiter,
|
|
|
|
};
|
|
|
|
|
2015-03-26 06:28:31 -04:00
|
|
|
STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
|
2014-01-09 20:00:20 -05:00
|
|
|
o->base.type = &dict_view_type;
|
|
|
|
o->dict = dict;
|
|
|
|
o->kind = kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(self_in));
|
2015-03-26 06:28:31 -04:00
|
|
|
return mp_obj_new_dict_view(self_in, kind);
|
2014-01-09 20:00:20 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_items(mp_obj_t self_in) {
|
2014-01-09 20:00:20 -05:00
|
|
|
return dict_view(self_in, MP_DICT_VIEW_ITEMS);
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
|
2014-01-09 20:00:20 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
|
2014-01-09 20:00:20 -05:00
|
|
|
return dict_view(self_in, MP_DICT_VIEW_KEYS);
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
|
2014-01-09 20:00:20 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t dict_values(mp_obj_t self_in) {
|
2014-01-09 20:00:20 -05:00
|
|
|
return dict_view(self_in, MP_DICT_VIEW_VALUES);
|
|
|
|
}
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
|
2014-01-09 20:00:20 -05:00
|
|
|
|
2017-11-27 07:40:31 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* dict iterator */
|
|
|
|
|
|
|
|
STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t));
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(self_in));
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t *)iter_buf;
|
2017-11-27 07:40:31 -05:00
|
|
|
o->base.type = &dict_view_it_type;
|
|
|
|
o->kind = MP_DICT_VIEW_KEYS;
|
|
|
|
o->dict = self_in;
|
|
|
|
o->cur = 0;
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
2014-01-06 12:17:02 -05:00
|
|
|
/******************************************************************************/
|
2014-01-11 14:22:29 -05:00
|
|
|
/* dict constructors & public C API */
|
2014-01-06 12:17:02 -05:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t dict_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&dict_clear_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&dict_copy_obj) },
|
2018-08-05 18:25:41 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_DICT_FROMKEYS
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_fromkeys), MP_ROM_PTR(&dict_fromkeys_obj) },
|
2018-08-05 18:25:41 -04:00
|
|
|
#endif
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&dict_get_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&dict_items_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&dict_keys_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&dict_pop_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_popitem), MP_ROM_PTR(&dict_popitem_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setdefault), MP_ROM_PTR(&dict_setdefault_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&dict_update_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&dict_values_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___getitem__), MP_ROM_PTR(&mp_op_getitem_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___setitem__), MP_ROM_PTR(&mp_op_setitem_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___delitem__), MP_ROM_PTR(&mp_op_delitem_obj) },
|
2014-01-07 18:18:25 -05:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_dict = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_dict,
|
2014-01-05 15:34:09 -05:00
|
|
|
.print = dict_print,
|
|
|
|
.make_new = dict_make_new,
|
2014-01-27 18:15:32 -05:00
|
|
|
.unary_op = dict_unary_op,
|
2014-01-05 15:34:09 -05:00
|
|
|
.binary_op = dict_binary_op,
|
2014-04-17 17:10:53 -04:00
|
|
|
.subscr = dict_subscr,
|
2014-01-06 12:17:02 -05:00
|
|
|
.getiter = dict_getiter,
|
2020-02-26 23:36:53 -05:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&dict_locals_dict,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2015-03-17 19:25:04 -04:00
|
|
|
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
|
|
|
|
const mp_obj_type_t mp_type_ordereddict = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_OrderedDict,
|
|
|
|
.print = dict_print,
|
|
|
|
.make_new = dict_make_new,
|
|
|
|
.unary_op = dict_unary_op,
|
|
|
|
.binary_op = dict_binary_op,
|
|
|
|
.subscr = dict_subscr,
|
|
|
|
.getiter = dict_getiter,
|
2017-04-05 22:09:01 -04:00
|
|
|
.parent = &mp_type_dict,
|
2020-02-26 23:36:53 -05:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&dict_locals_dict,
|
2015-03-17 19:25:04 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-02-16 00:15:04 -05:00
|
|
|
void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args) {
|
2014-04-05 16:53:54 -04:00
|
|
|
dict->base.type = &mp_type_dict;
|
|
|
|
mp_map_init(&dict->map, n_args);
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:15:04 -05:00
|
|
|
mp_obj_t mp_obj_new_dict(size_t n_args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
|
2014-04-05 16:53:54 -04:00
|
|
|
mp_obj_dict_init(o, n_args);
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
2013-12-29 17:32:51 -05:00
|
|
|
|
2017-02-16 00:15:04 -05:00
|
|
|
size_t mp_obj_dict_len(mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-03-26 06:28:31 -04:00
|
|
|
return self->map.used;
|
2013-12-29 17:32:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_check_self(mp_obj_is_dict_type(self_in));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-02-18 22:51:04 -05:00
|
|
|
mp_ensure_not_fixed(self);
|
2014-01-08 12:33:12 -05:00
|
|
|
mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
|
2013-12-29 17:32:51 -05:00
|
|
|
return self_in;
|
|
|
|
}
|
2014-01-09 15:57:50 -05:00
|
|
|
|
2014-04-05 08:25:13 -04:00
|
|
|
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
|
2017-07-04 09:24:59 -04:00
|
|
|
mp_obj_t args[2] = {self_in, key};
|
|
|
|
dict_get_helper(2, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
2014-04-05 08:25:13 -04:00
|
|
|
return self_in;
|
|
|
|
}
|