Debian Codegraph @

tests/foomodulegen_common.py

raw symbols SCIP
1 #! /usr/bin/env python
2 from __future__ import unicode_literals, print_function
3
4 import sys
5 import re
6
7 import pybindgen
8 import pybindgen.settings
9 pybindgen.settings.deprecated_virtuals = False
10
11 from pybindgen.typehandlers import base as typehandlers
12 from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
13 from pybindgen import CppMethod, CppConstructor, CppClass, Enum
14 from pybindgen.function import CustomFunctionWrapper
15 from pybindgen.cppmethod import CustomCppMethodWrapper
16
17
18
19 class PointerHolderTransformation(typehandlers.TypeTransformation):
20 def __init__(self):
21 self.rx = re.compile(r'(?:::)?PointerHolder<\s*(\w+)\s*>')
22
23 def get_untransformed_name(self, name):
24 m = self.rx.match(name)
25 if m is None:
26 return None
27 else:
28 return m.group(1)+' *'
29
30 def create_type_handler(self, type_handler, *args, **kwargs):
31 if issubclass(type_handler, Parameter):
32 kwargs['transfer_ownership'] = False
33 elif issubclass(type_handler, ReturnValue):
34 kwargs['caller_owns_return'] = True
35 else:
36 raise AssertionError
37 handler = type_handler(*args, **kwargs)
38 handler.set_transformation(self, self.get_untransformed_name(args[0]))
39 return handler
40
41 def untransform(self, type_handler, declarations, code_block, expression):
42 return '(%s).thePointer' % (expression,)
43
44 def transform(self, type_handler, declarations, code_block, expression):
45 assert type_handler.untransformed_ctype[-1] == '*'
46 var = declarations.declare_variable(
47 'PointerHolder<%s>' % type_handler.untransformed_ctype[:-1], 'tmp')
48 return '(%s.thePointer = (%s), %s)' % (var, expression, var)
49
50 transf = PointerHolderTransformation()
51 typehandlers.return_type_matcher.register_transformation(transf)
52 typehandlers.param_type_matcher.register_transformation(transf)
53 del transf
54
55 def customize_module_pre(module):
56 standard_error = module.add_exception('out_of_range', foreign_cpp_namespace='std',
57 custom_name='IndexError', is_standard_error=True)
58
59
60 def customize_module(module):
61 pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry
62
63 wrapper_body = '''
64 static PyObject *
65 _wrap_foofunction_that_takes_foo_from_string(PyObject * PYBINDGEN_UNUSED(dummy), PyObject *args,
66 PyObject *kwargs, PyObject **return_exception)
67 {
68 PyObject *py_retval;
69 char *datum;
70 const char *keywords[] = {"foo", NULL};
71
72 if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s", (char **) keywords, &datum)) {
73 {
74 PyObject *exc_type, *traceback;
75 PyErr_Fetch(&exc_type, return_exception, &traceback);
76 Py_XDECREF(exc_type);
77 Py_XDECREF(traceback);
78 }
79 return NULL;
80 }
81 function_that_takes_foo(Foo(datum));
82 Py_INCREF(Py_None);
83 py_retval = Py_None;
84 return py_retval;
85 }
86 '''
87 module.add_custom_function_wrapper('function_that_takes_foo',
88 '_wrap_foofunction_that_takes_foo_from_string',
89 wrapper_body,
90 docstring="I'm awake you rascals!")
91
92
93 ## test a custom method wrapper
94 Bar, = [cls for cls in module.classes if cls.name == 'Bar']
95 wrapper_body = '''
96 static PyObject *
97 _wrap_PyBar_Hooray_lenx(PyBar *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs,
98 PyObject **return_exception)
99 {
100 PyObject *py_retval;
101 int x;
102 const char *keywords[] = {"x", NULL};
103
104 if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "i", (char **) keywords, &x)) {
105 PyObject *exc_type, *traceback;
106 PyErr_Fetch(&exc_type, return_exception, &traceback);
107 Py_XDECREF(exc_type);
108 Py_XDECREF(traceback);
109 return NULL;
110 }
111
112 std::string retval;
113 retval = Bar::Hooray();
114 py_retval = Py_BuildValue((char *) "i", int(retval.size() + x));
115 return py_retval;
116 }
117 '''
118 Bar.add_custom_method_wrapper("Hooray", "_wrap_PyBar_Hooray_lenx",
119 wrapper_body,
120 flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"],
121 docstring='Zzzz.... Have good dreams.')
122
123
124 Foo, = [cls for cls in module.classes if cls.name == 'Foo']
125 def Foo_instance_creation_function(dummy_cpp_class, code_block, lvalue,
126 parameters, construct_type_name):
127 code_block.write_code(
128 "%s = new %s(%s);" % (lvalue, construct_type_name, parameters))
129 code_block.write_code("%s->initialize();" % (lvalue,))
130 Foo.set_instance_creation_function(Foo_instance_creation_function)
131
132 VectorLike2, = [cls for cls in module.classes if cls.name == 'VectorLike2']
133 VectorLike2.add_container_traits(ReturnValue.new('double'), begin_method='Begin', end_method='End', iterator_type='Iterator')
134
135 MapLike, = [cls for cls in module.classes if cls.name == 'MapLike']
136 MapLike.add_container_traits((ReturnValue.new('int'), ReturnValue.new('double')),
137 begin_method='Begin', end_method='End', iterator_type='Iterator',
138 is_mapping=True)
139
140 # just a compilation test, this won't actually work in runtime
141 #module.add_include('<stdio.h>')
142 #module.add_class(name="FILE", foreign_cpp_namespace="", import_from_module="__builtin__ named file")
143 #module.add_enum("reg_errcode_t", ["REG_NOERROR", "REG_NOMATCH"], import_from_module="__builtin__")
144