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.
This commit is contained in:
parent
a9dc9b8f6d
commit
a5efcd4745
|
@ -323,7 +323,7 @@ mp_obj_t instance_make_new(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, c
|
|||
return o;
|
||||
}
|
||||
|
||||
STATIC const qstr unary_op_method_name[] = {
|
||||
const qstr mp_unary_op_method_name[] = {
|
||||
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
|
||||
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
|
||||
//[MP_UNARY_OP_POSITIVE,
|
||||
|
@ -334,7 +334,7 @@ STATIC const qstr unary_op_method_name[] = {
|
|||
|
||||
STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) {
|
||||
mp_obj_instance_t *self = self_in;
|
||||
qstr op_name = unary_op_method_name[op];
|
||||
qstr op_name = mp_unary_op_method_name[op];
|
||||
/* Still try to lookup native slot
|
||||
if (op_name == 0) {
|
||||
return MP_OBJ_NULL;
|
||||
|
|
10
py/runtime.c
10
py/runtime.c
|
@ -242,10 +242,9 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
|
|||
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 @@ unsupported_op:
|
|||
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:
|
||||
|
|
|
@ -56,6 +56,10 @@ typedef struct _mp_arg_t {
|
|||
mp_arg_val_t defval;
|
||||
} mp_arg_t;
|
||||
|
||||
// defined in objtype.c
|
||||
extern const qstr mp_unary_op_method_name[];
|
||||
extern const qstr mp_binary_op_method_name[];
|
||||
|
||||
void mp_init(void);
|
||||
void mp_deinit(void);
|
||||
|
||||
|
|
|
@ -30,8 +30,6 @@
|
|||
#include "py/bc0.h"
|
||||
#include "py/bc.h"
|
||||
|
||||
extern const qstr mp_binary_op_method_name[];
|
||||
|
||||
#if MICROPY_DEBUG_PRINTERS
|
||||
|
||||
#define DECODE_UINT { \
|
||||
|
|
Loading…
Reference in New Issue