bindings: python: provide a helper for calling methods with python args
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 13 Jul 2018 13:07:47 +0000 (15:07 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 13 Jul 2018 13:08:45 +0000 (15:08 +0200)
Wrap the calls to PyObject_GetAttrString() and PyObject_Call() in
a single helper that allows to call a python method and pass it the
args and kwds tuples directly as received from the interpreter.

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

index d4af5a72d0fce22f32b9c1c19745f5e3cc844d4b..b45ce6604a17a9f101ae36fe1ebdb633896825bc 100644 (file)
@@ -78,6 +78,21 @@ enum {
        gpiod_FALLING_EDGE,
 };
 
+static PyObject *gpiod_CallMethodPyArgs(PyObject *obj, const char *method,
+                                       PyObject *args, PyObject *kwds)
+{
+       PyObject *callable, *ret;
+
+       callable = PyObject_GetAttrString((PyObject *)obj, method);
+       if (!callable)
+               return NULL;
+
+       ret = PyObject_Call(callable, args, kwds);
+       Py_DECREF(callable);
+
+       return ret;
+}
+
 static int gpiod_LineEvent_init(void)
 {
        PyErr_SetString(PyExc_NotImplementedError,
@@ -350,19 +365,15 @@ static PyObject *gpiod_Line_request(gpiod_LineObject *self,
                                    PyObject *args, PyObject *kwds)
 {
        gpiod_LineBulkObject *bulk_obj;
-       PyObject *ret, *callable;
+       PyObject *ret;
 
        bulk_obj = gpiod_LineToLineBulk(self);
        if (!bulk_obj)
                return NULL;
 
-       callable = PyObject_GetAttrString((PyObject *)bulk_obj, "request");
-       if (!callable)
-               return NULL;
-
-       ret = PyObject_Call(callable, args, kwds);
+       ret = gpiod_CallMethodPyArgs((PyObject *)bulk_obj,
+                                     "request", args, kwds);
        Py_DECREF(bulk_obj);
-       Py_DECREF(callable);
 
        return ret;
 }
@@ -461,19 +472,15 @@ static PyObject *gpiod_Line_event_wait(gpiod_LineObject *self,
                                       PyObject *args, PyObject *kwds)
 {
        gpiod_LineBulkObject *bulk_obj;
-       PyObject *events, *callable;
+       PyObject *events;
 
        bulk_obj = gpiod_LineToLineBulk(self);
        if (!bulk_obj)
                return NULL;
 
-       callable = PyObject_GetAttrString((PyObject *)bulk_obj, "event_wait");
-       if (!callable)
-               return NULL;
-
-       events = PyObject_Call(callable, args, kwds);
+       events = gpiod_CallMethodPyArgs((PyObject *)bulk_obj,
+                                       "event_wait", args, kwds);
        Py_DECREF(bulk_obj);
-       Py_DECREF(callable);
        if (!events)
                return NULL;