From: Bartosz Golaszewski Date: Mon, 30 Jul 2018 13:05:53 +0000 (+0200) Subject: bindings: python: fix gpiod_Chip_find_lines() for nonexistent lines X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=cdf5ef94c454b8ebab432cc9172250379545ec8a;p=qemu-gpiodev%2Flibgpiod.git bindings: python: fix gpiod_Chip_find_lines() for nonexistent lines Currently if we call gpiod_Chip_find_lines() with some names that cannot be looked up, we end up getting an exception from the LineBulk object's constructor (because None is not a Line and LineBulk can only hold Line objects). Fix the behavior of gpiod_Chip_find_lines() for nonexistent lines by raising an explicit exception if the underlying gpiod_Chip_find_line() call returns None. Also: add a relevant test case. Fixes: 96c524c4951c ("bindings: implement python bindings") Signed-off-by: Bartosz Golaszewski --- diff --git a/bindings/python/examples/gpiod_tests.py b/bindings/python/examples/gpiod_tests.py index 6a30a4c..910613a 100755 --- a/bindings/python/examples/gpiod_tests.py +++ b/bindings/python/examples/gpiod_tests.py @@ -215,6 +215,20 @@ def find_lines(): add_test('Find multiple lines by name', find_lines) +def find_lines_one_bad(): + chip = gpiod.Chip('gpiochip0') + + print('looking up lines by names') + try: + lines = chip.find_lines(['gpio-mockup-A-3', 'nonexistent', 'gpio-mockup-A-7']) + except TypeError as ex: + print('Error as expected') + return + + assert False, 'TypeError expected' + +add_test('Find multiple lines but one line name is non-existent', find_lines_one_bad) + def create_line_bulk_from_lines(): chip = gpiod.Chip('gpio-mockup-A') line1 = chip.get_line(2) diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index 3978490..a6092e3 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -1646,9 +1646,12 @@ gpiod_Chip_find_lines(gpiod_ChipObject *self, PyObject *args) line = gpiod_Chip_find_line(self, arg); Py_DECREF(arg); - if (!line) { + if (!line || (PyObject *)line == Py_None) { Py_DECREF(iter); Py_DECREF(lines); + if ((PyObject *)line == Py_None) + PyErr_SetString(PyExc_TypeError, + "Unable to find all lines from the list"); return NULL; }