py/objtype: Introduce MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS.

This allows to configure support for inplace special methods separately,
similar to "normal" and reverse special methods. This is useful, because
inplace methods are "the most optional" ones, for example, if inplace
methods aren't defined, the operation will be executed using normal
methods instead.

As a caveat, __iadd__ and __isub__ are implemented even if
MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS isn't defined. This is similar
to the state of affairs before binary operations refactor, and allows
to run existing tests even if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS
isn't defined.
This commit is contained in:
Paul Sokolovsky 2017-10-27 22:29:15 +03:00
parent 9b9dbc5815
commit 0e80f345f8
2 changed files with 15 additions and 7 deletions

View File

@ -765,16 +765,24 @@ typedef double mp_float_t;
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (0) #define MICROPY_PY_BUILTINS_TIMEOUTERROR (0)
#endif #endif
// Whether to support complete set of special methods // Whether to support complete set of special methods for user
// for user classes, or only the most used ones. "Reverse" // classes, or only the most used ones. "Inplace" methods are
// methods are controlled by MICROPY_PY_REVERSE_SPECIAL_METHODS // controlled by MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS below.
// below. // "Reverse" methods are controlled by
// MICROPY_PY_REVERSE_SPECIAL_METHODS below.
#ifndef MICROPY_PY_ALL_SPECIAL_METHODS #ifndef MICROPY_PY_ALL_SPECIAL_METHODS
#define MICROPY_PY_ALL_SPECIAL_METHODS (0) #define MICROPY_PY_ALL_SPECIAL_METHODS (0)
#endif #endif
// Whether to support reverse arithmetic operarions methods // Whether to support all inplace arithmetic operarion methods
// (__radd__, etc.) // (__imul__, etc.)
#ifndef MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS
#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (0)
#endif
// Whether to support reverse arithmetic operarion methods
// (__radd__, etc.). Additionally gated by
// MICROPY_PY_ALL_SPECIAL_METHODS.
#ifndef MICROPY_PY_REVERSE_SPECIAL_METHODS #ifndef MICROPY_PY_REVERSE_SPECIAL_METHODS
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (0) #define MICROPY_PY_REVERSE_SPECIAL_METHODS (0)
#endif #endif

View File

@ -422,11 +422,11 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
// MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result // MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result
[MP_BINARY_OP_IN] = MP_QSTR___contains__, [MP_BINARY_OP_IN] = MP_QSTR___contains__,
#if MICROPY_PY_ALL_SPECIAL_METHODS
// All inplace methods are optional, and normal methods will be used // All inplace methods are optional, and normal methods will be used
// as a fallback. // as a fallback.
[MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__, [MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__,
[MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__, [MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__,
#if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS
[MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__, [MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__,
[MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__, [MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__,
[MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__, [MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__,