Mock.GPIO icon indicating copy to clipboard operation
Mock.GPIO copied to clipboard

[BUG] Unassigned Variable Name (introduced in #12)

Open nathancartlidge opened this issue 8 months ago • 1 comments

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

nathancartlidge avatar Mar 17 '25 12:03 nathancartlidge

just realised that this bug has already been addressed in #14 - this should contain all required code to fix this issue

nathancartlidge avatar Mar 30 '25 22:03 nathancartlidge

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

cclauss avatar Sep 28 '25 12:09 cclauss

closing as this resolved

codenio avatar Sep 29 '25 07:09 codenio