From: Martin Ling Date: Wed, 24 Apr 2013 13:47:07 +0000 (+0100) Subject: python: Add conversion functions to/from GSList * X-Git-Tag: dsupstream~68 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=05cfe1147a9f85d9d3db41d6025c708898a66be9;p=libsigrok.git python: Add conversion functions to/from GSList * --- diff --git a/bindings/python/sigrok/core/classes.py b/bindings/python/sigrok/core/classes.py index 054eb6a1..14781b8c 100644 --- a/bindings/python/sigrok/core/classes.py +++ b/bindings/python/sigrok/core/classes.py @@ -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 diff --git a/bindings/python/sigrok/core/lowlevel.i b/bindings/python/sigrok/core/lowlevel.i index 576af5cb..f50d4d0f 100644 --- a/bindings/python/sigrok/core/lowlevel.i +++ b/bindings/python/sigrok/core/lowlevel.i @@ -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);