| 1 |
|
| 2 |
from __future__ import unicode_literals, print_function |
| 3 |
|
| 4 |
import sys, os |
| 5 |
|
| 6 |
import pybindgen |
| 7 |
from pybindgen import typehandlers |
| 8 |
from pybindgen.typehandlers import codesink |
| 9 |
from pybindgen.typehandlers.base import Parameter, ReturnValue |
| 10 |
from pybindgen.function import Function |
| 11 |
from pybindgen.module import Module |
| 12 |
from pybindgen import cppclass |
| 13 |
from pybindgen.typehandlers.base import NotSupportedError |
| 14 |
|
| 15 |
import pybindgen.settings |
| 16 |
pybindgen.settings.deprecated_virtuals = False |
| 17 |
|
| 18 |
import re |
| 19 |
|
| 20 |
stdint_rx = re.compile(".*int\\d+_t.*") |
| 21 |
|
| 22 |
class MyReverseWrapper(typehandlers.base.ReverseWrapperBase): |
| 23 |
def generate_python_call(self): |
| 24 |
params = ['NULL'] |
| 25 |
build_params = self.build_params.get_parameters() |
| 26 |
if build_params[0][0] == '"': |
| 27 |
build_params[0] = '(char *) ' + build_params[0] |
| 28 |
params.extend(build_params) |
| 29 |
self.before_call.write_code('py_retval = PyObject_CallFunction(%s);' % (', '.join(params),)) |
| 30 |
self.before_call.write_error_check('py_retval == NULL') |
| 31 |
self.before_call.add_cleanup_code('Py_DECREF(py_retval);') |
| 32 |
|
| 33 |
|
| 34 |
def type_blacklisted(type_name): |
| 35 |
if type_name.startswith('Glib::'): |
| 36 |
return True |
| 37 |
return False |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
def test(): |
| 42 |
code_out = codesink.FileCodeSink(sys.stdout) |
| 43 |
pybindgen.write_preamble(code_out) |
| 44 |
sys.stdout.write(""" |
| 45 |
#include <string> |
| 46 |
#include <stdint.h> |
| 47 |
""") |
| 48 |
|
| 49 |
sys.stdout.write(''' |
| 50 |
class Foo |
| 51 |
{ |
| 52 |
std::string m_datum; |
| 53 |
public: |
| 54 |
Foo () : m_datum ("") |
| 55 |
{} |
| 56 |
Foo (std::string datum) : m_datum (datum) |
| 57 |
{} |
| 58 |
std::string get_datum () const { return m_datum; } |
| 59 |
|
| 60 |
Foo (Foo const & other) : m_datum (other.get_datum ()) |
| 61 |
{} |
| 62 |
}; |
| 63 |
''') |
| 64 |
|
| 65 |
module = Module("foo") |
| 66 |
|
| 67 |
|
| 68 |
Foo = module.add_class('Foo') |
| 69 |
Foo.add_constructor([Parameter.new("const Foo&", "foo")]) |
| 70 |
|
| 71 |
Foo.generate_forward_declarations(code_out, module) |
| 72 |
|
| 73 |
wrapper_number = 0 |
| 74 |
|
| 75 |
|
| 76 |
for return_type, return_handler in list(typehandlers.base.return_type_matcher.items()): |
| 77 |
if type_blacklisted(return_type): |
| 78 |
continue |
| 79 |
if os.name == 'nt': |
| 80 |
if stdint_rx.search(return_type): |
| 81 |
continue |
| 82 |
|
| 83 |
code_out.writeln("/* Test %s (%s) return type */" % (return_type, return_handler)) |
| 84 |
if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, |
| 85 |
typehandlers.pyobjecttype.PyObjectReturnValue)): |
| 86 |
for caller_owns_return in True, False: |
| 87 |
retval = return_handler(return_type, caller_owns_return=caller_owns_return) |
| 88 |
wrapper = MyReverseWrapper(retval, []) |
| 89 |
wrapper_number += 1 |
| 90 |
try: |
| 91 |
wrapper.generate(code_out, |
| 92 |
'_test_wrapper_number_%i' % (wrapper_number,), |
| 93 |
['static']) |
| 94 |
except NotImplementedError: |
| 95 |
sys.stderr.write("ReverseWrapper %s(void) (caller_owns_return=%r)" |
| 96 |
" could not be generated: not implemented\n" |
| 97 |
% (retval.ctype, caller_owns_return)) |
| 98 |
sys.stdout.write("\n") |
| 99 |
else: |
| 100 |
retval = return_handler(return_type) |
| 101 |
try: |
| 102 |
wrapper = MyReverseWrapper(retval, []) |
| 103 |
except NotSupportedError: |
| 104 |
continue |
| 105 |
except NotImplementedError: |
| 106 |
sys.stderr.write("ReverseWrapper %s(void) could not be generated: not implemented\n" |
| 107 |
% (retval.ctype)) |
| 108 |
continue |
| 109 |
wrapper_number += 1 |
| 110 |
memsink = codesink.MemoryCodeSink() |
| 111 |
try: |
| 112 |
wrapper.generate(memsink, |
| 113 |
'_test_wrapper_number_%i' % (wrapper_number,), |
| 114 |
['static']) |
| 115 |
except NotImplementedError: |
| 116 |
sys.stderr.write("ReverseWrapper %s xxx (void) could not be generated: not implemented\n" |
| 117 |
% (retval.ctype,)) |
| 118 |
continue |
| 119 |
except NotSupportedError: |
| 120 |
continue |
| 121 |
else: |
| 122 |
memsink.flush_to(code_out) |
| 123 |
sys.stdout.write("\n") |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
for param_type, param_handler in list(typehandlers.base.param_type_matcher.items()): |
| 128 |
if type_blacklisted(param_type): |
| 129 |
continue |
| 130 |
if os.name == 'nt': |
| 131 |
if stdint_rx.search(param_type): |
| 132 |
continue |
| 133 |
for direction in param_handler.DIRECTIONS: |
| 134 |
if direction == (Parameter.DIRECTION_IN): |
| 135 |
param_name = 'param' |
| 136 |
elif direction == (Parameter.DIRECTION_IN|Parameter.DIRECTION_OUT): |
| 137 |
param_name = 'param_inout' |
| 138 |
elif direction == (Parameter.DIRECTION_OUT): |
| 139 |
param_name = 'param_out' |
| 140 |
|
| 141 |
def try_wrapper(param, wrapper_number): |
| 142 |
if 'const' in param.ctype and direction&Parameter.DIRECTION_OUT: |
| 143 |
return |
| 144 |
code_out.writeln("/* Test %s (%s) param type */" % (param_type, param_handler)) |
| 145 |
wrapper = MyReverseWrapper(ReturnValue.new('void'), [param]) |
| 146 |
try: |
| 147 |
wrapper.generate(code_out, |
| 148 |
'_test_wrapper_number_%i' % (wrapper_number,), |
| 149 |
['static']) |
| 150 |
except NotImplementedError: |
| 151 |
sys.stderr.write("ReverseWrapper void(%s) could not be generated: not implemented" |
| 152 |
% (param.ctype)) |
| 153 |
sys.stdout.write("\n") |
| 154 |
|
| 155 |
if issubclass(param_handler, (cppclass.CppClassPtrParameter, |
| 156 |
typehandlers.pyobjecttype.PyObjectParam)): |
| 157 |
for transfer_ownership in True, False: |
| 158 |
try: |
| 159 |
param = param_handler(param_type, param_name, transfer_ownership=transfer_ownership) |
| 160 |
except TypeError: |
| 161 |
sys.stderr.write("ERROR -----> param_handler(param_type=%r, " |
| 162 |
"transfer_ownership=%r, is_const=%r)\n" |
| 163 |
% (param_type, transfer_ownership, is_const)) |
| 164 |
wrapper_number += 1 |
| 165 |
try_wrapper(param, wrapper_number) |
| 166 |
else: |
| 167 |
param = param_handler(param_type, param_name, direction) |
| 168 |
wrapper_number += 1 |
| 169 |
try_wrapper(param, wrapper_number) |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
for return_type, return_handler in list(typehandlers.base.return_type_matcher.items()): |
| 175 |
if type_blacklisted(return_type): |
| 176 |
continue |
| 177 |
if os.name == 'nt': |
| 178 |
if stdint_rx.search(return_type): |
| 179 |
continue |
| 180 |
wrapper_number += 1 |
| 181 |
function_name = 'foo_function_%i' % (wrapper_number,) |
| 182 |
|
| 183 |
sys.stdout.write("%s %s(void);\n\n" % (return_type, function_name)) |
| 184 |
|
| 185 |
if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, |
| 186 |
typehandlers.pyobjecttype.PyObjectReturnValue)): |
| 187 |
retval = return_handler(return_type, caller_owns_return=True) |
| 188 |
else: |
| 189 |
retval = return_handler(return_type) |
| 190 |
|
| 191 |
module.add_function(function_name, retval, []) |
| 192 |
|
| 193 |
for param_type, param_handler in list(typehandlers.base.param_type_matcher.items()): |
| 194 |
if type_blacklisted(param_type): |
| 195 |
continue |
| 196 |
if os.name == 'nt': |
| 197 |
if stdint_rx.search(param_type): |
| 198 |
continue |
| 199 |
|
| 200 |
for is_const in [True, False]: |
| 201 |
for direction in param_handler.DIRECTIONS: |
| 202 |
if direction == (Parameter.DIRECTION_IN): |
| 203 |
param_name = 'param' |
| 204 |
elif direction == (Parameter.DIRECTION_IN|Parameter.DIRECTION_OUT): |
| 205 |
param_name = 'param_inout' |
| 206 |
elif direction == (Parameter.DIRECTION_OUT): |
| 207 |
param_name = 'param_out' |
| 208 |
|
| 209 |
if is_const and direction & Parameter.DIRECTION_OUT: |
| 210 |
continue |
| 211 |
|
| 212 |
if is_const: |
| 213 |
if '&' in param_type: |
| 214 |
continue |
| 215 |
param_type_with_const = "const %s" % (param_type,) |
| 216 |
else: |
| 217 |
param_type_with_const = param_type |
| 218 |
|
| 219 |
if issubclass(param_handler, (cppclass.CppClassPtrParameter, |
| 220 |
typehandlers.pyobjecttype.PyObjectParam)): |
| 221 |
for transfer_ownership in True, False: |
| 222 |
name = param_name + (transfer_ownership and '_transfer' or '_notransfer') |
| 223 |
try: |
| 224 |
param = param_handler(param_type, name, transfer_ownership=transfer_ownership) |
| 225 |
except TypeError: |
| 226 |
sys.stderr.write("ERROR -----> param_handler(param_type=%r, " |
| 227 |
"name=%r, transfer_ownership=%r, is_const=%r)\n" |
| 228 |
% (param_type, name, transfer_ownership, is_const)) |
| 229 |
wrapper_number += 1 |
| 230 |
function_name = 'foo_function_%i' % (wrapper_number,) |
| 231 |
|
| 232 |
sys.stdout.write("void %s(%s %s);\n\n" % (function_name, param_type_with_const, name)) |
| 233 |
module.add_function(function_name, ReturnValue.new('void'), [param]) |
| 234 |
else: |
| 235 |
param = param_handler(param_type, param_name, direction) |
| 236 |
wrapper_number += 1 |
| 237 |
function_name = 'foo_function_%i' % (wrapper_number,) |
| 238 |
|
| 239 |
sys.stdout.write("void %s(%s);\n\n" % (function_name, param_type_with_const)) |
| 240 |
module.add_function(function_name, ReturnValue.new('void'), [param]) |
| 241 |
|
| 242 |
module.generate(code_out) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
if __name__ == '__main__': |
| 247 |
test() |