diff options
author | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:02:22 +0000 |
---|---|---|
committer | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:02:22 +0000 |
commit | c1bf8393fb0d546ed14a53377d0e9074fdce8b95 (patch) | |
tree | 146bf0db93d53b664adbed70122c50a2e627a680 | |
parent | b780cf634ec9f2dc4790a04f4c1b1cdd505c58c2 (diff) | |
download | lasso-c1bf8393fb0d546ed14a53377d0e9074fdce8b95.tar.gz lasso-c1bf8393fb0d546ed14a53377d0e9074fdce8b95.tar.xz lasso-c1bf8393fb0d546ed14a53377d0e9074fdce8b95.zip |
[project @ fpeters@0d.be-20071006172452-7ed22uoeqm22gled]
support for exceptions, with a complete hierarchy of exceptions; and emulation
of swig binding behaviour (access to code via [0] and to error string via [1])
login = lasso.Login(server)
try:
login.initAuthnRequest('plop', lasso.HTTP_METHOD_REDIRECT)
except lasso.Error, error:
print error
# <lasso.ProviderNotFoundError(-201): ProviderID unknown to LassoServer.>
print error.code
# -201
print error[0]
# -201
print error[1]
# ProviderID unknown to LassoServer.
sys.exit(1)
Original author: Frederic Peters <fpeters@0d.be>
Date: 2007-10-06 19:24:52.095000+02:00
-rw-r--r-- | bindings/lang_python.py | 84 | ||||
-rw-r--r-- | bindings/overrides.xml | 13 | ||||
-rw-r--r-- | bindings/t.py | 2 |
3 files changed, 96 insertions, 3 deletions
diff --git a/bindings/lang_python.py b/bindings/lang_python.py index fbaa4695..85be9e7a 100644 --- a/bindings/lang_python.py +++ b/bindings/lang_python.py @@ -33,6 +33,7 @@ class PythonBinding: def generate(self): fd = open('python/lasso.py', 'w') self.generate_header(fd) + self.generate_exceptions(fd) self.generate_constants(fd) for clss in self.binding_data.structs: self.generate_class(clss, fd) @@ -52,6 +53,74 @@ import _lasso _lasso.init() ''' + def generate_exceptions(self, fd): + done_cats = [] + print >> fd, '''\ +class Error(Exception): + code = None + + @staticmethod + def raise_on_rc(rc): + global exceptions_dict + raise exceptions_dict.get(rc) + + def __str__(self): + return '<lasso.%s(%s): %s>' % (self.__class__.__name__, self.code, _lasso.strError(self.code)) + + def __getitem__(self, i): + # compatibility with SWIG bindings + if i == 0: + return self.code + elif i == 1: + return _lasso.strError(self.code) + else: + raise IndexError() +''' + for exc_cat in self.binding_data.overrides.findall('exception/category'): + cat = exc_cat.attrib.get('name') + done_cats.append(cat) + parent_cat = exc_cat.attrib.get('parent', '') + print >> fd, '''\ +class %sError(%sError): + pass +''' % (cat, parent_cat) + + exceptions_dict = {} + + for c in self.binding_data.constants: + m = re.match(r'LASSO_(\w+)_ERROR_(.*)', c[1]) + if not m: + continue + cat, detail = m.groups() + cat = cat.title().replace('_', '') + detail = detail.title().replace('_', '') + if not cat in done_cats: + done_cats.append(cat) + for exc_cat in self.binding_data.overrides.findall('exception/category'): + if exc_cat.attrib.get('name') == cat: + parent_cat = exc_cat.attrib.get('parent') + break + else: + parent_cat = '' + + print >> fd, '''\ +class %sError(%sError): + pass +''' % (cat, parent_cat) + + print >> fd, '''\ +class %sError(%sError): + code = _lasso.%s +''' % (detail, cat, c[1][6:]) + + exceptions_dict[detail] = c[1][6:] + + print >> fd, 'exceptions_dict = {' + for k, v in exceptions_dict.items(): + print >> fd, ' _lasso.%s: %sError,' % (v, k) + print >> fd, '}' + print >> fd, '' + def generate_footer(self, fd): print >> fd, ''' import lasso @@ -206,7 +275,7 @@ import lasso print >> fd, ' elif rc > 0:' # recoverable error print >> fd, ' return rc' print >> fd, ' elif rc < 0:' # unrecoverable error - print >> fd, ' raise \'XXX(rc)\'' # XXX: exception hierarchy + print >> fd, ' raise Error.raise_on_rc(rc)' else: print >> fd, ' return _lasso.%s(self._cptr%s)' % ( m.name[6:], c_args) @@ -321,7 +390,6 @@ register_constants(PyObject *d) print >> fd, '' - def return_value(self, fd, vtype): if vtype == 'gboolean': print >> fd, ' if (return_value) {' @@ -338,6 +406,16 @@ register_constants(PyObject *d) elif vtype in ('char*', 'gchar*'): print >> fd, ' if (return_value) {' print >> fd, ' return_pyvalue = PyString_FromString(return_value);' + print >> fd, ' g_free(return_value);' + #print >> fd, ' Py_INCREF(return_pyvalue);' + print >> fd, ' return return_pyvalue;' + print >> fd, ' } else {' + print >> fd, ' Py_INCREF(Py_None);' + print >> fd, ' return Py_None;' + print >> fd, ' }' + elif vtype in ('const char*', 'const gchar*'): + print >> fd, ' if (return_value) {' + print >> fd, ' return_pyvalue = PyString_FromString(return_value);' #print >> fd, ' Py_INCREF(return_pyvalue);' print >> fd, ' return return_pyvalue;' print >> fd, ' } else {' @@ -366,6 +444,8 @@ register_constants(PyObject *d) def generate_function_wrapper(self, m, fd): name = m.name[6:] + if name == 'strerror': # special case so it doesn't conflict with strerror(3) + name = 'strError' self.wrapper_list.append(name) print >> fd, '''static PyObject* %s(PyObject *self, PyObject *args) diff --git a/bindings/overrides.xml b/bindings/overrides.xml index eaf6bd18..1a862576 100644 --- a/bindings/overrides.xml +++ b/bindings/overrides.xml @@ -9,4 +9,17 @@ <param name="public_key" optional="true"/> <param name="ca_cert_chain" optional="true"/> </func> + <exception> + <category name="Profile"/> + <category name="Provider"/> + <category name="Server" parent="Provider"/> + <category name="Login" parent="Profile"/> + <category name="Logout" parent="Profile"/> + <category name="Defederation" parent="Profile"/> + <category name="NameIdentifierMapping" parent="Profile"/> + <category name="WsfProfile" parent="Profile"/> + <category name="Discovery" parent="WsfProfile"/> + <category name="DataService" parent="WsfProfile"/> + <category name="Dst" parent="WsfProfile"/> + </exception> </overrides> diff --git a/bindings/t.py b/bindings/t.py index 17957d33..a856142a 100644 --- a/bindings/t.py +++ b/bindings/t.py @@ -194,7 +194,7 @@ def parse_header(header_file): i += 1 line = line[:-1] + lines[i].lstrip() - m = re.match(r'LASSO_EXPORT\s+([\w]+\*?)\s+(\*?\w+)\s*\((.*?)\)', line) + m = re.match(r'LASSO_EXPORT\s+((?:const |)[\w]+\*?)\s+(\*?\w+)\s*\((.*?)\)', line) if m and not m.group(2).endswith('_get_type'): f = Function() binding.functions.append(f) |