diff mbox series

[libgpiod,8/9] bindings: python: chip: map names for lines requested by offset

Message ID 20251009130516.3729433-9-vfazio@gmail.com
State New
Headers show
Series bindings: python: improve line requests and reconfiguration | expand

Commit Message

Vincent Fazio Oct. 9, 2025, 1:05 p.m. UTC
Previously, lines requested by name could also be referred to by offset
when calling LineRequest.reconfigure_lines. However, lines requested by
offset could not be reconfigured by name if they had one.

Now, the name map is populated if the line has a name regardless of if
it's requested by name or offset.

To make behavior as consistent with earlier versions as possible, a line
requested by name always returns the first line on the chip with that
name. If there are duplicate line names and lines are requested by
offset, the first line in the request is mapped to the name. A line
requested by name always overrides a name mapped via an offset request.

If lines 0-3 all have the same name and a config object looks like:
  {("foo", 1, 2): None}
or
  {(1, "foo", 2): None}

Then lines 0-2 are requested and line 0 can be reconfigured by name.

If the config object looks like:
  {(1, 2): None}

Then lines 1 and 2 are requested and line 1 can be reconfigured by name.

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
 bindings/python/gpiod/chip.py               |  9 ++++++
 bindings/python/tests/tests_line_request.py | 35 ++++++++++++++++++---
 2 files changed, 40 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py
index 2e66018..a98fce6 100644
--- a/bindings/python/gpiod/chip.py
+++ b/bindings/python/gpiod/chip.py
@@ -302,7 +302,16 @@  class Chip:
                 )
 
             if isinstance(line, str):
+                # lines specifically requested by name overwrite any entries
                 name_map[line] = offset
+            else:
+                name = cast("_ext.Chip", self._chip).get_line_name(offset)
+                # Only track lines with actual names. Names from offsets do not
+                # overwrite existing entries (such as requests by name). So if
+                # multiple lines have the same name, the first in the request
+                # list is the one addressable by name
+                if name and name not in name_map:
+                    name_map[name] = offset
 
             line_cfg.add_line_settings(
                 offsets, _line_settings_to_ext(settings or LineSettings())
diff --git a/bindings/python/tests/tests_line_request.py b/bindings/python/tests/tests_line_request.py
index 66e9e8d..aa2cd83 100644
--- a/bindings/python/tests/tests_line_request.py
+++ b/bindings/python/tests/tests_line_request.py
@@ -71,7 +71,9 @@  class ModuleLineRequestsBehaveCorrectlyWithInvalidArguments(TestCase):
 
 class ChipLineRequestWorks(TestCase):
     def setUp(self) -> None:
-        self.sim = gpiosim.Chip(num_lines=8, line_names={5: "foo", 7: "bar"})
+        self.sim = gpiosim.Chip(
+            num_lines=8, line_names={0: "fizz", 1: "fizz", 5: "foo", 7: "bar"}
+        )
         self.chip = gpiod.Chip(self.sim.dev_path)
 
     def tearDown(self) -> None:
@@ -113,6 +115,16 @@  class ChipLineRequestWorks(TestCase):
         with self.chip.request_lines(config={"foo": None}) as req:
             self.assertEqual(req.offsets, [5])
 
+    def test_request_line_with_duplicate_names_by_offset(self) -> None:
+        with self.chip.request_lines(config={(1, 0): None}) as req:
+            self.assertEqual(req.offsets, [1, 0])
+            self.assertEqual(req.lines, [1, 0])
+
+    def test_request_line_with_duplicate_names_mixed_mode(self) -> None:
+        with self.chip.request_lines(config={(1, "fizz"): None}) as req:
+            self.assertEqual(req.offsets, [1, 0])
+            self.assertEqual(req.lines, [1, "fizz"])
+
 
 class ModuleLineRequestWorks(TestCase):
     def setUp(self) -> None:
@@ -500,11 +512,13 @@  class LineRequestSetOutputValues(TestCase):
 
 class ReconfigureRequestedLines(TestCase):
     def setUp(self) -> None:
-        self.sim = gpiosim.Chip(num_lines=8, line_names={3: "foo", 4: "bar", 6: "baz"})
+        self.sim = gpiosim.Chip(
+            num_lines=8, line_names={2: "fizz", 3: "foo", 4: "bar", 6: "baz", 7: "foo"}
+        )
         self.chip = gpiod.Chip(self.sim.dev_path)
         self.req = self.chip.request_lines(
             {
-                (0, 2, "foo", "baz"): gpiod.LineSettings(
+                (7, 0, 2, "foo", "baz"): gpiod.LineSettings(
                     direction=Direction.OUTPUT, active_low=True, drive=Drive.OPEN_DRAIN
                 )
             }
@@ -530,7 +544,7 @@  class ReconfigureRequestedLines(TestCase):
         info = self.chip.get_line_info(2)
         self.assertEqual(info.direction, Direction.OUTPUT)
         self.req.reconfigure_lines(
-            {(0, 2, "foo", "baz"): gpiod.LineSettings(direction=Direction.INPUT)}
+            {(0, "fizz", "foo", "baz"): gpiod.LineSettings(direction=Direction.INPUT)}
         )
         info = self.chip.get_line_info(2)
         self.assertEqual(info.direction, Direction.INPUT)
@@ -609,6 +623,19 @@  class ReconfigureRequestedLines(TestCase):
         info = self.chip.get_line_info(2)
         self.assertEqual(info.direction, Direction.INPUT)
 
+    def test_reconfigure_duplicate_names_with_offset(self) -> None:
+        info3 = self.chip.get_line_info(3)
+        info7 = self.chip.get_line_info(7)
+        self.assertEqual(info3.direction, Direction.OUTPUT)
+        self.assertEqual(info7.direction, Direction.OUTPUT)
+        self.req.reconfigure_lines(
+            {("foo", 7): gpiod.LineSettings(direction=Direction.INPUT)}
+        )
+        info3 = self.chip.get_line_info(3)
+        info7 = self.chip.get_line_info(7)
+        self.assertEqual(info3.direction, Direction.INPUT)
+        self.assertEqual(info7.direction, Direction.INPUT)
+
 
 class ReleasedLineRequestCannotBeUsed(TestCase):
     def test_using_released_line_request(self) -> None: