Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checking if the new node is not the same as original mode in the setmode function #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RPi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down