linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vincent Fazio <vfazio@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: vfazio@xes-inc.com, Vincent Fazio <vfazio@gmail.com>
Subject: [libgpiod][PATCH 8/9] bindings: python: chip: map names for lines requested by offset
Date: Thu,  9 Oct 2025 08:05:14 -0500	[thread overview]
Message-ID: <20251009130516.3729433-9-vfazio@gmail.com> (raw)
In-Reply-To: <20251009130516.3729433-1-vfazio@gmail.com>

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 --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:
-- 
2.43.0


  parent reply	other threads:[~2025-10-09 13:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-09 13:05 [libgpiod][PATCH 0/9] bindings: python: improve line requests and reconfiguration Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 1/9] bindings: python: make config iteration consistent Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 2/9] bindings: python: remove unused attribute from LineRequest Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 3/9] bindings: python: chip: track requested lines when enumerating Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 4/9] bindings: python: chip: simplify duplicate checking Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 5/9] bindings: python: chip: check mapped_output_values membership once Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 6/9] bindings: python: line_request: ignore invalid line names in reconfigure_lines Vincent Fazio
2025-10-09 13:05 ` [libgpiod][PATCH 7/9] bindings: python: ext: add ability to query line name Vincent Fazio
2025-10-09 13:05 ` Vincent Fazio [this message]
2025-10-09 13:05 ` [libgpiod][PATCH 9/9] bindings: python: line_request: warn on unknown lines when reconfiguring Vincent Fazio
2025-10-13 15:31 ` [libgpiod][PATCH 0/9] bindings: python: improve line requests and reconfiguration Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251009130516.3729433-9-vfazio@gmail.com \
    --to=vfazio@gmail.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=vfazio@xes-inc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).