Add return_internal_reference option support, similar to boost::python.
This commit is contained in:
1 parent
b74e0a903a
commit
fdf3481ef3
6 files changed
+109
-6
No files matched your search
@@ -532,12 +532,11 @@ class CppClassReturnValue(CppClassReturnValueBase):
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
REQUIRES_ASSIGNMENT_CONSTRUCTOR = True
|
||||
|
||||
def __init__(self, ctype, is_const=False, reference_existing_object=False):
|
||||
def __init__(self, ctype, is_const=False):
|
||||
"""override to fix the ctype parameter with namespace information"""
|
||||
if ctype == self.cpp_class.name:
|
||||
ctype = self.cpp_class.full_name
|
||||
super(CppClassReturnValue, self).__init__(ctype, is_const=is_const)
|
||||
self.reference_existing_object = reference_existing_object
|
||||
|
||||
def get_c_error_return(self): # only used in reverse wrappers
|
||||
"""See ReturnValue.get_c_error_return"""
|
||||
@@ -591,12 +590,19 @@ class CppClassRefReturnValue(CppClassReturnValueBase):
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
REQUIRES_ASSIGNMENT_CONSTRUCTOR = True
|
||||
|
||||
def __init__(self, ctype, is_const=False, caller_owns_return=False, reference_existing_object=False):
|
||||
def __init__(self, ctype, is_const=False, caller_owns_return=False, reference_existing_object=None,
|
||||
return_internal_reference=None):
|
||||
#override to fix the ctype parameter with namespace information
|
||||
if ctype == self.cpp_class.name:
|
||||
ctype = self.cpp_class.full_name
|
||||
super(CppClassRefReturnValue, self).__init__(ctype, is_const=is_const)
|
||||
self.reference_existing_object = reference_existing_object
|
||||
|
||||
self.return_internal_reference = return_internal_reference
|
||||
if self.return_internal_reference:
|
||||
assert self.reference_existing_object is None
|
||||
self.reference_existing_object = True
|
||||
|
||||
self.caller_owns_return = caller_owns_return
|
||||
|
||||
def get_c_error_return(self): # only used in reverse wrappers
|
||||
@@ -947,7 +953,8 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
|
||||
def __init__(self, ctype, caller_owns_return=None, custodian=None,
|
||||
is_const=False, reference_existing_object=None):
|
||||
is_const=False, reference_existing_object=None,
|
||||
return_internal_reference=None):
|
||||
"""
|
||||
@param ctype: C type, normally 'MyClass*'
|
||||
@param caller_owns_return: if true, ownership of the object pointer
|
||||
@@ -975,6 +982,12 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
to an object that may have been deallocated in the
|
||||
mean time. Calling methods on such an object would
|
||||
lead to a memory error.
|
||||
|
||||
@param return_internal_reference: like
|
||||
reference_existing_object, but additionally adds
|
||||
custodian/ward to bind the lifetime of the 'self' object
|
||||
(instance the method is bound to) to the lifetime of the
|
||||
return value.
|
||||
|
||||
@note: only arguments which are instances of C++ classes
|
||||
wrapped by PyBindGen can be used as custodians.
|
||||
@@ -990,6 +1003,10 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
|
||||
self.caller_owns_return = caller_owns_return
|
||||
self.reference_existing_object = reference_existing_object
|
||||
self.return_internal_reference = return_internal_reference
|
||||
if self.return_internal_reference:
|
||||
assert self.reference_existing_object is None
|
||||
self.reference_existing_object = True
|
||||
self.custodian = custodian
|
||||
|
||||
if self.caller_owns_return is None\
|
||||
@@ -1087,6 +1104,9 @@ def scan_custodians_and_wards(wrapper):
|
||||
if custodian is not None:
|
||||
wrapper.add_custodian_and_ward(custodian, -1)
|
||||
|
||||
if getattr(wrapper.return_value, "return_internal_reference", False):
|
||||
wrapper.add_custodian_and_ward(-1, 0)
|
||||
|
||||
|
||||
|
||||
def _add_ward(code_block, custodian, ward):
|
||||
|
||||
@@ -259,6 +259,8 @@ class GccXmlTypeRegistry(object):
|
||||
kwargs['caller_owns_return'] = annotations_scanner.parse_boolean(value)
|
||||
elif name == 'reference_existing_object':
|
||||
kwargs['reference_existing_object'] = annotations_scanner.parse_boolean(value)
|
||||
elif name == 'return_internal_reference':
|
||||
kwargs['return_internal_reference'] = annotations_scanner.parse_boolean(value)
|
||||
elif name == 'custodian':
|
||||
kwargs['custodian'] = int(value)
|
||||
else:
|
||||
|
||||
@@ -173,6 +173,7 @@ Foo function_that_returns_foo()
|
||||
int Foo::instance_count = 0;
|
||||
int SomeObject::NestedClass::instance_count = 0;
|
||||
int Foobar::instance_count = 0;
|
||||
int Box::instance_count = 0;
|
||||
|
||||
Foobar* get_foobar_with_other_as_custodian (SomeObject *other)
|
||||
{
|
||||
|
||||
+12
-1
@@ -177,6 +177,7 @@ void delete_stored_zbr (void);
|
||||
|
||||
|
||||
class Foobar
|
||||
// -#- allow_subclassing=true -#-
|
||||
{
|
||||
Foobar (const Foobar &);
|
||||
Foobar& operator= (const Foobar &);
|
||||
@@ -1130,13 +1131,23 @@ class Box
|
||||
Foobar m_foobar;
|
||||
|
||||
public:
|
||||
Box () {}
|
||||
static int instance_count;
|
||||
|
||||
Box () { ++instance_count;}
|
||||
virtual ~Box () { --instance_count;}
|
||||
|
||||
// -#- @return(reference_existing_object=true) -#-
|
||||
Foobar* getFoobarInternalPtr () { return &m_foobar; }
|
||||
|
||||
// -#- @return(reference_existing_object=true) -#-
|
||||
Foobar& getFoobarInternalRef () { return m_foobar; }
|
||||
|
||||
// -#- @return(return_internal_reference=true) -#-
|
||||
Foobar* getFoobarInternalPtr2 () { return &m_foobar; }
|
||||
|
||||
// -#- @return(return_internal_reference=true) -#-
|
||||
Foobar& getFoobarInternalRef2 () { return m_foobar; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ def my_module_gen(out_file):
|
||||
Zoo.add_method('get_datum', ReturnValue.new('std::string'), [])
|
||||
Zoo.implicitly_converts_to(Foo)
|
||||
|
||||
Foobar = mod.add_class('Foobar')
|
||||
Foobar = mod.add_class('Foobar', allow_subclassing=True)
|
||||
Foobar.add_static_attribute('instance_count', ReturnValue.new('int'))
|
||||
|
||||
|
||||
@@ -532,8 +532,11 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
|
||||
Box = mod.add_class('Box')
|
||||
Box.add_constructor([])
|
||||
Box.add_static_attribute('instance_count', ReturnValue.new('int'))
|
||||
Box.add_method('getFoobarInternalPtr', ReturnValue.new('Foobar*', reference_existing_object=True), [])
|
||||
Box.add_method('getFoobarInternalRef', ReturnValue.new('Foobar&', reference_existing_object=True), [])
|
||||
Box.add_method('getFoobarInternalPtr2', ReturnValue.new('Foobar*', return_internal_reference=True), [])
|
||||
Box.add_method('getFoobarInternalRef2', ReturnValue.new('Foobar&', return_internal_reference=True), [])
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1256,5 +1256,71 @@ class TestFoo(unittest.TestCase):
|
||||
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
|
||||
|
||||
|
||||
def test_reference_existing_object_ptr2(self):
|
||||
while gc.collect():
|
||||
pass
|
||||
Foobar_count1 = foo.Foobar.instance_count
|
||||
Box_count1 = foo.Box.instance_count;
|
||||
|
||||
box = foo.Box()
|
||||
self.assertEqual(foo.Box.instance_count, Box_count1 + 1)
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1+1)
|
||||
|
||||
f1 = box.getFoobarInternalPtr2()
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1+1)
|
||||
f2 = box.getFoobarInternalPtr2()
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1+1)
|
||||
self.assert_(f2 is f1)
|
||||
|
||||
del f2, box
|
||||
while gc.collect():
|
||||
pass
|
||||
|
||||
# f1 is still alive, so box should still exist, in spite of having been locally unreferenced
|
||||
self.assertEqual(foo.Box.instance_count, Box_count1 + 1)
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1 + 1)
|
||||
|
||||
# delete f1, it should cause box to be freeable
|
||||
del f1
|
||||
while gc.collect():
|
||||
pass
|
||||
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
|
||||
self.assertEqual(foo.Box.instance_count, Box_count1)
|
||||
|
||||
|
||||
def test_reference_existing_object_ref2(self):
|
||||
while gc.collect():
|
||||
pass
|
||||
Foobar_count1 = foo.Foobar.instance_count
|
||||
Box_count1 = foo.Box.instance_count;
|
||||
|
||||
box = foo.Box()
|
||||
self.assertEqual(foo.Box.instance_count, Box_count1 + 1)
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1+1)
|
||||
|
||||
f1 = box.getFoobarInternalRef2()
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1+1)
|
||||
f2 = box.getFoobarInternalRef2()
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1+1)
|
||||
self.assert_(f2 is f1)
|
||||
|
||||
del f2, box
|
||||
while gc.collect():
|
||||
pass
|
||||
|
||||
# f1 is still alive, so box should still exist, in spite of having been locally unreferenced
|
||||
self.assertEqual(foo.Box.instance_count, Box_count1 + 1)
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1 + 1)
|
||||
|
||||
# delete f1, it should cause box to be freeable
|
||||
del f1
|
||||
while gc.collect():
|
||||
pass
|
||||
|
||||
self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
|
||||
self.assertEqual(foo.Box.instance_count, Box_count1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user