From 34ae60b73e15c857146aa1da52d13c874ed2c0cd Mon Sep 17 00:00:00 2001 From: Yu-Hao Chang <9203700@gmail.com> Date: Sat, 18 Jul 2020 03:27:09 +0100 Subject: [PATCH 1/2] Add checking if the new node is not the same as original mode Signed-off-by: Yu-Hao Chang <9203700@gmail.com> --- RPi/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RPi/core.py b/RPi/core.py index 3137b74..3c83115 100644 --- a/RPi/core.py +++ b/RPi/core.py @@ -1046,7 +1046,7 @@ def setmode(mode): BOARD - Use Raspberry Pi board numbers BCM - Use Broadcom GPIO 00..nn numbers """ - if _State.mode != UNKNOWN: + if _State.mode != UNKNOWN and _State.mode != mode: raise ValueError("A different mode has already been set!") if mode != BCM and mode != BOARD: From 1d9f2d539bff3e41c7aa1516aac9336d84312ffb Mon Sep 17 00:00:00 2001 From: Yu-Hao Chang <9203700@gmail.com> Date: Sun, 26 Jul 2020 04:59:44 +0100 Subject: [PATCH 2/2] Add tests on the repeated mode setup Signed-off-by: Yu-Hao Chang <9203700@gmail.com> --- tests/test_gpio.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_gpio.py b/tests/test_gpio.py index 3cd0568..dac22e7 100644 --- a/tests/test_gpio.py +++ b/tests/test_gpio.py @@ -41,6 +41,16 @@ def test_is_iterable(): assert GPIO_DEVEL.is_iterable(i) is True +@pytest.mark.parametrize('GPIO_MODE', [GPIO.BCM, GPIO.BOARD]) +def test_setmode_remain_silent_in_same_mode(GPIO_MODE): + GPIO_DEVEL.Reset() + GPIO.setmode(GPIO_MODE) # Set a mode first + try: + GPIO.setmode(GPIO_MODE) # Expect there is not exception being raised + except ValueError: + pytest.fail('Failed to due double setup') + + def test_setmode_raise_double_setup_exception(): GPIO_DEVEL.Reset() with pytest.raises(Exception):