diff options
| author | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:01:42 +0000 |
|---|---|---|
| committer | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:01:42 +0000 |
| commit | b67dcfb9e6bd3ea329bb04875764879a2e7f9cd0 (patch) | |
| tree | 0cbd6fd8bf7a31620386feda70c861b2ee7d8804 | |
| parent | eddccffb2412dd42754cc36244ea6476d742d691 (diff) | |
| download | lasso-b67dcfb9e6bd3ea329bb04875764879a2e7f9cd0.tar.gz lasso-b67dcfb9e6bd3ea329bb04875764879a2e7f9cd0.tar.xz lasso-b67dcfb9e6bd3ea329bb04875764879a2e7f9cd0.zip | |
[project @ fpeters@0d.be-20071005155511-13lg9tc7usfht3ud]
minimalistic functional module
$ python -c 'import lasso; print lasso.Samlp2AuthnRequest().dump()'
<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
SignType="0" SignMethod="0" ForceAuthn="false" IsPassive="false"/>
Original author: Frederic Peters <fpeters@0d.be>
Date: 2007-10-05 17:55:11.508000+02:00
| -rw-r--r-- | bindings/lang_python.py | 70 | ||||
| -rw-r--r-- | bindings/lang_python_wrapper_bottom.c | 4 | ||||
| -rw-r--r-- | bindings/lang_python_wrapper_top.c | 4 | ||||
| -rw-r--r-- | bindings/t.py | 8 |
4 files changed, 81 insertions, 5 deletions
diff --git a/bindings/lang_python.py b/bindings/lang_python.py index 7a9cb316..420395c1 100644 --- a/bindings/lang_python.py +++ b/bindings/lang_python.py @@ -154,20 +154,88 @@ _lasso.init() def generate_wrapper(self, fd): print >> fd, open('lang_python_wrapper_top.c').read() + for h in self.binding_data.headers: + print >> fd, '#include <%s>' % h + print >> fd, '' + + self.wrapper_list = [] for m in self.binding_data.functions: self.generate_function_wrapper(m, fd) for c in self.binding_data.structs: for m in c.methods: self.generate_function_wrapper(m, fd) + self.generate_wrapper_list(fd) print >> fd, open('lang_python_wrapper_bottom.c').read() def generate_function_wrapper(self, m, fd): name = m.name[6:] + self.wrapper_list.append(name) print >> fd, '''static PyObject* %s(PyObject *self, PyObject *args) {''' % name - + parse_tuple_format = [] + parse_tuple_args = [] + for arg in m.args: + arg_type, arg_name = arg + print >> fd, ' %s %s;' % (arg[0], arg[1]) + if arg_type in ('char*', 'const char*', 'gchar*', 'const gchar*'): + arg_type = arg_type.replace('const ', '') + parse_tuple_format.append('s') + parse_tuple_args.append('&%s' % arg_name) + elif arg_type in ['int', 'gint', 'gboolean', 'const gboolean'] + self.binding_data.enums: + parse_tuple_format.append('i') + parse_tuple_args.append('&%s' % arg_name) + else: + parse_tuple_format.append('O') + print >> fd, ' PyGObjectPtr *cvt_%s;' % arg_name + parse_tuple_args.append('&cvt_%s' % arg_name) + + if m.return_type: + print >> fd, ' %s return_value;' % m.return_type + print >> fd, ' PyObject* return_pyvalue;' + print >> fd, '' + + parse_tuple_args = ', '.join(parse_tuple_args) + if parse_tuple_args: + parse_tuple_args = ', ' + parse_tuple_args + + print >> fd, ' if (! PyArg_ParseTuple(args, "%s"%s)) return NULL;' % ( + ''.join(parse_tuple_format), parse_tuple_args) + + for f, arg in zip(parse_tuple_format, m.args): + if f == 'O': + print >> fd, ' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]) + + if m.return_type: + print >> fd, ' return_value =', + print >> fd, '%s(%s);' % (m.name, ', '.join([x[1] for x in m.args])) + + if not m.return_type: + print >> fd, ' Py_INCREF(Py_None);' + print >> fd, ' return Py_None;' + elif m.return_type in ('int', 'gint'): + print >> fd, ' return_pyvalue = PyInt_FromLong(return_value);' + print >> fd, ' Py_INCREF(return_pyvalue);' + print >> fd, ' return return_pyvalue;' + elif m.return_type in ('char*', 'gchar*'): + print >> fd, ' return_pyvalue = PyString_FromString(return_value);' + print >> fd, ' Py_INCREF(return_pyvalue);' + print >> fd, ' return return_pyvalue;' + else: + print >> fd, ' return_pyvalue = PyGObjectPtr_New(G_OBJECT(return_value));' + print >> fd, ' Py_INCREF(return_pyvalue);' + print >> fd, ' return return_pyvalue;' print >> fd, '''} ''' + + def generate_wrapper_list(self, fd): + print >> fd, ''' +static PyMethodDef lasso_methods[] = {''' + for m in self.wrapper_list: + print >> fd, ' {"%s", %s, METH_VARARGS, NULL},' % (m, m) + print >> fd, ' {NULL, NULL, 0, NULL}' + print >> fd, '};' + print >> fd, '' + diff --git a/bindings/lang_python_wrapper_bottom.c b/bindings/lang_python_wrapper_bottom.c index 737ec02e..5f1220c3 100644 --- a/bindings/lang_python_wrapper_bottom.c +++ b/bindings/lang_python_wrapper_bottom.c @@ -1,6 +1,8 @@ PyMODINIT_FUNC init_lasso(void) { + PyObject *m; + if (PyType_Ready(&PyGObjectPtrType) < 0) return; @@ -10,7 +12,7 @@ init_lasso(void) lasso_wrapper_key = g_quark_from_static_string("PyLasso::wrapper"); Py_INCREF(&PyGObjectPtrType); - PyModule_AddObject(m, "PyGobjectPtr", (PyObject *)&PyGobjectPtrType); + PyModule_AddObject(m, "PyGObjectPtr", (PyObject *)&PyGObjectPtrType); } diff --git a/bindings/lang_python_wrapper_top.c b/bindings/lang_python_wrapper_top.c index 6c5f74a2..255087c3 100644 --- a/bindings/lang_python_wrapper_top.c +++ b/bindings/lang_python_wrapper_top.c @@ -28,7 +28,7 @@ PyGObjectPtr_New(GObject *obj) return Py_None; } - self = (PyGObjectPtr*)g_object_get_qdata(obj, lasso_wrapper_key) + self = (PyGObjectPtr*)g_object_get_qdata(obj, lasso_wrapper_key); if (self != NULL) { Py_INCREF(self); } else { @@ -40,7 +40,7 @@ PyGObjectPtr_New(GObject *obj) } static PyTypeObject PyGObjectPtrType = { - PyObject_HEAD_INIT(NULL), + PyObject_HEAD_INIT(NULL) 0, /* ob_size */ "_lasso.PyGObjectPtr", /* tp_name */ sizeof(PyGObjectPtr), /* tp_basicsize */ diff --git a/bindings/t.py b/bindings/t.py index 5a459103..798f08d3 100644 --- a/bindings/t.py +++ b/bindings/t.py @@ -7,10 +7,12 @@ import lang_python class BindingData: def __init__(self): + self.headers = [] self.constants = [] self.structs = [] self.struct_dict = {} self.functions = [] + self.enums = [] def display_structs(self): for struct in self.structs: @@ -113,6 +115,8 @@ def parse_header(header_file): elif in_enum: if line.startswith('}'): in_enum = False + enum_name = line[2:].strip().strip(';') + binding.enums.append(enum_name) else: m = re.match('\s*([a-zA-Z0-9_]+)', line) if m: @@ -170,7 +174,8 @@ def parse_header(header_file): if function_name[0] == '*': return_type += '*' function_name = function_name[1:] - f.return_type = return_type + if return_type != 'void': + f.return_type = return_type f.name = function_name f.args = [] for arg in [x.strip() for x in args.split(',')]: @@ -198,6 +203,7 @@ def parse_headers(): for filename in filenames: if filename == 'lasso_config.h' or 'private' in filename: continue + binding.headers.append(os.path.join(base, filename)[3:]) parse_header(os.path.join(base, filename)) |
