← index #5376PR #13127
Off-topic · high · value 0.803
QUERY · ISSUE

Empty op name printed when missing from mp_binary_op_method_name

openby Jongyopened 2019-12-04updated 2019-12-04
py-core

With MICROPY_PY_ALL_SPECIAL_METHODS =0,

>>> 1 + 1
2
>>> 1 + 1 / 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported types for  : 'int', 'int'
>>> 

You can see the print is a bit broken and it makes you question whether something deeper has gone wrong. I think it would be better to print something, like "unknown" or "unknown op".

For example, [0 ... MP_BINARY_OP_NUM_RUNTIME-1] = MP_QSTR_unknown, as the first line of mp_binary_op_method_name does the trick. It basically adds just this single "unknown" qstr.

CANDIDATE · PULL REQUEST

makeqstrdata: Put special method names early.

closedby jepleropened 2023-12-04updated 2023-12-05

In circuitpython, these QSTRs are close to falling out of the set of qstrs that fit in a byte, especially as we have added other qstrs that start with underscores causing xor to sort later. By putting them in this list, they're guaranteed to appear early and avoid problems.

I'm not entirely sure of the impact of this; does it force the strings to appear? for instance is __float__ added regardless of MICROPY_PY_BUILTINS_FLOAT? It appears so, which probably makes this approach unsuitable.

Perhaps an even greater savings would be possible if the sorting were such that mp_unary_op_method_name (and ditto binary) could be eliminated: Have MP_UNARY_OP_BASE = MP_QSTR__abs__ and ensure the invariant that e.g., MP_UNARY_OP_BASE + MP_UNARY_OP_NOT == MP_QSTR___not__. However, this probably saves <50 bytes, the tables are quite small.

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