Skip to content

Commit 0e80f34

Browse files
committed
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.
1 parent 9b9dbc5 commit 0e80f34

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

py/mpconfig.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -765,16 +765,24 @@ typedef double mp_float_t;
765765
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (0)
766766
#endif
767767

768-
// Whether to support complete set of special methods
769-
// for user classes, or only the most used ones. "Reverse"
770-
// methods are controlled by MICROPY_PY_REVERSE_SPECIAL_METHODS
771-
// below.
768+
// Whether to support complete set of special methods for user
769+
// classes, or only the most used ones. "Inplace" methods are
770+
// controlled by MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS below.
771+
// "Reverse" methods are controlled by
772+
// MICROPY_PY_REVERSE_SPECIAL_METHODS below.
772773
#ifndef MICROPY_PY_ALL_SPECIAL_METHODS
773774
#define MICROPY_PY_ALL_SPECIAL_METHODS (0)
774775
#endif
775776

776-
// Whether to support reverse arithmetic operarions methods
777-
// (__radd__, etc.)
777+
// Whether to support all inplace arithmetic operarion methods
778+
// (__imul__, etc.)
779+
#ifndef MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS
780+
#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (0)
781+
#endif
782+
783+
// Whether to support reverse arithmetic operarion methods
784+
// (__radd__, etc.). Additionally gated by
785+
// MICROPY_PY_ALL_SPECIAL_METHODS.
778786
#ifndef MICROPY_PY_REVERSE_SPECIAL_METHODS
779787
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (0)
780788
#endif

py/objtype.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
422422
// MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result
423423
[MP_BINARY_OP_IN] = MP_QSTR___contains__,
424424

425-
#if MICROPY_PY_ALL_SPECIAL_METHODS
426425
// All inplace methods are optional, and normal methods will be used
427426
// as a fallback.
428427
[MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__,
429428
[MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__,
429+
#if MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS
430430
[MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__,
431431
[MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__,
432432
[MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__,

0 commit comments

Comments
 (0)