From 9b0d3a46abfcbf93ee8cf20f9fa0d69174af69b6 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Thu, 27 Aug 2009 14:28:35 +0100 Subject: [PATCH] Add code to allow translation of the exception message into python --- pybindgen/cppexception.py | 19 +++++++++++++++++-- pybindgen/cppmethod.py | 6 ++++-- pybindgen/function.py | 3 ++- tests/foomodulegen-auto-split.py | 2 +- tests/foomodulegen-auto.py | 2 +- tests/foomodulegen.py | 2 +- tests/foomodulegen3.py | 2 +- tests/foomodulegen4.py | 2 +- tests/footest.py | 10 ++++++++++ 9 files changed, 38 insertions(+), 10 deletions(-) diff --git a/pybindgen/cppexception.py b/pybindgen/cppexception.py index ec5820f..ce79386 100644 --- a/pybindgen/cppexception.py +++ b/pybindgen/cppexception.py @@ -6,8 +6,7 @@ import utils class CppException(object): def __init__(self, name, parent=None, outer_class=None, custom_name=None, - foreign_cpp_namespace=None - ): + foreign_cpp_namespace=None, message_rvalue=None): """ @param name: exception class name @param parent: optional parent class wrapper @@ -23,6 +22,13 @@ class CppException(object): instance, this can be useful to wrap std classes, like std::ofstream, without having to create an extra python submodule. + + @param message_rvalue: if not None, this parameter is a string + that contains an rvalue C expression that evaluates to the + exception message. The Python % operator will be used to + substitute %(EXC)s for the caught exception variable name. The + rvalue expression must return a string of type "char const*", + a pointer owned by the exception instance. """ self.name = name self.full_name = None @@ -34,9 +40,18 @@ class CppException(object): self.mangled_full_name = None self.pytypestruct = None self.foreign_cpp_namespace = foreign_cpp_namespace + self.message_rvalue = message_rvalue + def __repr__(self): return "" % self.full_name + + def write_convert_to_python(self, code_block, variable_name): + if self.message_rvalue is None: + code_block.write_code('PyErr_SetNone((PyObject *) %s);' % self.pytypestruct) + else: + code_block.write_code('PyErr_SetString((PyObject *) %s, %s);' + % (self.pytypestruct, (self.message_rvalue % dict(EXC=variable_name)))) def get_module(self): """Get the Module object this type belongs to""" diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index 318e49d..dd653e2 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -224,7 +224,8 @@ class CppMethod(ForwardWrapperBase): 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 NULL;' % exc.pytypestruct) + exc.write_convert_to_python(self.before_call, 'exc') + self.before_call.write_code('return NULL;') self.before_call.unindent() self.before_call.write_code('}') @@ -535,7 +536,8 @@ class CppConstructor(ForwardWrapperBase): 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) + exc.write_convert_to_python(self.before_call, 'exc') + self.before_call.write_code('return -1;') self.before_call.unindent() self.before_call.write_code('}') diff --git a/pybindgen/function.py b/pybindgen/function.py index 3d1b057..0925089 100644 --- a/pybindgen/function.py +++ b/pybindgen/function.py @@ -146,7 +146,8 @@ class Function(ForwardWrapperBase): 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 NULL;' % exc.pytypestruct) + exc.write_convert_to_python(self.before_call, 'exc') + self.before_call.write_code('return NULL;') self.before_call.unindent() self.before_call.write_code('}') diff --git a/tests/foomodulegen-auto-split.py b/tests/foomodulegen-auto-split.py index 78b1da8..a2b5394 100644 --- a/tests/foomodulegen-auto-split.py +++ b/tests/foomodulegen-auto-split.py @@ -40,7 +40,7 @@ def my_module_gen(): 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.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()') module_parser.scan_types() module_parser.scan_methods() module_parser.scan_functions() diff --git a/tests/foomodulegen-auto.py b/tests/foomodulegen-auto.py index 86c9bba..17a2895 100644 --- a/tests/foomodulegen-auto.py +++ b/tests/foomodulegen-auto.py @@ -27,7 +27,7 @@ def my_module_gen(): 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.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()') module_parser.scan_types() module_parser.scan_methods() module_parser.scan_functions() diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index d5fbd01..d202adf 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -505,7 +505,7 @@ 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') + std_exception = mod.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()') 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')], diff --git a/tests/foomodulegen3.py b/tests/foomodulegen3.py index 9384ebd..b58767b 100644 --- a/tests/foomodulegen3.py +++ b/tests/foomodulegen3.py @@ -23,7 +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') + root_module.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()') ## 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 9d91b53..a6f0862 100644 --- a/tests/foomodulegen4.py +++ b/tests/foomodulegen4.py @@ -50,7 +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') + root_module.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()') foomodulegen_split.register_types(root_module) foomodulegen_split.register_methods(root_module) diff --git a/tests/footest.py b/tests/footest.py index 1d1c699..58b05c7 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -1181,12 +1181,22 @@ class TestFoo(unittest.TestCase): y = foo.my_inverse_func2(2) self.assertEqual(y, 0.5) self.assertRaises(foo.exception, foo.my_inverse_func2, 0) + try: + foo.my_inverse_func2(0) + except foo.exception, ex: + msg = str(ex) + self.assertEqual(msg, "value must be != 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) + try: + c.my_inverse_method2(0) + except foo.exception, ex: + msg = str(ex) + self.assertEqual(msg, "value must be != 0") if __name__ == '__main__': unittest.main()