diff options
Diffstat (limited to 'py/objdict.c')
-rw-r--r-- | py/objdict.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/py/objdict.c b/py/objdict.c index 6dbb1f316b..da1b5b9f5b 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -20,7 +20,7 @@ typedef struct _mp_obj_dict_t { static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur); static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in); -static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { +static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { mp_obj_dict_t *self = self_in; bool first = true; print(env, "{"); @@ -31,9 +31,9 @@ static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env print(env, ", "); } first = false; - mp_obj_print_helper(print, env, next->key); + mp_obj_print_helper(print, env, next->key, PRINT_REPR); print(env, ": "); - mp_obj_print_helper(print, env, next->value); + mp_obj_print_helper(print, env, next->value, PRINT_REPR); } print(env, "}"); } @@ -57,6 +57,12 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { return elem->value; } } + case RT_COMPARE_OP_IN: + case RT_COMPARE_OP_NOT_IN: + { + mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP); + return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (elem == NULL)); + } default: // op not supported return NULL; @@ -344,7 +350,7 @@ static mp_obj_t dict_view_getiter(mp_obj_t view_in) { return o; } -static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { +static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type)); mp_obj_dict_view_t *self = self_in; bool first = true; @@ -357,15 +363,25 @@ static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void print(env, ", "); } first = false; - mp_obj_print_helper(print, env, next); + mp_obj_print_helper(print, env, next, PRINT_REPR); } print(env, "])"); } +static mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { + /* only supported for the 'keys' kind until sets and dicts are refactored */ + mp_obj_dict_view_t *o = lhs_in; + if (o->kind != MP_DICT_VIEW_KEYS) return NULL; + if (op != RT_COMPARE_OP_IN && op != RT_COMPARE_OP_NOT_IN) return NULL; + return dict_binary_op(op, o->dict, rhs_in); +} + + static const mp_obj_type_t dict_view_type = { { &mp_const_type }, "dict_view", .print = dict_view_print, + .binary_op = dict_view_binary_op, .getiter = dict_view_getiter, }; |