summaryrefslogtreecommitdiff
path: root/py/objdict.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-11-19 14:41:31 +1100
committerDamien George <damien@micropython.org>2025-11-21 13:28:28 +1100
commit128420359e9144369df2ed24e99217b3dc54446f (patch)
tree5cbdf78662a9d018a1da0a14657a061d8c1c335b /py/objdict.c
parent45938432c6616fe684e67fdf4c5a9615a7c2fc73 (diff)
py/objdict: Implement bool and len unary ops for dict views.
Currently, dict views (eg `dict.keys()`, `dict.values()`) do not implement the `bool` or `len` unary operations. That may seem like a reasonable omission for MicroPython to keep code size down, but it actually leads to silently incorrect bool operations, because by default things are true. Eg we currently have: >>> bool(dict().keys()) True which is wrong, it should be `False` because the dict is empty. This commit implements `bool` and `len` unary operations on dict views by simply delegating to the existing dict unary op function. Fixes issue #12385. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/objdict.c')
-rw-r--r--py/objdict.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/py/objdict.c b/py/objdict.c
index 451e87329..692a7de42 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -521,7 +521,8 @@ static mp_obj_t dict_view_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
if (op == MP_UNARY_OP_HASH && o->kind == MP_DICT_VIEW_VALUES) {
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
}
- return MP_OBJ_NULL;
+ // delegate all other ops to dict unary op handler
+ return dict_unary_op(op, o->dict);
}
static mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {