From: Bartosz Golaszewski Date: Fri, 15 Feb 2019 12:37:18 +0000 (+0100) Subject: bindings: python: add support for 'default_val' in Line.request() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=0e2ff678538042b7bedd4370fcafbe3ce26b3bca;p=qemu-gpiodev%2Flibgpiod.git bindings: python: add support for 'default_val' in Line.request() Until now Line.request() only took 'default_vals' argument which was passed down directly to LineBulk.request() internally. This meant that even when requesting a single line in output mode, the default value had to be passed in a sequence. Add support for the 'default_val' argument which makes it possible to specify the default value as an integer. The previous 'default_vals' argument is still supported, but the two cannot be passed at the same time. Documentation for default_vals is removed, as it's now deprecated for Line.request(). Signed-off-by: Bartosz Golaszewski --- diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index e6ac3a6..ec28553 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -403,18 +403,55 @@ PyDoc_STRVAR(gpiod_Line_request_doc, " Type of the request.\n" " flags\n" " Other configuration flags.\n" -" default_vals\n" -" Default value of this line (as a sequence, example: default_vals=[ 1 ])."); +" default_val\n" +" Default value of this line." +"\n" +"Note: default_vals argument (sequence of default values passed down to\n" +"LineBulk.request()) is still supported for backward compatibility but is\n" +"now deprecated when requesting single lines."); -/* - * TODO: add support for 'default_val' argument which will allow users to pass - * a single default value directly rather than wrapping it in a sequence. - */ static PyObject *gpiod_Line_request(gpiod_LineObject *self, PyObject *args, PyObject *kwds) { + PyObject *ret, *def_val, *def_vals; gpiod_LineBulkObject *bulk_obj; - PyObject *ret; + int rv; + + def_val = PyDict_GetItemString(kwds, "default_val"); + def_vals = PyDict_GetItemString(kwds, "default_vals"); + + if (def_val && def_vals) { + PyErr_SetString(PyExc_TypeError, + "Cannot pass both default_val and default_vals arguments at the same time"); + return NULL; + } + + if (def_val) { + /* + * If default_val was passed as a single value, we wrap it + * in a tuple and add it to the kwds dictionary to be passed + * down to LineBulk.request(). We also remove the 'default_val' + * entry from kwds. + * + * I'm not sure if it's allowed to modify the kwds dictionary + * but it doesn't seem to cause any problems. If it does then + * we can simply copy the dictionary before calling + * LineBulk.request(). + */ + rv = PyDict_DelItemString(kwds, "default_val"); + if (rv) + return NULL; + + def_vals = Py_BuildValue("(O)", def_val); + if (!def_vals) + return NULL; + + rv = PyDict_SetItemString(kwds, "default_vals", def_vals); + if (rv) { + Py_DECREF(def_vals); + return NULL; + } + } bulk_obj = gpiod_LineToLineBulk(self); if (!bulk_obj)