diff mbox series

[libgpiod,7/9] bindings: python: ext: add ability to query line name

Message ID 20251009130516.3729433-8-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
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 mbox series

Patch

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,
+	},
 	{ }
 };