bindings: python: return None if line can't be found by name
authorBartosz Golaszewski <bartekgola@gmail.com>
Sat, 12 May 2018 12:48:58 +0000 (14:48 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 14 May 2018 09:49:13 +0000 (11:49 +0200)
For both find_line() variants (Chip's method and global function)
return None if the internal call to the C find_line function fails
with ENOENT. This is not an error so don't raise an exception.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
bindings/python/gpiodmodule.c

index 8e36ec82bef3a191a722f95fe1a7b3987b146973..9248616eef2172373436926e156826d21be09aec 100644 (file)
@@ -1291,6 +1291,11 @@ gpiod_Chip_find_line(gpiod_ChipObject *self, PyObject *args)
        line = gpiod_chip_find_line(self->chip, name);
        Py_END_ALLOW_THREADS;
        if (!line) {
+               if (errno == ENOENT) {
+                       Py_INCREF(Py_None);
+                       return (gpiod_LineObject *)Py_None;
+               }
+
                PyErr_SetFromErrno(PyExc_OSError);
                return NULL;
        }
@@ -1573,6 +1578,11 @@ static gpiod_LineObject *gpiod_Module_find_line(PyObject *self GPIOD_UNUSED,
        line = gpiod_line_find(name);
        Py_END_ALLOW_THREADS;
        if (!line) {
+               if (errno == ENOENT) {
+                       Py_INCREF(Py_None);
+                       return (gpiod_LineObject *)Py_None;
+               }
+
                PyErr_SetFromErrno(PyExc_OSError);
                return NULL;
        }