pgularski / pysm

Versatile and flexible Python State Machine library
http://pysm.readthedocs.io/
MIT License
73 stars 11 forks source link

deque Error in MicroPython within ESP32 #16

Open harryxu opened 2 months ago

harryxu commented 2 months ago

Env:

MicroPython v1.23.0 on 2024-06-02; Generic ESP32S3 module with ESP32S3

Error:

  File "/lib/pysm/pysm.py", line 474, in add_state
  File "/lib/pysm/pysm.py", line 784, in validate_add_state
  File "/lib/pysm/pysm.py", line 790, in _validate_state_already_added
  File "/lib/pysm/pysm.py", line 48, in __init__
TypeError: can't convert float to int

Through investigation, I found that the issue was caused by patch_deque in pysm using float('Inf') as the maxlen parameter for deque.

https://github.com/pgularski/pysm/blob/b48cbc79c06a70b84454bcbb24a5ac2095652c0b/pysm/pysm.py#L47-L48

The following is the code I ran in the ESP32 shell, which resulted in the same error.

>>> from collections import deque
iterable = []
maxlen = float('Inf')
>>> deque(iterable, maxlen)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert float to int
harryxu commented 2 months ago

@alhirzel Would it work correctly if we replace float('Inf') with sys.maxsize as the maxlen parameter?

harryxu commented 2 months ago

@alhirzel Would it work correctly if we replace float('Inf') with sys.maxsize as the maxlen parameter?

I tried; it won’t work and will cause a memory error.

thepartisan commented 1 month ago

I stumbled upon this when simply running the unit tests:

test/test_pysm.py:1423: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <pysm.pysm.patch_deque.<locals>.deque_maxlen object at 0x7903f66f9460>, iterable = [], maxlen = inf

    def __init__(self, iterable=None, maxlen=0):
        # pylint: disable=no-member
        if iterable is None:
            iterable = []
        if maxlen in [None, 0]:
            maxlen = float('Inf')
            #maxlen = sys.maxsize
>       self.q = deque_module.deque(iterable, maxlen)
E       TypeError: an integer is required

pysm/pysm.py:49: TypeError

The proposal with sys.maxsizefixed at least the test.