Commit a5efcd47 authored by Damien George's avatar Damien George
Browse files

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.
parent a9dc9b8f
...@@ -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 ...@@ -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; 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_BOOL] = MP_QSTR___bool__,
[MP_UNARY_OP_LEN] = MP_QSTR___len__, [MP_UNARY_OP_LEN] = MP_QSTR___len__,
//[MP_UNARY_OP_POSITIVE, //[MP_UNARY_OP_POSITIVE,
...@@ -334,7 +334,7 @@ STATIC const qstr unary_op_method_name[] = { ...@@ -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) { STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) {
mp_obj_instance_t *self = 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 /* Still try to lookup native slot
if (op_name == 0) { if (op_name == 0) {
return MP_OBJ_NULL; return MP_OBJ_NULL;
......
...@@ -242,10 +242,9 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) { ...@@ -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, nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"unsupported type for operator")); "unsupported type for operator"));
} else { } else {
// TODO specify in error message what the operator is
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"bad operand type for unary operator: '%s'", "unsupported type for %s: '%s'",
mp_obj_get_type_str(arg))); qstr_str(mp_unary_op_method_name[op]), mp_obj_get_type_str(arg)));
} }
} }
} }
...@@ -537,10 +536,9 @@ unsupported_op: ...@@ -537,10 +536,9 @@ unsupported_op:
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"unsupported type for operator")); "unsupported type for operator"));
} else { } else {
// TODO specify in error message what the operator is
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unsupported operand types for binary operator: '%s', '%s'", "unsupported types for %s: '%s', '%s'",
mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs))); qstr_str(mp_binary_op_method_name[op]), mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
} }
zero_division: zero_division:
......
...@@ -56,6 +56,10 @@ typedef struct _mp_arg_t { ...@@ -56,6 +56,10 @@ typedef struct _mp_arg_t {
mp_arg_val_t defval; mp_arg_val_t defval;
} mp_arg_t; } 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_init(void);
void mp_deinit(void); void mp_deinit(void);
......
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
#include "py/bc0.h" #include "py/bc0.h"
#include "py/bc.h" #include "py/bc.h"
extern const qstr mp_binary_op_method_name[];
#if MICROPY_DEBUG_PRINTERS #if MICROPY_DEBUG_PRINTERS
#define DECODE_UINT { \ #define DECODE_UINT { \
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment