Missing MP_BINARY_OP_REVERSE_LESS etc.
Port, board and/or hardware
Windows
MicroPython version
3.4.0; MicroPython 674a124bbe-dirty on 2026-01-01
Reproduction
from ulab import numpy as np
print(3 < np.array([1,2,3,4]))
Expected behaviour
[False False False True]
Observed behaviour
TypeError: unsupported types for lt: 'int', 'ndarray'
Additional Information
Here're the required changes for the fix:
-
Add to 'mp_binary_op_t' on runtime0.h
MP_BINARY_OP_REVERSE_LESS,
MP_BINARY_OP_REVERSE_MORE,
MP_BINARY_OP_REVERSE_EQUAL,
MP_BINARY_OP_REVERSE_LESS_EQUAL,
MP_BINARY_OP_REVERSE_MORE_EQUAL,
MP_BINARY_OP_REVERSE_NOT_EQUAL, -
Add the following code:
if (op >= MP_BINARY_OP_LESS && op <= MP_BINARY_OP_NOT_EQUAL) {
mp_obj_t t = rhs;
rhs = lhs;
lhs = t;
op += MP_BINARY_OP_REVERSE_LESS - MP_BINARY_OP_LESS;
goto generic_binary_op;
} else if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_POWER) {
instead of the line "if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_POWER) {"
After these two changes I get the expected result with no error
Code of Conduct
Yes, I agree
py: Improve support for inplace operators
This improves core support for inplace operators, both on built-in types and user classes.
It's an alternative to #10844 that is more comprehensive. The Python snippets in that PR (in particular reverse inplace division) will now work, with the patches in this PR.
There are three distinct changes/improvements here:
- disallow addition of memoryview objects; this is compatible with CPython, and is needed to keep the existing test suit passing with the other two changes
- make it so str objects don't prematurely raise TypeError if the arguments to a binary operation (eg +) are not both str
- the code that handles inplace -> normal binary operator fallback, move it from
py/objtype.ctopy/runtime.c, making it apply to all types, not just user classes