diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 214d8a0..1a23de4 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -1555,6 +1555,7 @@ typedef struct { PyObject_HEAD %s *obj; PyObject *inst_dict; + PyBindGenWrapperFlags flags:8; } %s; ''' % (self.full_name, self.pystruct)) @@ -1564,6 +1565,7 @@ typedef struct { typedef struct { PyObject_HEAD %s *obj; + PyBindGenWrapperFlags flags:8; } %s; ''' % (self.full_name, self.pystruct)) @@ -1962,6 +1964,7 @@ static PyObject*\n%s(%s *self) code_block.write_code("%s->obj = new %s(*self->obj);" % (py_copy, construct_name)) if self.allow_subclassing: code_block.write_code("%s->inst_dict = NULL;" % py_copy) + code_block.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % py_copy) self.wrapper_registry.write_register_new_wrapper(code_block, py_copy, "%s->obj" % py_copy) @@ -2030,7 +2033,9 @@ static PyObject*\n%s(%s *self) % self.full_name) delete_code = (" %s *tmp = self->obj;\n" " self->obj = NULL;\n" - " delete tmp;" + " if (!(self->flags&PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED)) {\n" + " delete tmp;\n" + " }" % (self.full_name,)) return delete_code diff --git a/pybindgen/cppclass_typehandlers.py b/pybindgen/cppclass_typehandlers.py index ed72154..398067d 100644 --- a/pybindgen/cppclass_typehandlers.py +++ b/pybindgen/cppclass_typehandlers.py @@ -141,6 +141,7 @@ class CppClassParameter(CppClassParameterBase): if self.cpp_class.allow_subclassing: wrapper.before_call.write_code( "%s->inst_dict = NULL;" % (self.py_name,)) + wrapper.before_call.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % (self.py_name,)) self.cpp_class.write_create_instance(wrapper.before_call, "%s->obj" % self.py_name, @@ -258,6 +259,7 @@ class CppClassRefParameter(CppClassParameterBase): if self.cpp_class.allow_subclassing: wrapper.after_call.write_code( "%s->inst_dict = NULL;" % (self.py_name,)) + wrapper.after_call.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % (self.py_name,)) self.cpp_class.write_create_instance(wrapper.before_call, "%s->obj" % self.py_name, @@ -300,6 +302,7 @@ class CppClassRefParameter(CppClassParameterBase): if self.cpp_class.allow_subclassing: wrapper.before_call.write_code( "%s->inst_dict = NULL;" % (self.py_name,)) + wrapper.before_call.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % (self.py_name,)) if self.direction == Parameter.DIRECTION_IN: self.cpp_class.write_create_instance(wrapper.before_call, @@ -380,6 +383,7 @@ class CppClassReturnValue(CppClassReturnValueBase): if self.cpp_class.allow_subclassing: wrapper.after_call.write_code( "%s->inst_dict = NULL;" % (py_name,)) + wrapper.after_call.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % (py_name,)) self.cpp_class.write_create_instance(wrapper.after_call, "%s->obj" % py_name, @@ -402,6 +406,22 @@ class CppClassReturnValue(CppClassReturnValueBase): else: wrapper.after_call.write_code('%s = *%s->obj;' % (self.value, name)) +def _add_ward(wrapper, custodian, ward): + wards = wrapper.declarations.declare_variable( + 'PyObject*', 'wards') + wrapper.after_call.write_code( + "%(wards)s = PyObject_GetAttrString(%(custodian)s, (char *) \"__wards__\");" + % vars()) + wrapper.after_call.write_code( + "if (%(wards)s == NULL) {\n" + " PyErr_Clear();\n" + " %(wards)s = PyList_New(0);\n" + " PyObject_SetAttrString(%(custodian)s, (char *) \"__wards__\", %(wards)s);\n" + "}" % vars()) + wrapper.after_call.write_code("PyList_Append(%s, %s);" % (wards, ward)) + wrapper.after_call.add_cleanup_code("Py_DECREF(%s);" % wards) + + class CppClassPtrParameter(CppClassParameterBase): "Class* handlers" @@ -519,6 +539,11 @@ class CppClassPtrParameter(CppClassParameterBase): wrapper.before_call.unindent() wrapper.before_call.write_code("}") + if self.custodian is not None: + wrapper.after_call.write_code("%s->flags = (PyBindGenWrapperFlags) (%s->flags |" + " PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED);" + % (self.py_name, self.py_name)) + def convert_c_to_python(self, wrapper): """foo""" @@ -564,6 +589,7 @@ class CppClassPtrParameter(CppClassParameterBase): if self.cpp_class.allow_subclassing: wrapper.before_call.write_code( "%s->inst_dict = NULL;" % (py_name,)) + wrapper.before_call.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % py_name) ## Assign the C++ value to the Python wrapper if self.transfer_ownership: @@ -680,24 +706,6 @@ class CppClassPtrParameter(CppClassParameterBase): wrapper.build_params.add_parameter("N", [py_name]) - -def _add_ward(wrapper, custodian, ward): - wards = wrapper.declarations.declare_variable( - 'PyObject*', 'wards') - wrapper.after_call.write_code( - "%(wards)s = PyObject_GetAttrString(%(custodian)s, (char *) \"__wards__\");" - % vars()) - wrapper.after_call.write_code( - "if (%(wards)s == NULL) {\n" - " PyErr_Clear();\n" - " %(wards)s = PyList_New(0);\n" - " PyObject_SetAttrString(%(custodian)s, (char *) \"__wards__\", %(wards)s);\n" - "}" % vars()) - wrapper.after_call.write_code("PyList_Append(%s, %s);" % (wards, ward)) - wrapper.after_call.add_cleanup_code("Py_DECREF(%s);" % wards) - - - class CppClassPtrReturnValue(CppClassReturnValueBase): "Class* return handler" CTYPES = [] @@ -797,6 +805,12 @@ class CppClassPtrReturnValue(CppClassReturnValueBase): if self.cpp_class.allow_subclassing: wrapper.after_call.write_code( "%s->inst_dict = NULL;" % (py_name,)) + if self.custodian is None: + wrapper.after_call.write_code( + "%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % (py_name,)) + else: + wrapper.after_call.write_code( + "%s->flags = PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED;" % (py_name,)) ## Assign the C++ value to the Python wrapper if self.caller_owns_return: diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index a5d92cf..cbf48d1 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -625,6 +625,7 @@ class CppFunctionAsConstructor(CppConstructor): ## FIXME: check caller_owns_return in self.function_return_value self.before_call.write_code("self->obj = %s(%s);" % (self.c_function_name, ", ".join(self.call_params))) + self.before_call.write_code("self->flags = PYBINDGEN_WRAPPER_FLAG_NONE;") class CppOverloadedConstructor(overloading.OverloadedWrapper): diff --git a/pybindgen/utils.py b/pybindgen/utils.py index 69d5597..c28889d 100644 --- a/pybindgen/utils.py +++ b/pybindgen/utils.py @@ -87,6 +87,12 @@ typedef intobjargproc ssizeobjargproc; #else # define PYBINDGEN_UNUSED(param) param #endif /* !__GNUC__ */ + +typedef enum _PyBindGenWrapperFlags { + PYBINDGEN_WRAPPER_FLAG_NONE = 0, + PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED = (1<<0), +} PyBindGenWrapperFlags; + ''') diff --git a/tests/foo.cc b/tests/foo.cc index 5fa94d0..a1c77dd 100644 --- a/tests/foo.cc +++ b/tests/foo.cc @@ -39,10 +39,13 @@ SomeObject::~SomeObject () m_internal_zbr->Unref (); m_internal_zbr = NULL; } + delete m_foobar; + m_foobar = NULL; } SomeObject::SomeObject (const SomeObject &other) - : m_prefix (other.m_prefix) + : m_prefix (other.m_prefix), + m_foobar (NULL) { if (other.m_foo_ptr) m_foo_ptr = new Foo (*other.m_foo_ptr); @@ -58,6 +61,7 @@ SomeObject::SomeObject (const SomeObject &other) m_internal_zbr = new Zbr; m_pyobject = NULL; + m_foobar = NULL; SomeObject::instance_count++; } @@ -65,7 +69,8 @@ SomeObject::SomeObject (std::string const prefix) : m_prefix (prefix), m_foo_ptr (0), m_foo_shared_ptr (0), m_zbr (0), m_internal_zbr (new Zbr), - m_pyobject (NULL) + m_pyobject (NULL), + m_foobar (NULL) { SomeObject::instance_count++; } @@ -74,7 +79,8 @@ SomeObject::SomeObject (int prefix_len) : m_prefix (prefix_len, 'X'), m_foo_ptr (0), m_foo_shared_ptr (0), m_zbr (0), m_internal_zbr (new Zbr), - m_pyobject (NULL) + m_pyobject (NULL), + m_foobar (NULL) { SomeObject::instance_count++; } @@ -168,10 +174,9 @@ int Foo::instance_count = 0; int SomeObject::NestedClass::instance_count = 0; int Foobar::instance_count = 0; -Foobar* get_foobar_with_other_as_custodian (const SomeObject *other) +Foobar* get_foobar_with_other_as_custodian (SomeObject *other) { - other++; - return new Foobar; + return other->get_foobar_with_self_as_custodian(); } Foobar* create_new_foobar() @@ -179,16 +184,16 @@ Foobar* create_new_foobar() return new Foobar; } -void set_foobar_with_other_as_custodian(Foobar *foobar, const SomeObject *other) +void set_foobar_with_other_as_custodian(Foobar *foobar, SomeObject *other) { - foobar++; - other++; + other->set_foobar_with_self_as_custodian(foobar); } SomeObject * set_foobar_with_return_as_custodian(Foobar *foobar) { - foobar++; - return new SomeObject("xxx"); + SomeObject *some = new SomeObject("xxx"); + some->set_foobar_with_self_as_custodian(foobar); + return some; } std::string some_object_get_something_prefixed(const SomeObject *obj, const std::string something) diff --git a/tests/foo.h b/tests/foo.h index d3a37e6..e92d7e5 100644 --- a/tests/foo.h +++ b/tests/foo.h @@ -245,6 +245,7 @@ private: Zbr *m_internal_zbr; PyObject *m_pyobject; + Foobar *m_foobar; SomeObject (); @@ -426,16 +427,19 @@ public: // -#- @return(custodian=0) -#- Foobar* get_foobar_with_self_as_custodian () { - return new Foobar; + if (m_foobar == NULL) { + m_foobar = new Foobar; + } + return m_foobar; } - // -#- @return(custodian=1) -#- - Foobar* get_foobar_with_other_as_custodian (const SomeObject *other) { - other++; - return new Foobar; + // -#- @return(custodian=1); @other(transfer_ownership=false) -#- + Foobar* get_foobar_with_other_as_custodian (SomeObject *other) { + return other->get_foobar_with_self_as_custodian (); } // -#- @foobar(custodian=0) -#- void set_foobar_with_self_as_custodian (Foobar *foobar) { - foobar++; + delete m_foobar; + m_foobar = foobar; } }; @@ -496,13 +500,13 @@ namespace xpto std::string get_foo_datum(FooXpto const &foo); } -// -#- @return(custodian=1) -#- -Foobar* get_foobar_with_other_as_custodian(const SomeObject *other); +// -#- @return(custodian=1); @other(transfer_ownership=false) -#- +Foobar* get_foobar_with_other_as_custodian(SomeObject *other); // -#- @return(caller_owns_return=true) -#- Foobar* create_new_foobar(); -// -#- @foobar(custodian=2) -#- -void set_foobar_with_other_as_custodian(Foobar *foobar, const SomeObject *other); +// -#- @foobar(custodian=2); @other(transfer_ownership=false) -#- +void set_foobar_with_other_as_custodian(Foobar *foobar, SomeObject *other); // -#- @foobar(custodian=-1); @return(caller_owns_return=true) -#- SomeObject * set_foobar_with_return_as_custodian(Foobar *foobar); diff --git a/tests/test-generation.py b/tests/test-generation.py index e4a0375..83b0029 100755 --- a/tests/test-generation.py +++ b/tests/test-generation.py @@ -29,7 +29,8 @@ class MyReverseWrapper(typehandlers.base.ReverseWrapperBase): def test(): - pybindgen.write_preamble(codesink.FileCodeSink(sys.stdout)) + code_out = codesink.FileCodeSink(sys.stdout) + pybindgen.write_preamble(code_out) print print "#include " print @@ -52,7 +53,6 @@ public: ''') module = Module("foo") - code_out = codesink.FileCodeSink(sys.stdout) ## Register type handlers for the class Foo = cppclass.CppClass('Foo')