diff --git a/examples/foomodulegen-auto.py b/examples/foomodulegen-auto.py index d92c444..d98c8b7 100644 --- a/examples/foomodulegen-auto.py +++ b/examples/foomodulegen-auto.py @@ -9,6 +9,7 @@ from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink) from pybindgen import (CppMethod, CppConstructor, CppClass, Enum) from pybindgen.gccxmlparser import ModuleParser from pybindgen.function import CustomFunctionWrapper +from pybindgen.cppmethod import CustomCppMethodWrapper @@ -84,6 +85,36 @@ _wrap_foofunction_that_takes_foo_from_string(PyObject * PYBINDGEN_UNUSED(dummy), '_wrap_foofunction_that_takes_foo_from_string', wrapper_body)) + + ## test a custom method wrapper + Bar, = [cls for cls in module.classes if cls.name == 'Bar'] + wrapper_body = ''' +static PyObject * +_wrap_PyBar_Hooray_lenx(PyBar *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, + PyObject **return_exception) +{ + PyObject *py_retval; + int x; + const char *keywords[] = {"x", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", (char **) keywords, &x)) { + PyObject *exc_type, *traceback; + PyErr_Fetch(&exc_type, return_exception, &traceback); + Py_XDECREF(exc_type); + Py_XDECREF(traceback); + return NULL; + } + + std::string retval; + retval = Bar::Hooray(); + py_retval = Py_BuildValue("i", int(retval.size() + x)); + return py_retval; +} +''' + Bar.add_method(CustomCppMethodWrapper("Hooray", "_wrap_PyBar_Hooray_lenx", + wrapper_body, + flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])) + module.generate(out) diff --git a/examples/foomodulegen.py b/examples/foomodulegen.py index 850c625..78f4379 100755 --- a/examples/foomodulegen.py +++ b/examples/foomodulegen.py @@ -9,6 +9,7 @@ from pybindgen.typehandlers import base as typehandlers from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink) from pybindgen import (CppMethod, CppConstructor, CppClass, Enum) from pybindgen.function import CustomFunctionWrapper +from pybindgen.cppmethod import CustomCppMethodWrapper @@ -383,6 +384,7 @@ def my_module_gen(out_file): pybindgen.settings.error_handler = MyErrorHandler() + ## test a custom function wrapper wrapper_body = ''' static PyObject * _wrap_foofunction_that_takes_foo_from_string(PyObject * PYBINDGEN_UNUSED(dummy), PyObject *args, @@ -410,6 +412,34 @@ _wrap_foofunction_that_takes_foo_from_string(PyObject * PYBINDGEN_UNUSED(dummy), '_wrap_foofunction_that_takes_foo_from_string', wrapper_body)) + ## test a custom method wrapper + wrapper_body = ''' +static PyObject * +_wrap_PyBar_Hooray_lenx(PyBar *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs, + PyObject **return_exception) +{ + PyObject *py_retval; + int x; + const char *keywords[] = {"x", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", (char **) keywords, &x)) { + PyObject *exc_type, *traceback; + PyErr_Fetch(&exc_type, return_exception, &traceback); + Py_XDECREF(exc_type); + Py_XDECREF(traceback); + return NULL; + } + + std::string retval; + retval = Bar::Hooray(); + py_retval = Py_BuildValue("i", int(retval.size() + x)); + return py_retval; +} +''' + Bar.add_method(CustomCppMethodWrapper("Hooray", "_wrap_PyBar_Hooray_lenx", + wrapper_body, + flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])) + ## ---- finally, generate the whole thing ---- mod.generate(FileCodeSink(out_file)) diff --git a/examples/footest.py b/examples/footest.py index 36c73bf..8ac817a 100644 --- a/examples/footest.py +++ b/examples/footest.py @@ -650,5 +650,15 @@ class TestFoo(unittest.TestCase): foo1 = foo.function_that_returns_foo() self.assertEqual(foo1.get_datum(), "yellow") + def test_custom_method_wrapper(self): + v1 = foo.Bar.Hooray() + self.assertEqual(v1, "Hooray!") + try: + v2 = foo.Bar.Hooray(123) # this one is a fake method + except TypeError: + self.fail() + else: + self.assertEqual(v2, len("Hooray!") + 123) + if __name__ == '__main__': unittest.main() diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index b93db87..f047cb2 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -4,7 +4,8 @@ Wrap C++ class methods and constructods. from copy import copy -from typehandlers.base import ForwardWrapperBase, ReverseWrapperBase, join_ctype_and_name, CodeGenerationError +from typehandlers.base import ForwardWrapperBase, ReverseWrapperBase, \ + join_ctype_and_name, CodeGenerationError, ReturnValue from typehandlers import codesink import overloading import settings @@ -619,4 +620,31 @@ Py_FatalError("Error detected, but parent virtual is pure virtual or private vir decl_post_modifiers=decl_post_modifiers) + +class CustomCppMethodWrapper(CppMethod): + """ + Adds a custom method wrapper. The custom wrapper must be + prepared to support overloading, i.e. it must have an additional + "PyObject **return_exception" parameter, and raised exceptions + must be returned by this parameter. + """ + + NEEDS_OVERLOADING_INTERFACE = True + + def __init__(self, method_name, wrapper_name, wrapper_body, + flags=('METH_VARARGS', 'METH_KEYWORDS')): + super(CustomCppMethodWrapper, self).__init__(ReturnValue.new('void'), method_name, []) + self.wrapper_base_name = wrapper_name + self.wrapper_actual_name = wrapper_name + self.meth_flags = list(flags) + self.wrapper_body = wrapper_body + + def generate(self, code_sink, dummy_wrapper_name=None, extra_wrapper_params=()): + assert extra_wrapper_params == ["PyObject **return_exception"] + code_sink.writeln(self.wrapper_body) + + def generate_call(self, *args, **kwargs): + pass + + import cppclass diff --git a/pybindgen/function.py b/pybindgen/function.py index 6b55b46..e755c23 100644 --- a/pybindgen/function.py +++ b/pybindgen/function.py @@ -146,7 +146,7 @@ class Function(ForwardWrapperBase): class CustomFunctionWrapper(Function): """ Adds a custom function wrapper. The custom wrapper must be - prepared to support overloading, i.e. it must have an additoional + prepared to support overloading, i.e. it must have an additional "PyObject **return_exception" parameter, and raised exceptions must be returned by this parameter. """ @@ -164,7 +164,6 @@ class CustomFunctionWrapper(Function): def generate(self, code_sink, dummy_wrapper_name=None, extra_wrapper_params=()): assert extra_wrapper_params == ["PyObject **return_exception"] code_sink.writeln(self.wrapper_body) - return self.wrapper_base_name def generate_call(self, *args, **kwargs): pass