bindings: python: provide the chip_name property in line_request
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Thu, 20 Jul 2023 14:47:46 +0000 (16:47 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 21 Jul 2023 18:33:39 +0000 (20:33 +0200)
Provide a wrapper around gpiod_line_request_get_chip_name() for Python
bindings and update the tests.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/chip.py
bindings/python/gpiod/ext/request.c
bindings/python/gpiod/line_request.py
bindings/python/tests/tests_line_request.py

index da93370e7713f7336aa343af7c3d2b2ef29b70e3..b3d8e6143bfff68d9568a8c43320afc8837bbe4e 100644 (file)
@@ -315,6 +315,7 @@ class Chip:
         req_internal = self._chip.request_lines(line_cfg, consumer, event_buffer_size)
         request = LineRequest(req_internal)
 
+        request._chip_name = req_internal.chip_name
         request._offsets = req_internal.offsets
         request._name_map = name_map
         request._offset_map = offset_map
index a32ff8ffa3aa7bc2667d3628de4d5d701f0cc0fe..5db69fe8a21161d21743c49355c04f624481d2df 100644 (file)
@@ -37,6 +37,13 @@ static void request_finalize(request_object *self)
                gpiod_edge_event_buffer_free(self->buffer);
 }
 
+static PyObject *
+request_chip_name(request_object *self, void *Py_UNUSED(ignored))
+{
+       return PyUnicode_FromString(
+                       gpiod_line_request_get_chip_name(self->request));
+}
+
 static PyObject *
 request_num_lines(request_object *self, void *Py_UNUSED(ignored))
 {
@@ -92,6 +99,10 @@ static PyObject *request_fd(request_object *self, void *Py_UNUSED(ignored))
 }
 
 static PyGetSetDef request_getset[] = {
+       {
+               .name = "chip_name",
+               .get = (getter)request_chip_name,
+       },
        {
                .name = "num_lines",
                .get = (getter)request_num_lines,
index 096bf1897561686a4d7e855f9d2c0c326c789c76..cde298f5c7a153676e52c193aa5a6cab6b3e4dc5 100644 (file)
@@ -212,10 +212,18 @@ class LineRequest:
         if not self._req:
             return "<LineRequest RELEASED>"
 
-        return "<LineRequest num_lines={} offsets={} fd={}>".format(
-            self.num_lines, self.offsets, self.fd
+        return '<LineRequest chip="{}" num_lines={} offsets={} fd={}>'.format(
+            self.chip_name, self.num_lines, self.offsets, self.fd
         )
 
+    @property
+    def chip_name(self) -> str:
+        """
+        Name of the chip this request was made on.
+        """
+        self._check_released()
+        return self._chip_name
+
     @property
     def num_lines(self) -> int:
         """
index aa84b9ac2f4d33f3c8aec2cb9629745e1446742d..f99b93df8273d8d956ac9d23650ed6815f8bc071 100644 (file)
@@ -529,11 +529,14 @@ class LineRequestStringRepresentation(TestCase):
         del self.sim
 
     def test_str(self):
-        with gpiod.request_lines(self.sim.dev_path, config={(2, 6, 4, 1): None}) as req:
-            self.assertEqual(
-                str(req),
-                "<LineRequest num_lines=4 offsets=[2, 6, 4, 1] fd={}>".format(req.fd),
-            )
+        with gpiod.Chip(self.sim.dev_path) as chip:
+            with chip.request_lines(config={(2, 6, 4, 1): None}) as req:
+                self.assertEqual(
+                    str(req),
+                    '<LineRequest chip="{}" num_lines=4 offsets=[2, 6, 4, 1] fd={}>'.format(
+                        self.sim.name, req.fd
+                    ),
+                )
 
     def test_str_released(self):
         req = gpiod.request_lines(self.sim.dev_path, config={(2, 6, 4, 1): None})