]> sigrok.org Git - libsigrok.git/commitdiff
python: Add conversion functions to/from GSList *
authorMartin Ling <redacted>
Wed, 24 Apr 2013 13:47:07 +0000 (14:47 +0100)
committerUwe Hermann <redacted>
Wed, 24 Apr 2013 21:20:47 +0000 (23:20 +0200)
bindings/python/sigrok/core/classes.py
bindings/python/sigrok/core/lowlevel.i

index 054eb6a17a8d934fb3a69e8d90b121f5b3c32fb8..14781b8c031f0ec4d40a676abf74c88c4e89b7dd 100644 (file)
@@ -125,14 +125,9 @@ class Driver(object):
         if not self._initialized:
             check(sr_driver_init(self.context.struct, self.struct))
             self._initialized = True
-        devices = []
         device_list = sr_driver_scan(self.struct, None)
-        device_list_item = device_list
-        while device_list_item:
-            ptr = device_list_item.data
-            device_ptr = gpointer_to_sr_dev_inst_ptr(ptr)
-            devices.append(Device(self, device_ptr))
-            device_list_item = device_list_item.next
+        devices = [Device(self, gpointer_to_sr_dev_inst_ptr(ptr))
+            for ptr in gslist_to_python(device_list)]
         g_slist_free(device_list)
         return devices
 
index 576af5cbd42d5a8e01f5464ba6ae9cf9296d1788..f50d4d0f75d2f53038fde466333db5e9a4c3dcce 100644 (file)
@@ -78,8 +78,36 @@ PyObject *cdata(const void *data, unsigned long size)
 #endif
 }
 
+GSList *python_to_gslist(PyObject *pylist)
+{
+    if (PyList_Check(pylist)) {
+        GSList *gslist = NULL;
+        int size = PyList_Size(pylist);
+        int i;
+        for (i = size - 1; i >= 0; i--) {
+            SwigPyObject *o = (SwigPyObject *)PyList_GetItem(pylist, i);
+            void *data = o->ptr;
+            gslist = g_slist_prepend(gslist, data);
+        }
+        return gslist;
+    }
+    return NULL;
+}
+
+PyObject *gslist_to_python(GSList *gslist)
+{
+    PyObject *pylist = PyList_New(0);
+    GSList *l;
+    for (l = gslist; l; l = l->next)
+        PyList_Append(pylist, SWIG_NewPointerObj(l->data, SWIGTYPE_p_void, 0));
+    return pylist;
+}
+
 %}
 
 int sr_session_datafeed_python_callback_add(PyObject *cb);
 
 PyObject *cdata(const void *data, unsigned long size);
+
+GSList *python_to_gslist(PyObject *pylist);
+PyObject *gslist_to_python(GSList *gslist);