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 7/9] bindings: python: ext: add ability to query line name
Date: Thu,  9 Oct 2025 08:05:13 -0500	[thread overview]
Message-ID: <20251009130516.3729433-8-vfazio@gmail.com> (raw)
In-Reply-To: <20251009130516.3729433-1-vfazio@gmail.com>

Investigation has shown that the construction of the Enum values to
create the LineInfo object is expensive [0].

Add a method to retrieve the line name from a given offset without the
overhead of generating a full LineInfo object.

[0]: https://github.com/brgl/libgpiod/issues/149#issuecomment-3319448712
Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
 bindings/python/gpiod/_ext.pyi   |  1 +
 bindings/python/gpiod/ext/chip.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/bindings/python/gpiod/_ext.pyi b/bindings/python/gpiod/_ext.pyi
index 1beb80d..31fd352 100644
--- a/bindings/python/gpiod/_ext.pyi
+++ b/bindings/python/gpiod/_ext.pyi
@@ -47,6 +47,7 @@ class Chip:
     def get_info(self) -> ChipInfo: ...
     def line_offset_from_id(self, id: str) -> int: ...
     def get_line_info(self, offset: int, watch: bool) -> LineInfo: ...
+    def get_line_name(self, offset: int) -> Optional[str]: ...
     def request_lines(
         self,
         line_cfg: LineConfig,
diff --git a/bindings/python/gpiod/ext/chip.c b/bindings/python/gpiod/ext/chip.c
index 1e97d7d..98435d9 100644
--- a/bindings/python/gpiod/ext/chip.c
+++ b/bindings/python/gpiod/ext/chip.c
@@ -147,6 +147,32 @@ static PyObject *chip_get_line_info(chip_object *self, PyObject *args)
 	return info_obj;
 }
 
+static PyObject *chip_get_line_name(chip_object *self, PyObject *args)
+{
+	int ret;
+	unsigned int offset;
+	struct gpiod_line_info *info;
+	PyObject *line_name;
+	const char *name;
+
+	ret = PyArg_ParseTuple(args, "I", &offset);
+	if (!ret)
+		return NULL;
+
+	Py_BEGIN_ALLOW_THREADS;
+	info = gpiod_chip_get_line_info(self->chip, offset);
+	Py_END_ALLOW_THREADS;
+	if (!info)
+		return Py_gpiod_SetErrFromErrno();
+
+	name = gpiod_line_info_get_name(info);
+	line_name = (name == NULL) ? Py_None : PyUnicode_FromString(name);
+
+	gpiod_line_info_free(info);
+
+	return line_name;
+}
+
 static PyObject *
 chip_unwatch_line_info(chip_object *self, PyObject *args)
 {
@@ -333,6 +359,11 @@ static PyMethodDef chip_methods[] = {
 		.ml_meth = (PyCFunction)chip_request_lines,
 		.ml_flags = METH_VARARGS,
 	},
+	{
+		.ml_name = "get_line_name",
+		.ml_meth = (PyCFunction)chip_get_line_name,
+		.ml_flags = METH_VARARGS,
+	},
 	{ }
 };
 
-- 
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 ` Vincent Fazio [this message]
2025-10-09 13:05 ` [libgpiod][PATCH 8/9] bindings: python: chip: map names for lines requested by offset Vincent Fazio
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-8-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).