Mock.GPIO
Mock.GPIO copied to clipboard
[BUG] Unassigned Variable Name (introduced in #12)
Describe the bug
The output function (as modified in #12) uses an incorrect variable name channel instead of channels in the case where only a single channel is provided. This variable will be unassigned
(see https://github.com/codenio/Mock.GPIO/blob/b5f98aa65cc3e7ecf69941605515003ee13aea3c/Mock/GPIO.py#L132C1-L136C87)
def output(channels, values):
...
else:
if type(values) is list or type(values) is tuple:
for value in values:
logger.info("Output channel : {} with value : {}".format(channel, value))
else:
logger.info("Output channel : {} with value : {}".format(channel, values))
To Reproduce Steps to reproduce the behavior:
import Mock.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.OUT)
GPIO.output(15, 1)
This produces an UnboundLocalError
Expected behavior
This should not produce an UnboundLocalError
Desktop (please complete the following information): Windows 11 / Python 3.12
just realised that this bug has already been addressed in #14 - this should contain all required code to fix this issue
The same fix is proposed in both:
- #13
- #14
Agreed. I can replicate:
% uvx --with=mock-gpio python # https://docs.astral.sh/uv
>>> import Mock.GPIO as GPIO
>>> GPIO.setmode(GPIO.BOARD)
>>> GPIO.setup(15, GPIO.OUT)
>>> GPIO.output([15], 1) # Passing a list works as expected.
>>> GPIO.output((15,), 1) # Passing a tuple works as expected.
>>> GPIO.output(15, 1) # Passing an integer value Fails!!!
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
GPIO.output(15, 1)
~~~~~~~~~~~^^^^^^^
File "/Users/cclauss/Python/itinerant_futurizer/Mock.GPIO/Mock/GPIO.py", line 136, in output
logger.info("Output channel : {} with value : {}".format(channel, values))
^^^^^^^
UnboundLocalError: cannot access local variable 'channel' where it is not associated with a value
closing as this resolved