adafruit / Adafruit_Blinka

Add CircuitPython hardware API and libraries to MicroPython & CPython devices
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
MIT License
439 stars 327 forks source link

Exception thrown in Lockable.unlock() does not follow CircuitPython behavior #748

Closed caternuson closed 6 months ago

caternuson commented 7 months ago

Board Name

Any Blinka board that supports busio

Steps

Call unlock() without any prior calls to try_lock(). Example on a linux machine using an MCP2221:

user$ python3
Python 3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import board
>>> i2c = board.I2C()
>>> i2c.unlock()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/blinka/lib/python3.8/site-packages/adafruit_blinka/__init__.py", line 76, in unlock
    raise ValueError("Not locked")
ValueError: Not locked
>>> 

For contrast, here's the same thing on a native CP board:

Adafruit CircuitPython 8.2.6 on 2023-09-12; Adafruit QT Py RP2040 with rp2040
>>> import board
>>> i2c = board.STEMMA_I2C()
>>> i2c.unlock()
>>> 

Description

No response

Additional information

CircuitPython busio documentation: https://docs.circuitpython.org/en/latest/shared-bindings/busio/

The native CP behavior seems to return a None type when calling unlock() regardless of current state:

Adafruit CircuitPython 8.2.6 on 2023-09-12; Adafruit QT Py RP2040 with rp2040
>>> import board
>>> i2c = board.STEMMA_I2C()
>>> print(i2c.unlock())
None
>>> print(i2c.try_lock())
True
>>> print(i2c.unlock())
None
>>>