diff options
| author | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:02:38 +0000 |
|---|---|---|
| committer | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:02:38 +0000 |
| commit | e1f40bbdd7ba46e51d72389ec17820160fa461b6 (patch) | |
| tree | f22b811d2f6a83492ba89c5f15886d73a396dc8f | |
| parent | 3e20b4b01c0e36a3b151f84c548681801785c482 (diff) | |
| download | lasso-e1f40bbdd7ba46e51d72389ec17820160fa461b6.tar.gz lasso-e1f40bbdd7ba46e51d72389ec17820160fa461b6.tar.xz lasso-e1f40bbdd7ba46e51d72389ec17820160fa461b6.zip | |
[project @ fpeters@0d.be-20071008090356-014qg89f2kq667v4]
support for getter/setter of list of strings (and partially done of objects)
Original author: Frederic Peters <fpeters@0d.be>
Date: 2007-10-08 11:03:56.732000+02:00
| -rw-r--r-- | bindings/lang_python.py | 80 | ||||
| -rw-r--r-- | bindings/t.py | 10 |
2 files changed, 84 insertions, 6 deletions
diff --git a/bindings/lang_python.py b/bindings/lang_python.py index e5825864..c0946b99 100644 --- a/bindings/lang_python.py +++ b/bindings/lang_python.py @@ -28,7 +28,7 @@ class PythonBinding: self.binding_data = binding_data def is_pygobject(self, t): - return t not in ['char*', 'const char*', 'gchar*', 'const gchar*', + return t not in ['char*', 'const char*', 'gchar*', 'const gchar*', 'GList*', 'int', 'gint', 'gboolean', 'const gboolean'] + self.binding_data.enums def generate(self): @@ -191,6 +191,7 @@ import lasso # create properties for members for m in clss.members: mname = format_as_python(m[1]) + options = m[2] print >> fd, ' def get_%s(self):' % mname if self.is_pygobject(m[0]): print >> fd, ' t, cptr = _lasso.%s_%s_get(self._cptr)' % ( @@ -199,6 +200,8 @@ import lasso print >> fd, ' o = klass.__new__(klass)' print >> fd, ' o._cptr = cptr' print >> fd, ' return o' + elif m[0] == 'GList*' and options.get('elem_type') != 'char*': + print >> fd, ' XXX' else: print >> fd, ' return _lasso.%s_%s_get(self._cptr)' % ( klassname, mname) @@ -346,6 +349,8 @@ register_constants(PyObject *d) print >> fd, ' PyObject* return_tuple;' print >> fd, ' PyObject* type_name;' + print >> fd, '' + print >> fd, ' if (! PyArg_ParseTuple(args, "O", &cvt_this)) return NULL;' print >> fd, ' this = (%s*)cvt_this->obj;' % klassname @@ -356,7 +361,7 @@ register_constants(PyObject *d) else: print >> fd, ' return_value = this->%s;' % m[1]; - self.return_value(fd, m[0]) + self.return_value(fd, m[0], m[2]) print >> fd, '}' print >> fd, '' @@ -379,13 +384,19 @@ register_constants(PyObject *d) parse_format = 'i' parse_arg = '&value' print >> fd, ' %s value;' % arg_type + elif arg_type == 'GList*': + parse_format = 'O' + print >> fd, ' %s value;' % arg_type + print >> fd, ' PyObject *cvt_value;' + print >> fd, ' int i, l;' + parse_arg = '&cvt_value' else: parse_format = 'O' print >> fd, ' %s value;' % arg_type print >> fd, ' PyGObjectPtr *cvt_value;' parse_arg = '&cvt_value' - print >> fd, ' if (! PyArg_ParseTuple(args, "O%s", &cvt_this, %s)) return NULL;' %( + print >> fd, ' if (! PyArg_ParseTuple(args, "O%s", &cvt_this, %s)) return NULL;' % ( parse_format, parse_arg) print >> fd, ' this = (%s*)cvt_this->obj;' % klassname @@ -394,6 +405,41 @@ register_constants(PyObject *d) elif parse_format in ('s', 'z'): print >> fd, ' if (this->%s) g_free(this->%s);' % (m[1], m[1]) print >> fd, ' this->%s = g_strdup(value);' % m[1] + elif parse_format == 'O' and arg_type == 'GList*': + elem_type = m[2].get('elem_type') + # XXX: raise appropriate exception (TypeError) + print >> fd, ' if (!PyTuple_Check(cvt_value)) return NULL;' + if elem_type == 'char*': + print >> fd, '''\ + if (this->%(v)s) { + /* free existing list */ + g_list_foreach(this->%(v)s, (GFunc)g_free, NULL); + g_list_free(this->%(v)s); + } + this->%(v)s = NULL; + /* create new list */ + l = PyTuple_Size(cvt_value); + for (i=0; i<l; i++) { + PyObject *pystr = PyTuple_GET_ITEM(cvt_value, i); + this->%(v)s = g_list_append(this->%(v)s, g_strdup(PyString_AsString(pystr))); + }''' % {'v': m[1]} + else: + # assumes type is GObject + print >> fd, '''\ + if (this->%(v)s) { + /* free existing list */ + g_list_foreach(this->%(v)s, (GFunc)g_object_unref, NULL); + g_list_free(this->%(v)s); + } + this->%(v)s = NULL; + /* create new list */ + l = PyTuple_Size(cvt_value); + for (i=0; i<l; i++) { + /* XXX: should check it is really a PyGObjectPtr */ + PyGObjectPtr *pyobj = (PyGObjectPtr*)PyTuple_GET_ITEM(cvt_value, i); + this->%(v)s = g_list_append(this->%(v)s, g_object_ref(pyobj->obj)); + }''' % {'v': m[1]} + elif parse_format == 'O': print >> fd, ' this->%s = (%s)g_object_ref(cvt_value->obj);' % (m[1], m[0]) @@ -403,7 +449,7 @@ register_constants(PyObject *d) print >> fd, '' - def return_value(self, fd, vtype): + def return_value(self, fd, vtype, options): if vtype == 'gboolean': print >> fd, ' if (return_value) {' print >> fd, ' Py_INCREF(Py_True);' @@ -435,6 +481,30 @@ register_constants(PyObject *d) print >> fd, ' Py_INCREF(Py_None);' print >> fd, ' return Py_None;' print >> fd, ' }' + elif vtype in ('GList*',): + print >> fd, '''\ + if (return_value == NULL) { + Py_INCREF(Py_None); + return Py_None; + } else { + GList *item; + int i; + + item = return_value; + return_pyvalue = PyTuple_New(g_list_length(return_value));''' + elem_type = options.get('elem_type') + if elem_type == 'char*': + print >> fd, '''\ + for (i = 0; item; i++) { + PyTuple_SetItem(return_pyvalue, i, PyString_FromString(item->data)); + item = g_list_next(item); + }''' + else: + # assume GObject* + pass + print >> fd, '''\ + return return_pyvalue; + }''' else: # return a tuple with (object type, cPtr) print >> fd, '''\ @@ -514,7 +584,7 @@ register_constants(PyObject *d) print >> fd, ' Py_INCREF(Py_None);' print >> fd, ' return Py_None;' else: - self.return_value(fd, m.return_type) + self.return_value(fd, m.return_type, {}) print >> fd, '}' print >> fd, '' diff --git a/bindings/t.py b/bindings/t.py index 350ab302..700188db 100644 --- a/bindings/t.py +++ b/bindings/t.py @@ -191,7 +191,15 @@ def parse_header(header_file): if member_name == 'parent': in_struct.parent = member_type else: - in_struct.members.append(normalise_var(member_type, member_name)) + in_struct.members.append( + list(normalise_var(member_type, member_name)) + [{}]) + if member_type == 'GList': + options = in_struct.members[-1][-1] + if '/* of' in line: + of_type = line[line.index('/* of')+6:].split()[0] + if of_type == 'strings': + of_type = 'char*' + options['elem_type'] = of_type elif line.startswith('LASSO_EXPORT '): while not line.strip().endswith(';'): i += 1 |
