Skip to content

Commit a5efcd4

Browse files
committed
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.
1 parent a9dc9b8 commit a5efcd4

4 files changed

Lines changed: 10 additions & 10 deletions

File tree

py/objtype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
323323
return o;
324324
}
325325

326-
STATIC const qstr unary_op_method_name[] = {
326+
const qstr mp_unary_op_method_name[] = {
327327
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
328328
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
329329
//[MP_UNARY_OP_POSITIVE,
@@ -334,7 +334,7 @@ STATIC const qstr unary_op_method_name[] = {
334334

335335
STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) {
336336
mp_obj_instance_t *self = self_in;
337-
qstr op_name = unary_op_method_name[op];
337+
qstr op_name = mp_unary_op_method_name[op];
338338
/* Still try to lookup native slot
339339
if (op_name == 0) {
340340
return MP_OBJ_NULL;

py/runtime.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,9 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
242242
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
243243
"unsupported type for operator"));
244244
} else {
245-
// TODO specify in error message what the operator is
246245
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
247-
"bad operand type for unary operator: '%s'",
248-
mp_obj_get_type_str(arg)));
246+
"unsupported type for %s: '%s'",
247+
qstr_str(mp_unary_op_method_name[op]), mp_obj_get_type_str(arg)));
249248
}
250249
}
251250
}
@@ -537,10 +536,9 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
537536
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
538537
"unsupported type for operator"));
539538
} else {
540-
// TODO specify in error message what the operator is
541539
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
542-
"unsupported operand types for binary operator: '%s', '%s'",
543-
mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
540+
"unsupported types for %s: '%s', '%s'",
541+
qstr_str(mp_binary_op_method_name[op]), mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
544542
}
545543

546544
zero_division:

py/runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ typedef struct _mp_arg_t {
5656
mp_arg_val_t defval;
5757
} mp_arg_t;
5858

59+
// defined in objtype.c
60+
extern const qstr mp_unary_op_method_name[];
61+
extern const qstr mp_binary_op_method_name[];
62+
5963
void mp_init(void);
6064
void mp_deinit(void);
6165

py/showbc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#include "py/bc0.h"
3131
#include "py/bc.h"
3232

33-
extern const qstr mp_binary_op_method_name[];
34-
3533
#if MICROPY_DEBUG_PRINTERS
3634

3735
#define DECODE_UINT { \

0 commit comments

Comments
 (0)