summaryrefslogtreecommitdiff
path: root/py/runtime.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2020-02-20 22:30:49 +1100
committerDamien George <damien@micropython.org>2021-07-23 12:40:00 +1000
commit4e39ff221abff9d1d7d7fc654da67a89a7543112 (patch)
tree6c26ab519d0a9fac8d3f6e7776d65031421ba3fa /py/runtime.c
parent14b853eae052f6d8ef285931336ca1212af76716 (diff)
py/runtime: Fix bool unary op for subclasses of native types.
Previously a subclass of a type that didn't implement unary_op, or didn't handle MP_UNARY_OP_BOOL, would raise TypeError on bool conversion. Fixes #5677.
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 19686c310..27e5bc13e 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -284,6 +284,12 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
return result;
}
}
+ if (op == MP_UNARY_OP_BOOL) {
+ // Type doesn't have unary_op (or didn't handle MP_UNARY_OP_BOOL),
+ // so is implicitly True as this code path is impossible to reach
+ // if arg==mp_const_none.
+ return mp_const_true;
+ }
// With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int().
// In this case provide a more focused error message to not confuse, e.g. chr(1.0)
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE