diff --git a/pybindgen/cppexception.py b/pybindgen/cppexception.py index d296d43..ec5820f 100644 --- a/pybindgen/cppexception.py +++ b/pybindgen/cppexception.py @@ -5,7 +5,25 @@ import utils class CppException(object): - def __init__(self, name, parent=None, outer_class=None, custom_name=None): + def __init__(self, name, parent=None, outer_class=None, custom_name=None, + foreign_cpp_namespace=None + ): + """ + @param name: exception class name + @param parent: optional parent class wrapper + + @param custom_name: an alternative name to give to this exception class + at python-side; if omitted, the name of the class in the + python module will be the same name as the class in C++ (minus + namespace). + + @param foreign_cpp_namespace: if set, the class is assumed to + belong to the given C++ namespace, regardless of the C++ + namespace of the python module it will be added to. For + instance, this can be useful to wrap std classes, like + std::ofstream, without having to create an extra python + submodule. + """ self.name = name self.full_name = None self.parent = parent @@ -15,7 +33,8 @@ class CppException(object): self.mangled_name = None self.mangled_full_name = None self.pytypestruct = None - + self.foreign_cpp_namespace = foreign_cpp_namespace + def __repr__(self): return "" % self.full_name @@ -35,14 +54,18 @@ class CppException(object): prefix = settings.name_prefix.capitalize() if self.outer_class is None: - if self._module.cpp_namespace_prefix: - if self._module.cpp_namespace_prefix == '::': - self.full_name = '::' + self.name - else: - self.full_name = self._module.cpp_namespace_prefix + '::' + self.name + if self.foreign_cpp_namespace: + self.full_name = self.foreign_cpp_namespace + '::' + self.name else: - self.full_name = self.name + if self._module.cpp_namespace_prefix: + if self._module.cpp_namespace_prefix == '::': + self.full_name = '::' + self.name + else: + self.full_name = self._module.cpp_namespace_prefix + '::' + self.name + else: + self.full_name = self.name else: + assert not self.foreign_cpp_namespace self.full_name = '::'.join([self.outer_class.full_name, self.name]) def make_upper(s): @@ -62,7 +85,7 @@ class CppException(object): def _get_python_name(self): if self.custom_name is None: - class_python_name = self.mangled_name + class_python_name = self.name else: class_python_name = self.custom_name return class_python_name diff --git a/tests/foo.cc b/tests/foo.cc index da44342..5fa94d0 100644 --- a/tests/foo.cc +++ b/tests/foo.cc @@ -2,6 +2,7 @@ #include #include #include +#include int print_something(const char *message) { @@ -469,3 +470,23 @@ ClassThatThrows::ClassThatThrows (double x) throw (DomainError) } } + +double +my_inverse_func2 (double x) throw (std::exception) +{ + if (x == 0) + { + throw std::runtime_error ("value must be != 0"); + } + return 1/x; +} + +double +ClassThatThrows::my_inverse_method2 (double x) throw (std::exception) +{ + if (x == 0) + { + throw std::runtime_error ("value must be != 0"); + } + return 1/x; +} diff --git a/tests/foo.h b/tests/foo.h index 06f4314..51c1dfa 100644 --- a/tests/foo.h +++ b/tests/foo.h @@ -9,6 +9,7 @@ #include #include #include +#include // Deprecation warnings look ugly and confusing; better to just // disable them and change this macro when we want to specifically @@ -1047,6 +1048,7 @@ struct DomainError : public Error // returns 1/x, raises DomainError if x == 0 double my_inverse_func (double x) throw (DomainError); +double my_inverse_func2 (double x) throw (std::exception); class ClassThatThrows { @@ -1055,6 +1057,7 @@ public: // returns 1/x, raises DomainError if x == 0 double my_inverse_method (double x) throw (DomainError); + double my_inverse_method2 (double x) throw (std::exception); }; diff --git a/tests/foomodulegen-auto-split.py b/tests/foomodulegen-auto-split.py index 4f0d7b4..78b1da8 100644 --- a/tests/foomodulegen-auto-split.py +++ b/tests/foomodulegen-auto-split.py @@ -37,8 +37,15 @@ def my_module_gen(): include_paths=eval(sys.argv[2]), ) - module_parser.parse([sys.argv[1]], includes=['"foo.h"'], pygen_sink=pygen, pygen_classifier=MyPygenClassifier(), - gccxml_options=gccxml_options) + module_parser.parse_init([sys.argv[1]], includes=['"foo.h"'], pygen_sink=pygen, pygen_classifier=MyPygenClassifier(), + gccxml_options=gccxml_options) + module = module_parser.module + module.add_exception('exception', foreign_cpp_namespace='std') + module_parser.scan_types() + module_parser.scan_methods() + module_parser.scan_functions() + module_parser.parse_finalize() + for sect in pygen: sect.code_sink.file.close() diff --git a/tests/foomodulegen-auto.py b/tests/foomodulegen-auto.py index e2913e0..86c9bba 100644 --- a/tests/foomodulegen-auto.py +++ b/tests/foomodulegen-auto.py @@ -24,8 +24,15 @@ def my_module_gen(): include_paths=eval(sys.argv[2]), ) - module = module_parser.parse([sys.argv[1]], includes=['"foo.h"'], pygen_sink=FileCodeSink(pygen_file), - gccxml_options=gccxml_options) + module_parser.parse_init([sys.argv[1]], includes=['"foo.h"'], pygen_sink=FileCodeSink(pygen_file), + gccxml_options=gccxml_options) + module = module_parser.module + module.add_exception('exception', foreign_cpp_namespace='std') + module_parser.scan_types() + module_parser.scan_methods() + module_parser.scan_functions() + module_parser.parse_finalize() + pygen_file.close() foomodulegen_common.customize_module(module) diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index 7a2baf1..d5fbd01 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -505,6 +505,14 @@ int %s::custom_method_added_by_a_hook(int x) ClassThatThrows.add_method('my_inverse_method', 'double', [Parameter.new('double', 'x')], throw=[DomainError]) + std_exception = mod.add_exception('exception', foreign_cpp_namespace='std') + mod.add_function('my_inverse_func2', 'double', [Parameter.new('double', 'x')], + throw=[std_exception]) + ClassThatThrows.add_method('my_inverse_method2', 'double', [Parameter.new('double', 'x')], + throw=[std_exception]) + + + #### --- error handler --- class MyErrorHandler(pybindgen.settings.ErrorHandler): diff --git a/tests/foomodulegen3.py b/tests/foomodulegen3.py index 4327061..9384ebd 100644 --- a/tests/foomodulegen3.py +++ b/tests/foomodulegen3.py @@ -23,6 +23,7 @@ pybindgen.settings.error_handler = ErrorHandler() def my_module_gen(): out = FileCodeSink(sys.stdout) root_module = foomodulegen_generated.module_init() + root_module.add_exception('exception', foreign_cpp_namespace='std') ## this is a very special case when we want to change the name of ## the python module to allow parallel testing of the same basic diff --git a/tests/foomodulegen4.py b/tests/foomodulegen4.py index 68db8d3..9d91b53 100644 --- a/tests/foomodulegen4.py +++ b/tests/foomodulegen4.py @@ -50,6 +50,7 @@ class MyMultiSectionFactory(MultiSectionFactory): def my_module_gen(): out = MyMultiSectionFactory(sys.argv[1]) root_module = foomodulegen_split.module_init() + root_module.add_exception('exception', foreign_cpp_namespace='std') foomodulegen_split.register_types(root_module) foomodulegen_split.register_methods(root_module) diff --git a/tests/footest.py b/tests/footest.py index eb6fcbc..1d1c699 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -1176,5 +1176,17 @@ class TestFoo(unittest.TestCase): c = foo.ClassThatThrows(1) self.assertRaises(foo.DomainError, foo.ClassThatThrows, 0) + + def test_function_exception2(self): + y = foo.my_inverse_func2(2) + self.assertEqual(y, 0.5) + self.assertRaises(foo.exception, foo.my_inverse_func2, 0) + + def test_method_exception2(self): + c = foo.ClassThatThrows(1) + y = c.my_inverse_method2(2) + self.assertEqual(y, 0.5) + self.assertRaises(foo.exception, c.my_inverse_method2, 0) + if __name__ == '__main__': unittest.main()