diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 3ea9e4d..25e8b32 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -78,6 +78,7 @@ class CppHelperClass(object): def add_virtual_method(self, method): assert method.is_virtual + assert method.class_ is not None for existing in self.virtual_methods: if method.matches_signature(existing): @@ -91,11 +92,13 @@ class CppHelperClass(object): if not method.is_pure_virtual: if method.visibility in ['public', 'protected']: parent_caller = CppVirtualMethodParentCaller(method) - parent_caller.main_wrapper = method + #parent_caller.class_ = method.class_ + parent_caller.helper_class = self + parent_caller.main_wrapper = method # XXX: need to explain this self.add_virtual_parent_caller(parent_caller) proxy = CppVirtualMethodProxy(method) - proxy.main_wrapper = method + proxy.main_wrapper = method # XXX: need to explain this self.add_virtual_proxy(proxy) @@ -116,8 +119,6 @@ class CppHelperClass(object): overload.pystruct = self.class_.pystruct self.virtual_parent_callers[name] = overload assert self.class_ is not None - parent_caller.class_ = self.class_ - parent_caller.helper_class = self overload.add(parent_caller) def add_custom_method(self, declaration, body=None): @@ -202,7 +203,7 @@ void set_pyobj(PyObject *pyobj) ## write the parent callers (_name) for parent_caller in self.virtual_parent_callers.itervalues(): - parent_caller.class_ = self.class_ + #parent_caller.class_ = self.class_ parent_caller.helper_class = self parent_caller.reset_code_generation_state() ## test code generation @@ -224,11 +225,11 @@ void set_pyobj(PyObject *pyobj) ## write the virtual proxies for virtual_proxy in self.virtual_proxies: - virtual_proxy.class_ = self.class_ + #virtual_proxy.class_ = self.class_ virtual_proxy.helper_class = self ## test code generation - virtual_proxy.class_ = self.class_ - virtual_proxy.helper_class = self + #virtual_proxy.class_ = self.class_ + #virtual_proxy.helper_class = self virtual_proxy.reset_code_generation_state() try: try: @@ -262,7 +263,7 @@ void set_pyobj(PyObject *pyobj) ## write the parent callers (_name) method_defs = [] for name, parent_caller in self.virtual_parent_callers.iteritems(): - parent_caller.class_ = self.class_ + #parent_caller.class_ = self.class_ parent_caller.helper_class = self code_sink.writeln() @@ -272,11 +273,11 @@ void set_pyobj(PyObject *pyobj) (code_sink,), {}, parent_caller) except utils.SkipWrapper: continue - method_defs.append(parent_caller.get_py_method_def(name)) + method_defs.append(parent_caller.get_py_method_def('_' + name)) ## write the virtual proxies for virtual_proxy in self.virtual_proxies: - virtual_proxy.class_ = self.class_ + #virtual_proxy.class_ = self.class_ virtual_proxy.helper_class = self code_sink.writeln() @@ -966,6 +967,8 @@ public: else: raise TypeError + method.class_ = self + if method.visibility == 'public': try: overload = self.methods[name] @@ -974,11 +977,45 @@ public: overload.pystruct = self.pystruct self.methods[name] = overload - method.class_ = self + ## add it.... try: utils.call_with_error_handling(overload.add, (method,), {}, method) except utils.SkipWrapper: return + + + # Grr! I hate C++. Overloading + inheritance = disaster! + # So I ended up coding something which C++ does not in + # fact support, but I feel bad to just throw away my good + # code due to a C++ fault, so I am leaving here the code + # disabled. Maybe some future C++ version will come along + # and fix this problem, who knows :P + if 0: + # due to a limitation of the pybindgen overloading + # strategy, we need to re-wrap for this class all + # methods with the same name and different signature + # from parent classes. + overload._compute_all_wrappers() + if isinstance(method, CppMethod): + mro = self.get_mro() + mro.next() # skip 'self' + for cls in mro: + try: + parent_overload = cls.methods[name] + except KeyError: + continue + parent_overload._compute_all_wrappers() + for parent_method in parent_overload.all_wrappers: + already_exists = False + for existing_method in overload.all_wrappers: + if existing_method.matches_signature(parent_method): + already_exists = True + break + if not already_exists: + new_method = parent_method.clone() + new_method.class_ = self + overload.add(new_method) + else: self.nonpublic_methods.append(method) if method.is_virtual: @@ -1030,6 +1067,7 @@ public: ## recorded in the class, even if the method is ## not wrapped. method = CppDummyMethod(*args, **kwargs) + method.class_ = self self._dummy_methods.append(method) self._have_pure_virtual_methods = None helper_class = self.get_helper_class() @@ -1214,6 +1252,7 @@ public: for method in cls.get_all_methods(): if not method.is_virtual: continue + method = method.clone() self.helper_class.add_virtual_method(method) diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index 6b7ae11..9dea0eb 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -94,11 +94,22 @@ class CppMethod(ForwardWrapperBase): self.custom_name = (custom_name or custom_template_method_name) self._class = None + self._helper_class = None self.docstring = None self.wrapper_base_name = None self.wrapper_actual_name = None #self.static_decl = True + def set_helper_class(self, helper_class): + "Set the C++ helper class, which is used for overriding virtual methods" + self._helper_class = helper_class + self.wrapper_base_name = "%s::_wrap_%s" % (self._helper_class.name, self.method_name) + def get_helper_class(self): + "Get the C++ helper class, which is used for overriding virtual methods" + return self._helper_class + helper_class = property(get_helper_class, set_helper_class) + + def matches_signature(self, other): if self.mangled_name != other.mangled_name: return False @@ -132,7 +143,8 @@ class CppMethod(ForwardWrapperBase): is_static=self.is_static, template_parameters=self.template_parameters, is_virtual=self.is_virtual, - is_const=self.is_const) + is_const=self.is_const, + visibility=self.visibility) meth._class = self._class meth.docstring = self.docstring meth.wrapper_base_name = self.wrapper_base_name @@ -181,6 +193,18 @@ class CppMethod(ForwardWrapperBase): CppClass parameters""" cppclass.implement_parameter_custodians(self) + def _get_pystruct(self): + # When a method is used in the context of a helper class, we + # should use the pystruct of the helper class' class. + # + # self.class_: original base class where the method is defined + # self._helper_class.class_: subclass that is inheriting the method + # + if self._helper_class is None: + return self.class_.pystruct + else: + return self._helper_class.class_.pystruct + def get_wrapper_signature(self, wrapper_name, extra_wrapper_params=()): flags = self.get_py_method_def_flags() @@ -195,15 +219,16 @@ class CppMethod(ForwardWrapperBase): # extra = ', '.join([''] + list(extra_wrapper_params)) # else: # extra = '' + if 'METH_VARARGS' in flags: if 'METH_KEYWORDS' in flags: - self.wrapper_args = ["%s *%s" % (self.class_.pystruct, _self_name), + self.wrapper_args = ["%s *%s" % (self._get_pystruct(), _self_name), "PyObject *args", "PyObject *kwargs"] else: assert not extra_wrapper_params, \ "extra_wrapper_params can only be used with"\ " full varargs/kwargs wrappers" - self.wrapper_args = ["%s *%s" % (self.class_.pystruct, _self_name), + self.wrapper_args = ["%s *%s" % (self._get_pystruct(), _self_name), "PyObject *args"] else: assert not extra_wrapper_params, \ @@ -211,7 +236,7 @@ class CppMethod(ForwardWrapperBase): if 'METH_STATIC' in flags: self.wrapper_args = ['void'] else: - self.wrapper_args = ["%s *%s" % (self.class_.pystruct, _self_name)] + self.wrapper_args = ["%s *%s" % (self._get_pystruct(), _self_name)] self.wrapper_args.extend(extra_wrapper_params) return self.wrapper_return, "%s(%s)" % (self.wrapper_actual_name, ', '.join(self.wrapper_args)) @@ -558,22 +583,16 @@ class CppVirtualMethodParentCaller(CppMethod): """ super(CppVirtualMethodParentCaller, self).__init__( method.method_name, method.return_value, method.parameters, unblock_threads=unblock_threads) - self._helper_class = None #self.static_decl = False self.method = method - def set_class(self, class_): - "Set the class wrapper object (CppClass)" - self._class = class_ + def get_class(self): + return self.method.class_ + class_ = property(get_class) - def set_helper_class(self, helper_class): - "Set the C++ helper class, which is used for overriding virtual methods" - self._helper_class = helper_class - self.wrapper_base_name = "%s::_wrap_%s" % (self._helper_class.name, self.method_name) - def get_helper_class(self): - "Get the C++ helper class, which is used for overriding virtual methods" - return self._helper_class - helper_class = property(get_helper_class, set_helper_class) + #def set_class(self, class_): + # "Set the class wrapper object (CppClass)" + # self._class = class_ def generate_declaration(self, code_sink, extra_wrapper_parameters=()): ## We need to fake generate the code (and throw away the @@ -608,18 +627,16 @@ class CppVirtualMethodParentCaller(CppMethod): ', '.join([join_ctype_and_name(param.ctype, param.name) for param in self.parameters]) )) if self.return_value.ctype == 'void': - code_sink.writeln('{ %s::%s(%s); }' % (self._class.full_name, self.method_name, + code_sink.writeln('{ %s::%s(%s); }' % (self.method.class_.full_name, self.method_name, ', '.join([param.name for param in self.parameters]))) else: - code_sink.writeln('{ return %s::%s(%s); }' % (self._class.full_name, self.method_name, + code_sink.writeln('{ return %s::%s(%s); }' % (self.method.class_.full_name, self.method_name, ', '.join([param.name for param in self.parameters]))) def generate_call(self, class_=None): "virtual method implementation; do not call" - #assert isinstance(class_, CppClass) - if class_ is None: - class_ = self._class + class_ = self.class_ method = 'reinterpret_cast< %s* >(self->obj)->%s__parent_caller' % (self._helper_class.name, self.method_name) if self.return_value.ctype == 'void': self.before_call.write_code( @@ -646,7 +663,7 @@ class CppVirtualMethodParentCaller(CppMethod): method_name = self.method_name flags = self.get_py_method_def_flags() return "{\"%s\", (PyCFunction) %s, %s, %s }," % \ - ('_'+method_name, + (method_name, self.wrapper_actual_name,#'::'.join((self._helper_class.name, self.wrapper_actual_name)), '|'.join(flags), (self.docstring is None and "NULL" or ('"'+self.docstring+'"'))) @@ -661,7 +678,8 @@ class CppVirtualMethodParentCaller(CppMethod): self.return_value, self.method_name, [copy(param) for param in self.parameters]) - meth._class = self._class + #meth._class = self._class + meth.method = self.method meth._helper_class = self._helper_class meth.docstring = self.docstring meth.wrapper_base_name = self.wrapper_base_name @@ -682,19 +700,12 @@ class CppVirtualMethodProxy(ReverseWrapperBase): super(CppVirtualMethodProxy, self).__init__(method.return_value, method.parameters) self.method_name = method.method_name self.method = method - self._class = None self._helper_class = None - - def set_class(self, class_): - "Set the class wrapper object (CppClass)" - assert isinstance(class_, cppclass.CppClass) - self._class = class_ def get_class(self): "Get the class wrapper object (CppClass)" - return self._class - class_ = property(get_class, set_class) - + return self.method.class_ + class_ = property(get_class) def set_helper_class(self, helper_class): "Set the C++ helper class, which is used for overriding virtual methods" @@ -746,7 +757,7 @@ class CppVirtualMethodProxy(ReverseWrapperBase): if self.return_value.ctype == 'void': if not (self.method.is_pure_virtual or self.method.visibility == 'private'): self.before_call.write_code(r' %s::%s(%s);' - % (self._class.full_name, self.method_name, call_params)) + % (self.class_.full_name, self.method_name, call_params)) self.before_call.write_code(r' return;') else: if self.method.is_pure_virtual or self.method.visibility == 'private': @@ -760,7 +771,7 @@ Py_FatalError("Error detected, but parent virtual is pure virtual or private vir "and return is a class without trival constructor");''') else: self.set_error_return("PyErr_Print();\nreturn %s::%s(%s);" - % (self._class.full_name, self.method_name, call_params)) + % (self.class_.full_name, self.method_name, call_params)) self.before_call.indent() self.before_call.write_code(self.error_return) self.before_call.unindent() @@ -768,18 +779,18 @@ Py_FatalError("Error detected, but parent virtual is pure virtual or private vir ## Set "m_pyself->obj = this" around virtual method call invocation self_obj_before = self.declarations.declare_variable( - '%s*' % self._class.full_name, 'self_obj_before') + '%s*' % self.class_.full_name, 'self_obj_before') self.before_call.write_code("%s = reinterpret_cast< %s* >(m_pyself)->obj;" % - (self_obj_before, self._class.pystruct)) + (self_obj_before, self.class_.pystruct)) if self.method.is_const: this_expression = ("const_cast< %s* >((const %s*) this)" % - (self._class.full_name, self._class.full_name)) + (self.class_.full_name, self.class_.full_name)) else: - this_expression = "(%s*) this" % (self._class.full_name) + this_expression = "(%s*) this" % (self.class_.full_name) self.before_call.write_code("reinterpret_cast< %s* >(m_pyself)->obj = %s;" % - (self._class.pystruct, this_expression)) + (self.class_.pystruct, this_expression)) self.before_call.add_cleanup_code("reinterpret_cast< %s* >(m_pyself)->obj = %s;" % - (self._class.pystruct, self_obj_before)) + (self.class_.pystruct, self_obj_before)) super(CppVirtualMethodProxy, self).generate( code_sink, '::'.join((self._helper_class.name, self.method_name)), @@ -810,7 +821,7 @@ class CustomCppMethodWrapper(CppMethod): def generate(self, code_sink, dummy_wrapper_name=None, extra_wrapper_params=()): assert extra_wrapper_params == ["PyObject **return_exception"] - self.wrapper_args = ["%s *self" % self._class.pystruct, "PyObject *args", "PyObject *kwargs", "PyObject **return_exception"] + self.wrapper_args = ["%s *self" % self.class_.pystruct, "PyObject *args", "PyObject *kwargs", "PyObject **return_exception"] self.wrapper_return = "PyObject *" if self.wrapper_body is not None: code_sink.writeln(self.wrapper_body) @@ -848,7 +859,7 @@ class CustomCppConstructorWrapper(CppConstructor): assert extra_wrapper_params == ["PyObject **return_exception"] code_sink.writeln(self.wrapper_body) return ("int %s (%s *self, PyObject *args, PyObject *kwargs, PyObject **return_exception)" - % (self.wrapper_actual_name, self._class.pystruct)) + % (self.wrapper_actual_name, self.class_.pystruct)) def generate_call(self, *args, **kwargs): pass diff --git a/tests/foo.h b/tests/foo.h index a33810d..9e72f94 100644 --- a/tests/foo.h +++ b/tests/foo.h @@ -655,4 +655,18 @@ typedef void (*Callback) (void); void function_that_takes_callback (Callback cb); +// +struct Socket +{ + virtual int Bind () { return -1; } + virtual int Bind (int address) { return address; } +}; + +struct UdpSocket : public Socket +{ + virtual int Bind () { return 0; } +}; +// + + #endif /* !FOO_H_ */ diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index 5f16dac..63afa27 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -345,7 +345,14 @@ int %s::custom_method_added_by_a_hook(int x) inner.add_method('Do', 'void', []) + Socket = mod.add_class('Socket', allow_subclassing=True) + Socket.add_constructor([]) + Socket.add_method('Bind', ReturnValue.new('int'), [], is_virtual=True) + Socket.add_method('Bind', ReturnValue.new('int'), [Parameter.new('int', 'address')], is_virtual=True) + UdpSocket = mod.add_class('UdpSocket', parent=Socket) + UdpSocket.add_constructor([]) + UdpSocket.add_method('Bind', ReturnValue.new('int'), [], is_virtual=True) #### --- error handler --- class MyErrorHandler(pybindgen.settings.ErrorHandler): diff --git a/tests/footest.py b/tests/footest.py index a603818..093a808 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -719,6 +719,22 @@ class TestFoo(unittest.TestCase): inner.Do () self.assertEqual (True, True) + def test_overloaded_methods_and_inheritance(self): + """https://bugs.launchpad.net/pybindgen/+bug/246069""" + + sock = foo.Socket() + self.assertEqual(sock.Bind(), -1) + self.assertEqual(sock.Bind(123), 123) + + udpsock = foo.UdpSocket() + self.assertEqual(udpsock.Bind(), 0) + try: + self.assertEqual(udpsock.Bind(123), 123) + except TypeError, ex: + pass + else: + self.fail("UdpSocket.Bind(int) was not supposed to work") + if __name__ == '__main__': unittest.main()