diff --git a/examples/a/modulegen.py b/examples/a/modulegen.py index 4c6bf10..e2e9f05 100644 --- a/examples/a/modulegen.py +++ b/examples/a/modulegen.py @@ -11,9 +11,9 @@ def my_module_gen(out_file): mod = Module('a') mod.add_include('"a.h"') - mod.add_function(Function(ReturnValue.new('void'), 'ADoA', [])) - mod.add_function(Function(ReturnValue.new('void'), 'ADoB', [Parameter.new('uint32_t', 'b')])) - mod.add_function(Function(ReturnValue.new('uint32_t'), 'ADoC', [])) + mod.add_function(Function('ADoA', ReturnValue.new('void'), [])) + mod.add_function(Function('ADoB', ReturnValue.new('void'), [Parameter.new('uint32_t', 'b')])) + mod.add_function(Function('ADoC', ReturnValue.new('uint32_t'), [])) mod.generate(FileCodeSink(out_file) ) diff --git a/examples/b/modulegen.py b/examples/b/modulegen.py index fbcb145..b98cfd2 100644 --- a/examples/b/modulegen.py +++ b/examples/b/modulegen.py @@ -18,8 +18,8 @@ def my_module_gen(out_file): B.add_instance_attribute(ReturnValue.new('uint32_t'), 'b_a') B.add_instance_attribute(ReturnValue.new('uint32_t'), 'b_b') - mod.add_function(Function(ReturnValue.new('void'), 'BDoA', [Parameter.new('B', 'b')])) - mod.add_function(Function(ReturnValue.new('B'), 'BDoB', [])) + mod.add_function(Function('BDoA', ReturnValue.new('void'), [Parameter.new('B', 'b')])) + mod.add_function(Function('BDoB', ReturnValue.new('B'), [])) mod.generate(FileCodeSink(out_file) ) diff --git a/examples/d/modulegen.py b/examples/d/modulegen.py index bd7bdaf..c4b0e41 100644 --- a/examples/d/modulegen.py +++ b/examples/d/modulegen.py @@ -15,10 +15,10 @@ def my_module_gen(out_file): D = mod.add_class('D', free_function='DDestroy') D.add_instance_attribute(ReturnValue.new('bool'), 'd') - D.add_constructor(Function(ReturnValue.new("D*", caller_owns_return=True), "DCreate", [])) - mod.add_function(Function(ReturnValue.new('void'), 'DDoA', [Parameter.new('D*', 'd', transfer_ownership=False)])) - mod.add_function(Function(ReturnValue.new('void'), 'DDoB', [Parameter.new('D&', 'd', direction=Parameter.DIRECTION_IN)])) - mod.add_function(Function(ReturnValue.new('void'), 'DDoC', [Parameter.new('D&', 'd', + D.add_constructor(Function("DCreate", ReturnValue.new("D*", caller_owns_return=True), [])) + mod.add_function(Function('DDoA', ReturnValue.new('void'), [Parameter.new('D*', 'd', transfer_ownership=False)])) + mod.add_function(Function('DDoB', ReturnValue.new('void'), [Parameter.new('D&', 'd', direction=Parameter.DIRECTION_IN)])) + mod.add_function(Function('DDoC', ReturnValue.new('void'), [Parameter.new('D&', 'd', direction=Parameter.DIRECTION_IN, is_const=True)])) diff --git a/examples/e/modulegen.py b/examples/e/modulegen.py index f220dab..5d54af4 100644 --- a/examples/e/modulegen.py +++ b/examples/e/modulegen.py @@ -15,7 +15,7 @@ def my_module_gen(out_file): E = mod.add_class('E', decref_method='Unref', incref_method='Ref') if 1: - E.add_constructor(Function(ReturnValue.new("E*", caller_owns_return=True), "E::CreateWithRef", [])) + E.add_constructor(Function("E::CreateWithRef", ReturnValue.new("E*", caller_owns_return=True), [])) else: ## alternative: E.add_constructor(Function(ReturnValue.new("E*", caller_owns_return=False), "E::CreateWithoutRef", [])) diff --git a/examples/g/modulegen.py b/examples/g/modulegen.py index 411d93e..7cdc968 100644 --- a/examples/g/modulegen.py +++ b/examples/g/modulegen.py @@ -11,11 +11,11 @@ def my_module_gen(out_file): mod = Module('g') mod.add_include('"g.h"') - mod.add_function(Function(ReturnValue.new('void'), 'GDoA', [])) + mod.add_function(Function('GDoA', ReturnValue.new('void'), [])) G = mod.add_cpp_namespace("G") - G.add_function(Function(ReturnValue.new('void'), 'GDoB', [])) + G.add_function(Function('GDoB', ReturnValue.new('void'), [])) GInner = G.add_cpp_namespace("GInner") - GInner.add_function(Function(ReturnValue.new('void'), 'GDoC', [])) + GInner.add_function(Function('GDoC', ReturnValue.new('void'), [])) mod.generate(FileCodeSink(out_file)) diff --git a/pybindgen/function.py b/pybindgen/function.py index 9c8e821..b285d80 100644 --- a/pybindgen/function.py +++ b/pybindgen/function.py @@ -16,12 +16,14 @@ class Function(ForwardWrapperBase): Class that generates a wrapper to a C function. """ - def __init__(self, return_value, function_name, parameters, docstring=None, unblock_threads=None, + def __init__(self, function_name, return_value, parameters, docstring=None, unblock_threads=None, template_parameters=()): """ - return_value -- the function return value - function_name -- name of the C function - parameters -- the function parameters + @param function_name: name of the C function + @param return_value: the function return value + @type return_value: L{ReturnValue} + @param parameters: the function parameters + @type parameters: list of L{Parameter} """ if unblock_threads is None: unblock_threads = settings.unblock_threads @@ -31,6 +33,7 @@ class Function(ForwardWrapperBase): error_return="return NULL;", unblock_threads=unblock_threads) self._module = None + assert isinstance(function_name, str) self.function_name = function_name self.wrapper_base_name = None self.wrapper_actual_name = None @@ -44,8 +47,8 @@ class Function(ForwardWrapperBase): function wrapper clone contains copies of all parameters, so they can be modified at will. """ - func = Function(self.return_value, - self.function_name, + func = Function(self.function_name, + self.return_value, [copy(param) for param in self.parameters], docstring=self.docstring) func._module = self._module @@ -156,7 +159,7 @@ class CustomFunctionWrapper(Function): def __init__(self, function_name, wrapper_name, wrapper_body, flags=('METH_VARARGS', 'METH_KEYWORDS')): - super(CustomFunctionWrapper, self).__init__(ReturnValue.new('void'), function_name, []) + super(CustomFunctionWrapper, self).__init__(function_name, ReturnValue.new('void'), []) self.wrapper_base_name = wrapper_name self.wrapper_actual_name = wrapper_name self.meth_flags = list(flags) diff --git a/pybindgen/gccxmlparser.py b/pybindgen/gccxmlparser.py index 2850df8..f59ca74 100644 --- a/pybindgen/gccxmlparser.py +++ b/pybindgen/gccxmlparser.py @@ -1306,13 +1306,13 @@ class ModuleParser(object): #cpp_class = type_registry.find_class(of_class, (self.module_namespace_name or '::')) cpp_class = root_module[normalize_class_name(of_class, (self.module_namespace_name or '::'))] - function_wrapper = Function(return_type, fun.name, arguments) + function_wrapper = Function(fun.name, return_type, arguments) cpp_class.add_method(function_wrapper, name=as_method) function_wrapper.gccxml_definition = fun self.pygen_sink.writeln("root_module[%r].add_method(Function(%s), name=%r)" % (cpp_class.full_name, - ", ".join([return_type._pygen_repr, repr(fun.name), arglist_repr]), + ", ".join([repr(fun.name), return_type._pygen_repr, arglist_repr]), as_method)) continue @@ -1320,14 +1320,14 @@ class ModuleParser(object): if is_constructor_of is not None: #cpp_class = type_registry.find_class(is_constructor_of, (self.module_namespace_name or '::')) cpp_class = root_module[normalize_class_name(is_constructor_of, (self.module_namespace_name or '::'))] - function_wrapper = Function(return_type, fun.name, arguments) + function_wrapper = Function(fun.name, return_type, arguments) cpp_class.add_constructor(function_wrapper) function_wrapper.gccxml_definition = fun self.pygen_sink.writeln("root_module[%r].add_constructor(Function(%s))" % (cpp_class.full_name, - ", ".join([return_type._pygen_repr, repr(fun.name), arglist_repr]),)) + ", ".join([repr(fun.name), return_type._pygen_repr, arglist_repr]),)) continue @@ -1336,7 +1336,7 @@ class ModuleParser(object): if templates.is_instantiation(fun.demangled_name): kwargs['template_parameters'] = templates.args(fun.demangled_name) - func_wrapper = Function(return_type, fun.name, arguments, **kwargs) + func_wrapper = Function(fun.name, return_type, arguments, **kwargs) func_wrapper.gccxml_definition = fun module.add_function(func_wrapper, name=alt_name) @@ -1345,7 +1345,7 @@ class ModuleParser(object): else: _pygen_altname_arg = ', name=%r' % alt_name self.pygen_sink.writeln("module.add_function(Function(%s)%s)" % - (", ".join([return_type._pygen_repr, repr(fun.name), arglist_repr] + _pygen_kwargs(kwargs)), + (", ".join([repr(fun.name), return_type._pygen_repr, arglist_repr] + _pygen_kwargs(kwargs)), _pygen_altname_arg)) for hook in self._post_scan_hooks: diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index 7828a12..edc90bd 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -48,8 +48,9 @@ def my_module_gen(out_file): Bar.add_method(CppMethod(ReturnValue.new('std::string'), 'Hooray', [], is_static=True)) ## to test RTTI with a hidden subclass - mod.add_function(Function(ReturnValue.new('Foo*', caller_owns_return=True), - 'get_hidden_subclass_pointer', [])) + mod.add_function(Function('get_hidden_subclass_pointer', + ReturnValue.new('Foo*', caller_owns_return=True), + [])) ## Zbr is a reference counted class @@ -75,24 +76,24 @@ int %s::custom_method_added_by_a_hook(int x) is_virtual=True)) Zbr.add_static_attribute(ReturnValue.new('int'), 'instance_count') - mod.add_function(Function(ReturnValue.new('void'), 'store_zbr', + mod.add_function(Function('store_zbr', ReturnValue.new('void'), [Parameter.new('Zbr*', 'zbr', transfer_ownership=True)])) - mod.add_function(Function(ReturnValue.new('int'), 'invoke_zbr', + mod.add_function(Function('invoke_zbr', ReturnValue.new('int'), [Parameter.new('int', 'x')])) - mod.add_function(Function(ReturnValue.new('void'), 'delete_stored_zbr', [])) + mod.add_function(Function('delete_stored_zbr', ReturnValue.new('void'), [])) - mod.add_function(Function(ReturnValue.new('int'), 'print_something', + mod.add_function(Function('print_something', ReturnValue.new('int'), [Parameter.new('const char*', 'message')])) - mod.add_function(Function(ReturnValue.new('int'), 'print_something_else', + mod.add_function(Function('print_something_else', ReturnValue.new('int'), [Parameter.new('const char*', 'message2')])) ## test overloaded functions - mod.add_function(Function(ReturnValue.new('int'), 'get_int_from_string', + mod.add_function(Function('get_int_from_string', ReturnValue.new('int'), [Parameter.new('const char*', 'from_string'), Parameter.new('int', 'multiplier', default_value='1')]), name="get_int") - mod.add_function(Function(ReturnValue.new('int'), 'get_int_from_float', + mod.add_function(Function('get_int_from_float', ReturnValue.new('int'), [Parameter.new('double', 'from_float')]), name="get_int") @@ -150,21 +151,21 @@ int %s::custom_method_added_by_a_hook(int x) ## add a function that appears as a method of an object SomeObject.add_method( - Function(ReturnValue.new('std::string'), 'some_object_get_something_prefixed', + Function('some_object_get_something_prefixed', ReturnValue.new('std::string'), [Parameter.new('SomeObject*', 'obj', transfer_ownership=False, is_const=True), Parameter.new('std::string', 'something')]), name='get_something_prefixed') ## add a function that appears as a method of an object SomeObject.add_method( - Function(ReturnValue.new('std::string'), 'some_object_val_get_something_prefixed', + Function('some_object_val_get_something_prefixed', ReturnValue.new('std::string'), [Parameter.new('SomeObject', 'obj'), Parameter.new('std::string', 'something')]), name='val_get_something_prefixed') ## add a function that appears as a method of an object SomeObject.add_method( - Function(ReturnValue.new('std::string'), 'some_object_ref_get_something_prefixed', + Function('some_object_ref_get_something_prefixed', ReturnValue.new('std::string'), [Parameter.new('SomeObject&', 'obj', is_const=True), Parameter.new('std::string', 'something')]), name='ref_get_something_prefixed') @@ -209,20 +210,17 @@ int %s::custom_method_added_by_a_hook(int x) ReturnValue.new('void'), 'set_foobar_with_self_as_custodian', [Parameter.new('Foobar*', 'foobar', custodian=0)])) - mod.add_function(Function(ReturnValue.new('Foobar*', custodian=1), - 'get_foobar_with_other_as_custodian', + mod.add_function(Function('get_foobar_with_other_as_custodian', ReturnValue.new('Foobar*', custodian=1), [Parameter.new('SomeObject*', 'other', transfer_ownership=False)])) - mod.add_function(Function(ReturnValue.new('Foobar*', caller_owns_return=True), - 'create_new_foobar', [])) + mod.add_function(Function('create_new_foobar', ReturnValue.new('Foobar*', caller_owns_return=True), + [])) - mod.add_function(Function(ReturnValue.new('void'), - 'set_foobar_with_other_as_custodian', + mod.add_function(Function('set_foobar_with_other_as_custodian', ReturnValue.new('void'), [Parameter.new('Foobar*', 'foobar', custodian=2), Parameter.new('SomeObject*', 'other', transfer_ownership=False)])) - mod.add_function(Function(ReturnValue.new('SomeObject*', caller_owns_return=True), - 'set_foobar_with_return_as_custodian', + mod.add_function(Function('set_foobar_with_return_as_custodian', ReturnValue.new('SomeObject*', caller_owns_return=True), [Parameter.new('Foobar*', 'foobar', custodian=-1)])) @@ -254,31 +252,29 @@ int %s::custom_method_added_by_a_hook(int x) name="get_int") - mod.add_function(Function(ReturnValue.new('void'), 'store_some_object', + mod.add_function(Function('store_some_object', ReturnValue.new('void'), [Parameter.new('SomeObject*', 'obj', transfer_ownership=True)])) - mod.add_function(Function(ReturnValue.new('std::string'), 'invoke_some_object_get_prefix', + mod.add_function(Function('invoke_some_object_get_prefix', ReturnValue.new('std::string'), [])) - mod.add_function(Function(ReturnValue.new('SomeObject*', caller_owns_return=True), - 'take_some_object', [])) - mod.add_function(Function(ReturnValue.new('void'), 'delete_some_object', [])) + mod.add_function(Function('take_some_object', ReturnValue.new('SomeObject*', caller_owns_return=True), [])) + mod.add_function(Function('delete_some_object', ReturnValue.new('void'), [])) xpto = mod.add_cpp_namespace("xpto") - xpto.add_function(Function(ReturnValue.new('std::string'), 'some_function', [])) + xpto.add_function(Function('some_function', ReturnValue.new('std::string'), [])) ## enums.. xpto.add_enum('FooType', ['FOO_TYPE_AAA', 'FOO_TYPE_BBB', 'FOO_TYPE_CCC']) - xpto.add_function(Function(ReturnValue.new('FooType'), 'get_foo_type', [])) - xpto.add_function(Function(ReturnValue.new('void'), 'set_foo_type', [Parameter.new("FooType", 'type')])) + xpto.add_function(Function('get_foo_type', ReturnValue.new('FooType'), [])) + xpto.add_function(Function('set_foo_type', ReturnValue.new('void'), [Parameter.new("FooType", 'type')])) xpto_SomeClass = xpto.add_class('SomeClass') xpto_SomeClass.add_constructor(CppConstructor([])) ## ---- some implicity conversion APIs - mod.add_function(Function(ReturnValue.new('void'), - 'function_that_takes_foo', + mod.add_function(Function('function_that_takes_foo', ReturnValue.new('void'), [Parameter.new('Foo', 'foo')])) - mod.add_function(Function(ReturnValue.new('Foo'), 'function_that_returns_foo', [])) + mod.add_function(Function('function_that_returns_foo', ReturnValue.new('Foo'), [])) cls = mod.add_class('ClassThatTakesFoo') cls.add_constructor(CppConstructor([Parameter.new('Foo', 'foo')])) @@ -292,8 +288,7 @@ int %s::custom_method_added_by_a_hook(int x) ## A class that has no public default constructor... cls = mod.add_class('InterfaceId', is_singleton=True) ## A function that returns such a class... - mod.add_function(Function(ReturnValue.new('InterfaceId'), - 'make_interface_id', [])) + mod.add_function(Function('make_interface_id', ReturnValue.new('InterfaceId'), [])) ## A class the cannot be constructed; this will cause late CodeGenerationError's @@ -303,10 +298,12 @@ int %s::custom_method_added_by_a_hook(int x) 'get_value', [], is_static=True)) cls.add_method(CppMethod(ReturnValue.new('CannotBeConstructed*', caller_owns_return=True), 'get_ptr', [], is_static=True)) - mod.add_function(Function(ReturnValue.new('CannotBeConstructed'), - 'get_cannot_be_constructed_value', [])) - mod.add_function(Function(ReturnValue.new('CannotBeConstructed*', caller_owns_return=True), - 'get_cannot_be_constructed_ptr', [])) + mod.add_function(Function('get_cannot_be_constructed_value', + ReturnValue.new('CannotBeConstructed'), + [])) + mod.add_function(Function('get_cannot_be_constructed_ptr', + ReturnValue.new('CannotBeConstructed*', caller_owns_return=True), + [])) ## A nested class diff --git a/tests/test-generation.py b/tests/test-generation.py index c51356f..6839c2e 100755 --- a/tests/test-generation.py +++ b/tests/test-generation.py @@ -142,7 +142,7 @@ public: else: retval = return_handler(return_type) - wrapper = Function(retval, function_name, []) + wrapper = Function(function_name, retval, []) module.add_function(wrapper) for param_type, param_handler in typehandlers.base.param_type_matcher.items(): @@ -167,7 +167,7 @@ public: ## declare a fake prototype print "void %s(%s %s);" % (function_name, param_type, name) print - wrapper = Function(ReturnValue.new('void'), function_name, [param]) + wrapper = Function(function_name, ReturnValue.new('void'), [param]) module.add_function(wrapper) else: param = param_handler(param_type, param_name, direction) @@ -176,7 +176,7 @@ public: ## declare a fake prototype print "void %s(%s);" % (function_name, param_type) print - wrapper = Function(ReturnValue.new('void'), function_name, [param]) + wrapper = Function(function_name, ReturnValue.new('void'), [param]) module.add_function(wrapper) module.generate(code_out)