Add support for custom method wrappers.
This commit is contained in:
1 parent
ce56e9a994
commit
63c1e58915
5 files changed
+101
-3
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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()
|
||||
+29
-1
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user