@@ -3,6 +3,7 @@
from __future__ import annotations
+import warnings
from typing import TYPE_CHECKING, Optional, Union, cast
from . import _ext
@@ -173,12 +174,21 @@ class LineRequest:
for line, settings in config_iter(config):
try:
offset = self._line_to_offset(line)
- line_settings[offset] = settings
+ if offset in self.offsets:
+ line_settings[offset] = settings
+ else:
+ warnings.warn(
+ f"Line offset '{offset}' was not included in original request.",
+ stacklevel=2,
+ )
except ValueError:
# _line_to_offset will raise a ValueError when it encounters
# an unrecognized line name. Ignore these like we do offsets
# that were not in the original request.
- pass
+ warnings.warn(
+ f"Line name '{line}' was not included in original request.",
+ stacklevel=2,
+ )
for offset in self.offsets:
settings = line_settings.get(offset) or LineSettings()
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
+import warnings
from unittest import TestCase
import gpiod
@@ -604,22 +605,30 @@ class ReconfigureRequestedLines(TestCase):
def test_reconfigure_extra_offsets(self) -> None:
info = self.chip.get_line_info(2)
self.assertEqual(info.direction, Direction.OUTPUT)
- self.req.reconfigure_lines(
- {(0, 2, 3, 6, 5): gpiod.LineSettings(direction=Direction.INPUT)}
- )
+ with warnings.catch_warnings(record=True) as w:
+ self.req.reconfigure_lines(
+ {(0, 2, 3, 6, 5): gpiod.LineSettings(direction=Direction.INPUT)}
+ )
+ assert len(w) == 1
+ assert issubclass(w[0].category, UserWarning)
+ assert "Line offset '5'" in str(w[0].message)
info = self.chip.get_line_info(2)
self.assertEqual(info.direction, Direction.INPUT)
def test_reconfigure_extra_names(self) -> None:
info = self.chip.get_line_info(2)
self.assertEqual(info.direction, Direction.OUTPUT)
- self.req.reconfigure_lines(
- {
- (0, 2, "foo", "baz", "buzz"): gpiod.LineSettings(
- direction=Direction.INPUT
- )
- }
- )
+ with warnings.catch_warnings(record=True) as w:
+ self.req.reconfigure_lines(
+ {
+ (0, 2, "foo", "baz", "buzz"): gpiod.LineSettings(
+ direction=Direction.INPUT
+ )
+ }
+ )
+ assert len(w) == 1
+ assert issubclass(w[0].category, UserWarning)
+ assert "Line name 'buzz'" in str(w[0].message)
info = self.chip.get_line_info(2)
self.assertEqual(info.direction, Direction.INPUT)
Previously, attempting to reconfigure a line that was not included in the original request provided no feedback to the caller that there was invalid data in the request. Now, emit a warning when an unknown offset or line name is encountered. Signed-off-by: Vincent Fazio <vfazio@gmail.com> --- bindings/python/gpiod/line_request.py | 14 ++++++++-- bindings/python/tests/tests_line_request.py | 29 ++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-)