← index #11638Issue #11639
Related · high · value 2.723
QUERY · 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\'
CANDIDATE · ISSUE

Hidden Extraneous memory allocation

closedby GuillaumeLeclercopened 2023-05-28updated 2024-07-13
import uctypes
import gc
import micropython

MY_TYPE = {
    'var': 0 | uctypes.UINT32,
    "previous_measurements":  4 | uctypes.UINT32,
}
buffer = bytearray(uctypes.sizeof(MY_TYPE))

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

if __name__ == '__main__':
    gc.collect()
    gc.disable()
    bugreport()

This code crash but shouldn't ever allocate memory after the garbage collector is disabled.

When the bugreport is compiled, offsets should be computable by reading MY_TYPE, and memory access replaced accordingly.

I thought that allocating all the data ahead of time in a struct would help make my code jitter free but it seems impossible in micropython.

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