diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index 21ef6b4..318e49d 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -430,7 +430,7 @@ class CppConstructor(ForwardWrapperBase): wrapper is used as the python class __init__ method. """ - def __init__(self, parameters, unblock_threads=None, visibility='public', deprecated=False): + def __init__(self, parameters, unblock_threads=None, visibility='public', deprecated=False, throw=()): """ @param parameters: the constructor parameters @@ -438,6 +438,10 @@ class CppConstructor(ForwardWrapperBase): - False: Not deprecated - True: Deprecated - "message": Deprecated, and deprecation warning contains the given message + + @param throw: list of C++ exceptions that the constructor may throw + @type throw: list of L{CppException} + """ self.stack_where_defined = traceback.extract_stack() if unblock_threads is None: @@ -457,6 +461,10 @@ class CppConstructor(ForwardWrapperBase): self.wrapper_actual_name = None self._class = None + for t in throw: + assert isinstance(t, CppException) + self.throw = list(throw) + def clone(self): """Creates a semi-deep copy of this constructor wrapper. The returned constructor wrapper clone contains copies of all parameters, so @@ -482,6 +490,11 @@ class CppConstructor(ForwardWrapperBase): "virtual method implementation; do not call" if class_ is None: class_ = self._class + + if self.throw: + self.before_call.write_code('try\n{') + self.before_call.indent() + #assert isinstance(class_, CppClass) if class_.helper_class is None: class_.write_create_instance(self.before_call, "self->obj", ", ".join(self.call_params)) @@ -516,6 +529,17 @@ class CppConstructor(ForwardWrapperBase): self.before_call.unindent() self.before_call.write_code("}") + if self.throw: + for exc in self.throw: + self.before_call.unindent() + self.before_call.write_code('} catch (%s const &exc) {' % exc.full_name) + self.before_call.indent() + self.before_call.write_cleanup() + self.before_call.write_code('PyErr_SetNone((PyObject *) %s);\nreturn -1;' % exc.pytypestruct) + self.before_call.unindent() + self.before_call.write_code('}') + + def _before_return_hook(self): "hook that post-processes parameters and check for custodian= CppClass parameters" cppclass_typehandlers.implement_parameter_custodians(self) diff --git a/tests/foo.cc b/tests/foo.cc index 5168853..da44342 100644 --- a/tests/foo.cc +++ b/tests/foo.cc @@ -458,3 +458,14 @@ ClassThatThrows::my_inverse_method (double x) throw (DomainError) return 1/x; } + +ClassThatThrows::ClassThatThrows (double x) throw (DomainError) +{ + if (x == 0) + { + DomainError ex; + ex.message = "value must be != 0"; + throw ex; + } +} + diff --git a/tests/foo.h b/tests/foo.h index 43d9853..cd3b087 100644 --- a/tests/foo.h +++ b/tests/foo.h @@ -1046,8 +1046,11 @@ struct DomainError : public Error // returns 1/x, raises DomainError if x == 0 double my_inverse_func (double x) throw (DomainError); -struct ClassThatThrows +class ClassThatThrows { +public: + ClassThatThrows (double x) throw (DomainError); + // returns 1/x, raises DomainError if x == 0 double my_inverse_method (double x) throw (DomainError); }; diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index b2b6f44..7a2baf1 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -500,7 +500,8 @@ int %s::custom_method_added_by_a_hook(int x) mod.add_function('my_inverse_func', 'double', [Parameter.new('double', 'x')], throw=[DomainError]) - ClassThatThrows = mod.add_struct('ClassThatThrows') + ClassThatThrows = mod.add_class('ClassThatThrows') + ClassThatThrows.add_constructor([Parameter.new('double', 'x')], throw=[DomainError]) ClassThatThrows.add_method('my_inverse_method', 'double', [Parameter.new('double', 'x')], throw=[DomainError]) diff --git a/tests/footest.py b/tests/footest.py index 60a4b33..eb6fcbc 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -1167,10 +1167,14 @@ class TestFoo(unittest.TestCase): self.assertRaises(foo.DomainError, foo.my_inverse_func, 0) def test_method_exception(self): - c = foo.ClassThatThrows() + c = foo.ClassThatThrows(1) y = c.my_inverse_method(2) self.assertEqual(y, 0.5) self.assertRaises(foo.DomainError, c.my_inverse_method, 0) + def test_constructor_exceptions(self): + c = foo.ClassThatThrows(1) + self.assertRaises(foo.DomainError, foo.ClassThatThrows, 0) + if __name__ == '__main__': unittest.main()