diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-09-10 17:05:20 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-09-10 17:05:57 +0300 |
commit | eb84a830df62813f5af7f0144fc77444bf18f3a8 (patch) | |
tree | c75036150f3584486bc2064572faa2fe2750ae8c /py/runtime.c | |
parent | de981040b392685b0d7f2381a63526b71e633b9c (diff) |
py/runtime: Implement dispatch for "reverse op" special methods.
If, for class X, X.__add__(Y) doesn't exist (or returns NotImplemented),
try Y.__radd__(X) instead.
This patch could be simpler, but requires undoing operand swap and
operation switch to get non-confusing error message in case __radd__
doesn't exist.
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/py/runtime.c b/py/runtime.c index 21ef42577..c533558da 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -555,7 +555,20 @@ generic_binary_op: } } - // TODO implement dispatch for reverse binary ops +#if MICROPY_PY_REVERSE_SPECIAL_METHODS + if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_REVERSE_POWER) { + mp_obj_t t = rhs; + rhs = lhs; + lhs = t; + if (op <= MP_BINARY_OP_POWER) { + op += MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR; + goto generic_binary_op; + } + + // Convert __rop__ back to __op__ for error message + op -= MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR; + } +#endif unsupported_op: if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |