diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index a465974..aa42eec 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -441,6 +441,7 @@ class CppClass(object): self.helper_class_disabled = False self.cannot_be_constructed = '' # reason self.has_trivial_constructor = False + self.has_copy_constructor = False self.has_output_stream_operator = False self._have_pure_virtual_methods = None ## list of CppClasses from which a value of this class can be @@ -1143,6 +1144,9 @@ public: self.constructors.append(wrapper) if not wrapper.parameters: self.has_trivial_constructor = True # FIXME: I don't remember what is this used for anymore, maybe remove + if len(wrapper.parameters) == 1 and isinstance(wrapper.parameters[0], CppClassParameterBase) \ + and wrapper.parameters[0].cpp_class is self: + self.has_copy_constructor = True def add_output_stream_operator(self): """ @@ -1499,6 +1503,34 @@ typedef struct { or constructor)) return have_constructor + def _generate_copy_method(self, code_sink): + construct_name = self.get_construct_name() + copy_wrapper_name = '_wrap_%s__copy__' % self.pystruct + code_sink.writeln(''' +static PyObject*\n%s(%s *self) +{ +''' % (copy_wrapper_name, self.pystruct)) + code_sink.indent() + + if self.allow_subclassing: + new_func = 'PyObject_GC_New' + else: + new_func = 'PyObject_New' + + code_sink.writeln("%s *py_copy = %s(%s, %s);" % + (self.pystruct, new_func, self.pystruct, '&'+self.pytypestruct)) + + if self.allow_subclassing: + code_sink.writeln("py_copy->inst_dict = NULL;") + + code_sink.writeln("py_copy->obj = new %s(*self->obj);" % construct_name) + + code_sink.writeln("return (PyObject*) py_copy;") + code_sink.unindent() + code_sink.writeln("}") + code_sink.writeln() + return copy_wrapper_name + def _generate_methods(self, code_sink, parent_caller_methods): """generate the method wrappers""" method_defs = [] @@ -1513,6 +1545,15 @@ typedef struct { method_defs.append(overload.get_py_method_def(meth_name)) code_sink.writeln() method_defs.extend(parent_caller_methods) + + if self.has_copy_constructor: + try: + copy_wrapper_name = utils.call_with_error_handling(self._generate_copy_method, (code_sink,), {}, None) + except utils.SkipWrapper: + pass + else: + method_defs.append('{"__copy__", (PyCFunction) %s, METH_NOARGS, NULL},' % copy_wrapper_name) + ## generate the method table code_sink.writeln("static PyMethodDef %s_methods[] = {" % (self.pystruct,)) code_sink.indent() diff --git a/tests/footest.py b/tests/footest.py index 41c7609..aba9940 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -2,6 +2,7 @@ import sys import weakref import gc import os.path +import copy sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'build', 'default', 'tests')) which = int(sys.argv[1]) @@ -900,7 +901,13 @@ class TestFoo(unittest.TestCase): self.assertEqual(s2.xpto, 321) self.assertEqual(s1.xpto, 123) - # copy via __copy__ TODO + # copy via __copy__ + s3 = copy.copy(s1) + self.assertEqual(s3.xpto, 123) + s3.xpto = 456 + self.assertEqual(s3.xpto, 456) + self.assertEqual(s1.xpto, 123) + if __name__ == '__main__': unittest.main()