py: Specify unary/binary op name in TypeError error message.
Eg, "() + 1" now tells you that __add__ is not supported for tuple and
int types (before it just said the generic "binary operator"). We reuse
the table of names for slot lookup because it would be a waste of code
space to store the pretty name for each operator.
diff --git a/py/runtime.c b/py/runtime.c
index 3597f56..fc8d128 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -242,10 +242,9 @@
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"unsupported type for operator"));
} else {
- // TODO specify in error message what the operator is
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "bad operand type for unary operator: '%s'",
- mp_obj_get_type_str(arg)));
+ "unsupported type for %s: '%s'",
+ qstr_str(mp_unary_op_method_name[op]), mp_obj_get_type_str(arg)));
}
}
}
@@ -537,10 +536,9 @@
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"unsupported type for operator"));
} else {
- // TODO specify in error message what the operator is
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "unsupported operand types for binary operator: '%s', '%s'",
- mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
+ "unsupported types for %s: '%s', '%s'",
+ qstr_str(mp_binary_op_method_name[op]), mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
}
zero_division: