← index #18626PR #11478
Related · high · value 0.161
QUERY · ISSUE

Missing MP_BINARY_OP_REVERSE_LESS etc.

openby arnonsenopened 2026-01-01updated 2026-01-02
enhancementpy-core

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:

  1. 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,

  2. 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

CANDIDATE · PULL REQUEST

py: Improve support for inplace operators

mergedby dpgeorgeopened 2023-05-12updated 2023-05-19
py-core

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:

  1. 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
  2. make it so str objects don't prematurely raise TypeError if the arguments to a binary operation (eg +) are not both str
  3. the code that handles inplace -> normal binary operator fallback, move it from py/objtype.c to py/runtime.c, making it apply to all types, not just user classes

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied