def test09_opaque_pointer_passing(self): """Test passing around of opaque pointers""" import _cppyy as cppyy some_concrete_class = cppyy.gbl.some_concrete_class o = some_concrete_class() # TODO: figure out the PyPy equivalent of CObject (may have to do this # through the C-API from C++) #cobj = cppyy.as_cobject(o) addr = cppyy.addressof(o) #assert o == cppyy.bind_object(cobj, some_concrete_class) #assert o == cppyy.bind_object(cobj, type(o)) #assert o == cppyy.bind_object(cobj, o.__class__) #assert o == cppyy.bind_object(cobj, "some_concrete_class") assert cppyy.addressof(o) == cppyy.addressof(cppyy.bind_object(addr, some_concrete_class)) assert o == cppyy.bind_object(addr, some_concrete_class) assert o == cppyy.bind_object(addr, type(o)) assert o == cppyy.bind_object(addr, o.__class__) assert o == cppyy.bind_object(addr, "some_concrete_class") raises(TypeError, cppyy.bind_object, addr, "does_not_exist") raises(TypeError, cppyy.bind_object, addr, 1)
def test08_void_pointer_passing(self): """Test passing of variants of void pointer arguments""" import _cppyy as cppyy pointer_pass = cppyy.gbl.pointer_pass some_concrete_class = cppyy.gbl.some_concrete_class pp = pointer_pass() o = some_concrete_class() assert cppyy.addressof(o) == pp.gime_address_ptr(o) assert cppyy.addressof(o) == pp.gime_address_ptr_ptr(o) assert cppyy.addressof(o) == pp.gime_address_ptr_ref(o) import array addressofo = array.array('l', [cppyy.addressof(o)]) assert addressofo[0] == pp.gime_address_ptr_ptr(addressofo) assert 0 == pp.gime_address_ptr(0) raises(TypeError, pp.gime_address_ptr, None) ptr = cppyy.bind_object(0, some_concrete_class) assert cppyy.addressof(ptr) == 0 pp.set_address_ptr_ref(ptr) assert cppyy.addressof(ptr) == 0x1234 pp.set_address_ptr_ptr(ptr) assert cppyy.addressof(ptr) == 0x4321 assert cppyy.addressof(cppyy.nullptr) == 0 raises(TypeError, cppyy.addressof, None) assert cppyy.addressof(0) == 0
def test10_object_identity(self): """Test object identity""" import _cppyy some_concrete_class = _cppyy.gbl.some_concrete_class some_class_with_data = _cppyy.gbl.some_class_with_data o = some_concrete_class() addr = _cppyy.addressof(o) o2 = _cppyy.bind_object(addr, some_concrete_class) assert o is o2 o3 = _cppyy.bind_object(addr, some_class_with_data) assert not o is o3 d1 = some_class_with_data() d2 = d1.gime_copy() assert not d1 is d2 dd1a = d1.gime_data() dd1b = d1.gime_data() assert dd1a is dd1b dd2 = d2.gime_data() assert not dd1a is dd2 assert not dd1b is dd2 d2.destruct() d1.destruct()
def test04_wrong_arg_addressof(self): """Test addressof() error reporting""" import _cppyy assert _cppyy.gbl.fragile == _cppyy.gbl.fragile fragile = _cppyy.gbl.fragile assert fragile.F == fragile.F assert fragile.F().check() == ord('F') f = fragile.F() o = object() _cppyy.addressof(f) raises(TypeError, _cppyy.addressof, o) raises(TypeError, _cppyy.addressof, 1)
def test08_void_pointer_passing(self): """Test passing of variants of void pointer arguments""" import _cppyy pointer_pass = _cppyy.gbl.pointer_pass some_concrete_class = _cppyy.gbl.some_concrete_class pp = pointer_pass() o = some_concrete_class() assert _cppyy.addressof(o) == pp.gime_address_ptr(o) assert _cppyy.addressof(o) == pp.gime_address_ptr_ptr(o) assert _cppyy.addressof(o) == pp.gime_address_ptr_ref(o) import array addressofo = array.array('l', [_cppyy.addressof(o)]) assert addressofo.buffer_info()[0] == pp.gime_address_ptr_ptr( addressofo) assert 0 == pp.gime_address_ptr(0) assert 0 == pp.gime_address_ptr(None) ptr = _cppyy.bind_object(0, some_concrete_class) assert _cppyy.addressof(ptr) == 0 pp.set_address_ptr_ref(ptr) assert _cppyy.addressof(ptr) == 0x1234 pp.set_address_ptr_ptr(ptr) assert _cppyy.addressof(ptr) == 0x4321
def test05_wrong_arg_addressof(self): """Test addressof() error reporting""" import _cppyy assert _cppyy.gbl.fragile == _cppyy.gbl.fragile fragile = _cppyy.gbl.fragile assert fragile.F == fragile.F assert fragile.F().check() == ord('F') f = fragile.F() o = object() _cppyy.addressof(f) raises(TypeError, _cppyy.addressof, o) raises(TypeError, _cppyy.addressof, 1) # 0, None, and nullptr allowed assert _cppyy.addressof(0) == 0 assert _cppyy.addressof(None) == 0 assert _cppyy.addressof(_cppyy.gbl.nullptr) == 0
def test12_actual_type(self): """Test that a pointer to base return does an auto-downcast""" import _cppyy as cppyy base_class = cppyy.gbl.base_class derived_class = cppyy.gbl.derived_class b = base_class() d = derived_class() assert b == b.cycle(b) assert id(b) == id(b.cycle(b)) assert b == d.cycle(b) assert id(b) == id(d.cycle(b)) assert d == b.cycle(d) assert id(d) == id(b.cycle(d)) assert d == d.cycle(d) assert id(d) == id(d.cycle(d)) assert isinstance(b.cycle(b), base_class) assert isinstance(d.cycle(b), base_class) assert isinstance(b.cycle(d), derived_class) assert isinstance(d.cycle(d), derived_class) assert isinstance(b.clone(), base_class) # TODO: clone() leaks assert isinstance(d.clone(), derived_class) # TODO: clone() leaks # special case when round-tripping through a void* ptr voidp = b.mask(d) assert not isinstance(voidp, base_class) assert not isinstance(voidp, derived_class) d1 = cppyy.bind_object(voidp, base_class, cast=True) assert isinstance(d1, derived_class) assert d1 is d b1 = cppyy.bind_object(voidp, base_class) assert isinstance(b1, base_class) assert cppyy.addressof(b1) == cppyy.addressof(d) assert not (b1 is d)
def test10_object_identity(self): """Test object identity""" import _cppyy as cppyy some_concrete_class = cppyy.gbl.some_concrete_class some_class_with_data = cppyy.gbl.some_class_with_data o = some_concrete_class() addr = cppyy.addressof(o) o2 = cppyy.bind_object(addr, some_concrete_class) assert o is o2 o3 = cppyy.bind_object(addr, some_class_with_data) assert not o is o3 d1 = some_class_with_data() d2 = d1.gime_copy() assert not d1 is d2 dd1a = d1.gime_data() dd1b = d1.gime_data() assert dd1a is dd1b dd2 = d2.gime_data() assert not dd1a is dd2 assert not dd1b is dd2 d2.__destruct__() d1.__destruct__() RTS = cppyy.gbl.refers_to_self r1 = RTS() r2 = RTS() r1.m_other = r2 r3 = r1.m_other r4 = r1.m_other assert r3 is r4 assert r3 == r2 assert r3 is r2 r3.extra = 42 assert r2.extra == 42 assert r4.extra == 42
def address_equality_test(a, b): assert cppyy.addressof(a) == cppyy.addressof(b) b2 = cppyy.bind_object(a, CppyyTestData) assert b is b2 # memory regulator recycles b3 = cppyy.bind_object(cppyy.addressof(a), CppyyTestData) assert b is b3 # likewise