Skip to content

Commit 9b9dbc5

Browse files
committed
py/objtype: Define all special methods if requested.
If MICROPY_PY_ALL_SPECIAL_METHODS is defined, actually define all special methods (still subject to gating by e.g. MICROPY_PY_REVERSE_SPECIAL_METHODS). This adds quite a number of qstr's, so should be used sparingly.
1 parent b992326 commit 9b9dbc5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

py/objtype.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
342342
[MP_UNARY_OP_POSITIVE] = MP_QSTR___pos__,
343343
[MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
344344
[MP_UNARY_OP_INVERT] = MP_QSTR___invert__,
345+
[MP_UNARY_OP_ABS] = MP_QSTR___abs__,
345346
#endif
346347
#if MICROPY_PY_SYS_GETSIZEOF
347348
[MP_UNARY_OP_SIZEOF] = MP_QSTR___sizeof__,
@@ -422,8 +423,20 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
422423
[MP_BINARY_OP_IN] = MP_QSTR___contains__,
423424

424425
#if MICROPY_PY_ALL_SPECIAL_METHODS
426+
// All inplace methods are optional, and normal methods will be used
427+
// as a fallback.
425428
[MP_BINARY_OP_INPLACE_ADD] = MP_QSTR___iadd__,
426429
[MP_BINARY_OP_INPLACE_SUBTRACT] = MP_QSTR___isub__,
430+
[MP_BINARY_OP_INPLACE_MULTIPLY] = MP_QSTR___imul__,
431+
[MP_BINARY_OP_INPLACE_FLOOR_DIVIDE] = MP_QSTR___ifloordiv__,
432+
[MP_BINARY_OP_INPLACE_TRUE_DIVIDE] = MP_QSTR___itruediv__,
433+
[MP_BINARY_OP_INPLACE_MODULO] = MP_QSTR___imod__,
434+
[MP_BINARY_OP_INPLACE_POWER] = MP_QSTR___ipow__,
435+
[MP_BINARY_OP_INPLACE_OR] = MP_QSTR___ior__,
436+
[MP_BINARY_OP_INPLACE_XOR] = MP_QSTR___ixor__,
437+
[MP_BINARY_OP_INPLACE_AND] = MP_QSTR___iand__,
438+
[MP_BINARY_OP_INPLACE_LSHIFT] = MP_QSTR___ilshift__,
439+
[MP_BINARY_OP_INPLACE_RSHIFT] = MP_QSTR___irshift__,
427440
#endif
428441

429442
[MP_BINARY_OP_ADD] = MP_QSTR___add__,
@@ -432,12 +445,31 @@ const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
432445
[MP_BINARY_OP_MULTIPLY] = MP_QSTR___mul__,
433446
[MP_BINARY_OP_FLOOR_DIVIDE] = MP_QSTR___floordiv__,
434447
[MP_BINARY_OP_TRUE_DIVIDE] = MP_QSTR___truediv__,
448+
[MP_BINARY_OP_MODULO] = MP_QSTR___mod__,
449+
[MP_BINARY_OP_DIVMOD] = MP_QSTR___divmod__,
450+
[MP_BINARY_OP_POWER] = MP_QSTR___pow__,
451+
[MP_BINARY_OP_OR] = MP_QSTR___or__,
452+
[MP_BINARY_OP_XOR] = MP_QSTR___xor__,
453+
[MP_BINARY_OP_AND] = MP_QSTR___and__,
454+
[MP_BINARY_OP_LSHIFT] = MP_QSTR___lshift__,
455+
[MP_BINARY_OP_RSHIFT] = MP_QSTR___rshift__,
435456
#endif
436457

437458
#if MICROPY_PY_REVERSE_SPECIAL_METHODS
438459
[MP_BINARY_OP_REVERSE_ADD] = MP_QSTR___radd__,
439460
[MP_BINARY_OP_REVERSE_SUBTRACT] = MP_QSTR___rsub__,
461+
#if MICROPY_PY_ALL_SPECIAL_METHODS
440462
[MP_BINARY_OP_REVERSE_MULTIPLY] = MP_QSTR___rmul__,
463+
[MP_BINARY_OP_REVERSE_FLOOR_DIVIDE] = MP_QSTR___rfloordiv__,
464+
[MP_BINARY_OP_REVERSE_TRUE_DIVIDE] = MP_QSTR___rtruediv__,
465+
[MP_BINARY_OP_REVERSE_MODULO] = MP_QSTR___rmod__,
466+
[MP_BINARY_OP_REVERSE_POWER] = MP_QSTR___rpow__,
467+
[MP_BINARY_OP_REVERSE_OR] = MP_QSTR___ror__,
468+
[MP_BINARY_OP_REVERSE_XOR] = MP_QSTR___rxor__,
469+
[MP_BINARY_OP_REVERSE_AND] = MP_QSTR___rand__,
470+
[MP_BINARY_OP_REVERSE_LSHIFT] = MP_QSTR___rlshift__,
471+
[MP_BINARY_OP_REVERSE_RSHIFT] = MP_QSTR___rrshift__,
472+
#endif
441473
#endif
442474
};
443475

tests/unix/extra_coverage.py.exp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ ementation
3939
0
4040
0
4141
# runtime utils
42-
TypeError: unsupported type for : 'str'
43-
TypeError: unsupported types for : 'str', 'str'
42+
TypeError: unsupported type for __abs__: 'str'
43+
TypeError: unsupported types for __divmod__: 'str', 'str'
4444
Warning: test
4545
# format float
4646
?

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy