def test_custodian_method_other(self): while gc.collect(): pass SomeObject_count_before = foo.SomeObject.instance_count Foobar_count1 = foo.Foobar.instance_count obj1 = foo.SomeObject("xxx") obj2 = foo.SomeObject("yyy") foo1 = obj1.get_foobar_with_other_as_custodian(obj2) while gc.collect(): pass Foobar_count2 = foo.Foobar.instance_count ## now, deleting foo1 should keep Foobar count the same, since ## obj2 is keeping it alive del foo1 while gc.collect(): pass self.assertEqual(foo.Foobar.instance_count, Foobar_count2) ## deleting obj1 should still keep Foobar count the same, since ## obj2, not obj1, is keeping it alive del obj1 while gc.collect(): pass self.assertEqual(foo.Foobar.instance_count, Foobar_count2) self.assertEqual(foo.SomeObject.instance_count, SomeObject_count_before + 1) ## now deleting obj2 should cause both foo1 and obj2 to be destroyed del obj2 while gc.collect(): pass self.assertEqual(foo.SomeObject.instance_count, SomeObject_count_before) self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
def test_custodian_method_self(self): while gc.collect(): pass SomeObject_count_before = foo.SomeObject.instance_count obj1 = foo.SomeObject("xxx") Foobar_count1 = foo.Foobar.instance_count # get once foo1 = obj1.get_foobar_with_self_as_custodian() Foobar_count2 = foo.Foobar.instance_count self.assertEqual(Foobar_count2, Foobar_count1 + 1) # get another time, the C++ object should be the same foo2 = obj1.get_foobar_with_self_as_custodian() Foobar_count3 = foo.Foobar.instance_count self.assertEqual(Foobar_count3, Foobar_count2) ## now, deleting foo1 should keep the Foobar count the same, since ## obj1 is keeping it alive del foo1, foo2 while gc.collect(): pass self.assertEqual(foo.Foobar.instance_count, Foobar_count2) ## now deleting obj1 should cause both foo1 and obj1 to be destroyed del obj1 while gc.collect(): pass self.assertEqual(foo.SomeObject.instance_count, SomeObject_count_before) self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
def test_pass_foo_shared_ptr(self): obj = foo.SomeObject("") f = foo.Foo("hello") obj.set_foo_shared_ptr(f) self.assertEqual(f.get_datum(), "hello") f2 = obj.get_foo_shared_ptr() self.assertEqual(f2.get_datum(), "hello")
def test_custodian_method_param_self(self): while gc.collect(): pass SomeObject_count_before = foo.SomeObject.instance_count obj1 = foo.SomeObject("xxx") Foobar_count1 = foo.Foobar.instance_count foo1 = foo.create_new_foobar() obj1.set_foobar_with_self_as_custodian(foo1) Foobar_count2 = foo.Foobar.instance_count self.assertEqual(Foobar_count2, Foobar_count1 + 1) ## now, deleting foo1 should keep the Foobar count the same, since ## obj1 is keeping it alive del foo1 while gc.collect(): pass self.assertEqual(foo.Foobar.instance_count, Foobar_count2) ## now deleting obj1 should cause both foo1 and obj1 to be destroyed del obj1 while gc.collect(): pass self.assertEqual(foo.SomeObject.instance_count, SomeObject_count_before) self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
def test_pass_foo_by_value(self): obj = foo.SomeObject("") f = foo.Foo("hello") obj.set_foo_value(f) f2 = obj.get_foo_value() self.assertEqual(f2.get_datum(), "hello")
def test_custodian_function_param_other(self): while gc.collect(): pass SomeObject_count_before = foo.SomeObject.instance_count Foobar_count1 = foo.Foobar.instance_count obj1 = foo.SomeObject("xxx") foo1 = foo.create_new_foobar() foo.set_foobar_with_other_as_custodian(foo1, obj1) Foobar_count2 = foo.Foobar.instance_count ## now, deleting foo1 should keep Foobar count the same, since ## obj1 is keeping it alive del foo1 while gc.collect(): pass self.assertEqual(foo.Foobar.instance_count, Foobar_count2) ## now deleting obj1 should cause both foo1 and obj1 to be destroyed del obj1 while gc.collect(): pass self.assertEqual(foo.SomeObject.instance_count, SomeObject_count_before) self.assertEqual(foo.Foobar.instance_count, Foobar_count1)
def test_pass_by_reference(self): obj = foo.SomeObject("") f = foo.Foo("hello") obj.set_foo_by_ref(f) self.assertEqual(f.get_datum(), "hello") f2 = obj.get_foo_by_ref() self.assertEqual(f2.get_datum(), "hello")
def test_refcounting(self): obj = foo.SomeObject("") z = foo.Zbr("hello") obj.set_zbr_transfer(z) self.assertEqual(z.get_datum(), "hello") z2 = obj.get_zbr() self.assertEqual(z2.get_datum(), "hello") z3 = obj.get_zbr() self.assertEqual(z3.get_datum(), "hello") zz = foo.Zbr("world") self.assertEqual(zz.get_datum(), "world") obj.set_zbr_shared(zz) ## previous z's should not have been changed self.assertEqual(z.get_datum(), "hello") self.assertEqual(z2.get_datum(), "hello") self.assertEqual(z3.get_datum(), "hello") self.assertEqual(zz.get_datum(), "world") zz2 = obj.get_zbr() self.assertEqual(zz2.get_datum(), "world") zz3 = obj.peek_zbr() self.assertEqual(zz3.get_datum(), "world")
def test_function_as_method_ref(self): while gc.collect(): pass obj = foo.SomeObject("xpto") self.assertEqual(obj.ref_get_something_prefixed("something"), "xptosomething") while gc.collect(): pass
def test_pass_foo_by_transfer_ptr(self): obj = foo.SomeObject("") f = foo.Foo("hello") obj.set_foo_ptr(f) del f f2 = obj.get_foo_ptr() self.assertEqual(f2.get_datum(), "hello")
def test_overloaded_methods(self): obj = foo.SomeObject("zbr") v1 = obj.get_int(123.0) self.assertEqual(v1, 123) v2 = obj.get_int("123") self.assertEqual(v2, 123) self.assertRaises(TypeError, obj.get_int, [123])
def test_implicit_conversion_method_value(self): obj = foo.SomeObject("xxx") zoo1 = foo.Zoo("zpto") try: obj.set_foo_value(zoo1) except TypeError: self.fail() foo1 = obj.get_foo_value() self.assertEqual(foo1.get_datum(), "zpto")
def test_cstring_return(self): some = foo.SomeObject("xxx") s = some.method_returning_cstring() self.assertEqual(s, "foobar") class Test(foo.SomeObject): def method_returning_cstring(self): return "another string" some = Test("xxx") s = some.method_returning_cstring() self.assertEqual(s, "another string")
def test_nosubclass_gc(self): """Check if subclassable object is garbage collected""" while gc.collect(): pass count_before = foo.SomeObject.instance_count t = foo.SomeObject("xxx") t.xxx = t del t while gc.collect(): pass self.assertEqual(foo.SomeObject.instance_count, count_before)
def test_return_type_narrowing(self): obj = foo.SomeObject("zbr") obj.set_foo_ptr(foo.Foo()) foo1 = obj.get_foo_ptr() self.assertEqual(type(foo1), foo.Foo) bar2 = foo.Bar() self.assertEqual(type(bar2), foo.Bar) obj.set_foo_ptr(bar2) foo2 = obj.get_foo_ptr() self.assertEqual(type(foo2), foo.Bar)
def test_subclassable_transfer_ptr(self): while gc.collect(): pass count_before = foo.SomeObject.instance_count obj = foo.SomeObject("xxx") foo.store_some_object(obj) del obj while gc.collect(): pass ## check that SomeObject isn't prematurely deleted self.assertEqual(foo.SomeObject.instance_count, count_before + 1) ## now delete the object from the C side.. foo.delete_some_object() while gc.collect(): pass ## check that SomeObject was finally deleted self.assertEqual(foo.SomeObject.instance_count, count_before)
def test_refcounting_v2(self): while gc.collect(): pass zbr_count_before = foo.Zbr.instance_count obj = foo.SomeObject("") z1 = obj.get_internal_zbr() z2 = obj.get_internal_zbr() self.assertTrue(z1 is z2) del obj, z1, z2 while gc.collect(): pass zbr_count_after = foo.Zbr.instance_count self.assertEqual(zbr_count_after, zbr_count_before)
def test_virtual_overload(self): ## first test the plain object obj = foo.SomeObject("") self.assertEqual(obj.get_something(), "something") self.assertEqual(obj.get_something(123), "123") ## now subclass it class MyObject(foo.SomeObject): def get_something(self, arg=None): self.arg = arg return str(arg) obj = MyObject("") s1 = obj.get_something() self.assertEqual(s1, "None") self.assertEqual(obj.arg, None) s2 = obj.get_something(123) self.assertEqual(s2, "123") self.assertEqual(obj.arg, 123)
def test_subclassable_store_and_take_back(self): while gc.collect(): pass count_before = foo.SomeObject.instance_count obj = foo.SomeObject("xxx") foo.store_some_object(obj) del obj while gc.collect(): pass ## check that SomeObject isn't prematurely deleted self.assertEqual(foo.SomeObject.instance_count, count_before + 1) ## now get the object back from the C side.. obj = foo.take_some_object() self.assertEqual(obj.get_prefix(), "xxx") del obj while gc.collect(): pass ## check that SomeObject was finally deleted self.assertEqual(foo.SomeObject.instance_count, count_before)
def test_refcounting_v4(self): # same as v3 but using peek_zbr instead of get_zbr zbr_count_before = foo.Zbr.instance_count class MyZbr(foo.Zbr): pass z_in = MyZbr() obj = foo.SomeObject("") obj.set_zbr_transfer(z_in) del z_in z1 = obj.peek_zbr() z2 = obj.peek_zbr() self.assertTrue(z1 is z2) del obj, z1, z2 while gc.collect(): pass zbr_count_after = foo.Zbr.instance_count self.assertEqual(zbr_count_after, zbr_count_before)
def test_refcounting_v3(self): zbr_count_before = foo.Zbr.instance_count class MyZbr(foo.Zbr): pass z_in = MyZbr() obj = foo.SomeObject("") obj.set_zbr_transfer(z_in) del z_in z1 = obj.get_zbr() z2 = obj.get_zbr() self.assert_(z1 is z2) del obj, z1, z2 while gc.collect(): pass zbr_count_after = foo.Zbr.instance_count self.assertEqual(zbr_count_after, zbr_count_before)
def test_add_prefix(self): obj = foo.SomeObject("hello ") ln, msg = obj.add_prefix("gjc") self.assertEqual(msg, "hello gjc") self.assertEqual(ln, len("hello gjc"))
def test_operator_call(self): obj = foo.SomeObject("xpto_") l, s = obj("hello") self.assertEqual(s, "xpto_hello") self.assertEqual(l, len("xpto_hello"))
def test_instance_get_attribute(self): obj = foo.SomeObject("Hello") self.assertEqual(obj.m_prefix, "Hello")
def test_instance_set_attribute(self): obj = foo.SomeObject("") obj.m_prefix = "World" self.assertEqual(obj.m_prefix, "World")
def test_protected_method_no_subclassing(self): some = foo.SomeObject("xxx") self.assertTrue(hasattr(some, "protected_method_that_is_not_virtual")) self.assertRaises(TypeError, some.protected_method_that_is_not_virtual, "aax") del some
def test_overloaded_constructors(self): obj1 = foo.SomeObject("zbr") self.assertEqual(obj1.get_prefix(), "zbr") obj2 = foo.SomeObject(5) self.assertEqual(obj2.get_prefix(), "XXXXX") self.assertRaises(TypeError, foo.SomeObject, [123])
def test_attribute_with_getset(self): obj = foo.SomeObject("") f1 = foo.Foo("hello") obj.foo = f1 f2 = obj.foo self.assertEqual(f2.get_datum(), "hello")
def test_virtual_no_subclass(self): t = foo.SomeObject("xxx") self.assertEqual(t.call_get_prefix(), "xxx")