Skip to content

Commit 36f7952

Browse files
committed
py/objtype: Clean up unary- and binary-op enum-to-qstr mapping tables.
1 parent 0864a69 commit 36f7952

1 file changed

Lines changed: 18 additions & 42 deletions

File tree

py/objtype.c

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size
332332
return MP_OBJ_FROM_PTR(o);
333333
}
334334

335-
const uint16_t mp_unary_op_method_name[] = {
335+
const uint16_t mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
336336
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
337337
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
338338
[MP_UNARY_OP_HASH] = MP_QSTR___hash__,
@@ -344,7 +344,6 @@ const uint16_t mp_unary_op_method_name[] = {
344344
#if MICROPY_PY_SYS_GETSIZEOF
345345
[MP_UNARY_OP_SIZEOF] = MP_QSTR_getsizeof,
346346
#endif
347-
[MP_UNARY_OP_NOT] = MP_QSTR_, // don't need to implement this, used to make sure array has full size
348347
};
349348

350349
STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
@@ -406,59 +405,36 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
406405
}
407406
}
408407

409-
const uint16_t mp_binary_op_method_name[] = {
410-
/*
411-
MP_BINARY_OP_OR,
412-
MP_BINARY_OP_XOR,
413-
MP_BINARY_OP_AND,
414-
MP_BINARY_OP_LSHIFT,
415-
MP_BINARY_OP_RSHIFT,
416-
*/
408+
// Binary-op enum values not listed here will have the default value of 0 in the
409+
// table, corresponding to MP_QSTR_, and are therefore unsupported (a lookup will
410+
// fail). They can be added at the expense of code size for the qstr.
411+
const uint16_t mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
412+
[MP_BINARY_OP_LESS] = MP_QSTR___lt__,
413+
[MP_BINARY_OP_MORE] = MP_QSTR___gt__,
414+
[MP_BINARY_OP_EQUAL] = MP_QSTR___eq__,
415+
[MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__,
416+
[MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__,
417+
// MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result
418+
[MP_BINARY_OP_IN] = MP_QSTR___contains__,
419+
420+
#if MICROPY_PY_ALL_SPECIAL_METHODS
421+
[MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__,
422+
[MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__,
423+
#endif
424+
417425
[MP_BINARY_OP_ADD] = MP_QSTR___add__,
418426
[MP_BINARY_OP_SUBTRACT] = MP_QSTR___sub__,
419427
#if MICROPY_PY_ALL_SPECIAL_METHODS
420428
[MP_BINARY_OP_MULTIPLY] = MP_QSTR___mul__,
421429
[MP_BINARY_OP_FLOOR_DIVIDE] = MP_QSTR___floordiv__,
422430
[MP_BINARY_OP_TRUE_DIVIDE] = MP_QSTR___truediv__,
423431
#endif
424-
/*
425-
MP_BINARY_OP_MODULO,
426-
MP_BINARY_OP_POWER,
427-
MP_BINARY_OP_DIVMOD,
428-
MP_BINARY_OP_INPLACE_OR,
429-
MP_BINARY_OP_INPLACE_XOR,
430-
MP_BINARY_OP_INPLACE_AND,
431-
MP_BINARY_OP_INPLACE_LSHIFT,
432-
MP_BINARY_OP_INPLACE_RSHIFT,*/
433-
#if MICROPY_PY_ALL_SPECIAL_METHODS
434-
[MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__,
435-
[MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__,
436-
#endif
437-
/*MP_BINARY_OP_INPLACE_MULTIPLY,
438-
MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
439-
MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
440-
MP_BINARY_OP_INPLACE_MODULO,
441-
MP_BINARY_OP_INPLACE_POWER,*/
442432

443433
#if MICROPY_PY_REVERSE_SPECIAL_METHODS
444434
[MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__,
445435
[MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__,
446436
[MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__,
447437
#endif
448-
449-
[MP_BINARY_OP_LESS] = MP_QSTR___lt__,
450-
[MP_BINARY_OP_MORE] = MP_QSTR___gt__,
451-
[MP_BINARY_OP_EQUAL] = MP_QSTR___eq__,
452-
[MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__,
453-
[MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__,
454-
/*
455-
MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result
456-
*/
457-
[MP_BINARY_OP_IN] = MP_QSTR___contains__,
458-
/*
459-
MP_BINARY_OP_IS,
460-
*/
461-
[MP_BINARY_OP_LAST] = 0, // used to make sure array has full size, TODO: FIXME
462438
};
463439

464440
STATIC mp_obj_t instance_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {

0 commit comments

Comments
 (0)