← index #9114Issue #11638
Related · medium · value 1.189
QUERY · ISSUE

Viper bug: integers > 0x3fffffff appear as python objects

openby rkompassopened 2022-08-26updated 2023-04-16
bug

The bug presumably is a result of the same logic as the now closed #8956 (perhaps the fix is also as simple).

Integers outside the mpy small int range ( > 0x3fffffff) are handled as python objects in viper.
This rather should happen with integers > 0xffffffff.

Code to illustrate the issue:

# @micropython.viper
# def largeint(a) -> int:
#     b = a & 0xfffffffe
#     return b                   #  ->  ViperTypeError: return expected 'int' but got 'object'

# print(largeint(3))

@micropython.viper
def largeint2(a):
    b = a & 0xfffffffe           #      works, but b is now a python object rather than a viper integer
    return b

print(largeint2(3))


gv = b'\x03\x00\x00\x00'

@micropython.viper
def and32(p:ptr32):
    p[0] = p[0] & 0xfffffffe              # -> ViperTypeError: can't do binary op between 'int' and 'object'
                                          # but  works with 0x3ffffffe and with int(0xfffffffe)

print('Before: ', int.from_bytes(gv, 'little'))   #  -> 3
and32(gv) 
print('After: ', int.from_bytes(gv, 'little'))    # should be 2, but does not work
CANDIDATE · ISSUE

Type Inference issue when using struct in viper

openby GuillaumeLeclercopened 2023-05-28updated 2023-05-28
bug
MY_TYPE = {
    "var": 0 | uctypes.UINT32
}
storage = bytearray(uctypes.sizeof(MY_TYPE))

@micropython.viper
def bugreport():
    data = uctypes.struct(ptr32(storage), MY_TYPE)
    data.var += int(1)

bugreport()

For some reason data.var is an object even though it's type is clearly defined from the struct. the following error occurs:

ViperTypeError: can\'t do binary op between \'object\' and \'int\'

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