def test09_opaque_pointer_passing(self): """Test passing around of opaque pointers""" import 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 test09_opaque_pointer_passing(self): """Test passing around of opaque pointers""" import 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 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 address_equality_test(a, b): assert cppyy.addressof(a) == cppyy.addressof(b) b2 = cppyy.bind_object(a, CppyyTestData) b.m_int = 888 assert b.m_int == 888 assert b == b2 and b.m_int == b2.m_int # TODO: in PyROOT, non-TObjects are not mem-regulated #assert b is b2 # memory regulator recycles b3 = cppyy.bind_object(cppyy.addressof(a), CppyyTestData) assert b3.m_int == 888 assert b == b3 and b.m_int == b3.m_int
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)
def keep(self, obj): try: objid = (OBJTYPE_CPPYY, cppyy.addressof(obj)) except TypeError: objid = (OBJTYPE_PYTHON, id(obj)) self._objects.setdefault(objid, [0, obj])[0] += 1 return obj
def test16_opaque_handle(self): """Support use of opaque handles""" import cppyy assert cppyy.gbl.fragile.OpaqueType assert cppyy.gbl.fragile.OpaqueHandle_t handle = cppyy.gbl.fragile.OpaqueHandle_t(0x42) assert handle assert cppyy.addressof(handle) == 0x42 raises(TypeError, cppyy.gbl.fragile.OpaqueType) assert not 'OpaqueType' in cppyy.gbl.fragile.__dict__ handle = cppyy.gbl.fragile.OpaqueHandle_t() assert not handle addr = cppyy.gbl.fragile.create_handle(handle) assert addr assert not not handle assert cppyy.gbl.fragile.destroy_handle(handle, addr) # now define OpaqueType cppyy.cppdef( "namespace fragile { class OpaqueType { public: int m_int; }; }") # get fresh (should not have been cached while incomplete) o = cppyy.gbl.fragile.OpaqueType() assert hasattr(o, 'm_int') assert 'OpaqueType' in cppyy.gbl.fragile.__dict__
def test05_ptr_ptr_with_array(self): """Example of ptr-ptr with array""" import cppyy, ctypes cppyy.cppdef("""namespace Advert05 { struct SomeStruct { int i; }; void create_them(SomeStruct** them, int* sz) { *sz = 4; *them = new SomeStruct[*sz]; for (int i = 0; i < *sz; ++i) (*them)[i].i = i*i; } }""") cppyy.gbl.Advert05 from cppyy.gbl.Advert05 import SomeStruct, create_them ptr = cppyy.bind_object(cppyy.nullptr, SomeStruct) sz = ctypes.c_int(0) create_them(ptr, sz) arr = cppyy.bind_object(cppyy.addressof(ptr), cppyy.gbl.std.array[SomeStruct, sz.value]) total = 0 for s in arr: total += s.i assert total == 14
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 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)
def main(args): mak, arr = load_argos() tree = ET.parse(args.config) root = tree.getroot() controllers = root.find("controllers") if controllers: for controller in controllers: id = controller.get('id') controller.set("library", config.CONTROLLERS[id]) path = pathlib.Path(controller.get("library")) print(path) if args.gen and id == "nn_rm_1dot1": print("Adding gen file") controller.find('params').set('genome_file', args.gen) elif args.fsm_config and id == "automode": print('Adding fsm config') controller.find('params').set('fsm-config', args.fsm_config) loops = root.find("loop_functions") if loops: findloop = pathlib.Path(config.LOOP_PREFIX).glob("**/*.so") old_loop_path = pathlib.Path(loops.get("library")) looplist = [ loop for loop in findloop if loop.name == old_loop_path.name ] if len(looplist) == 0: print('Could not find the loopfunc in the prefix') raise looppath = looplist[0] if len(looplist) > 1: print('multiple possible loopfunc file found: {}'.format(looplist)) print('Using loopfunc: {}'.format(looppath)) loops.set('library', str(looppath.absolute())) # print(looppath) tmpfile = tempfile.NamedTemporaryFile() tree.write(tmpfile.name) mak.SetExperimentFileName(tmpfile.name) mak.SetRandomSeed(int(args.seed)) mak.LoadExperiment() # print(type(mak), type(args.gen)) cSpace = mak.GetSpace() mak.Execute() loopfunc = mak.GetLoopFunctions() from cppyy.gbl import CoreLoopFunctions loopfunc = bind_object(addressof(loopfunc), CoreLoopFunctions) score = loopfunc.GetObjectiveFunction() print(score) mak.Destroy() tmpfile.close()
def test09_opaque_pointer_assing(self): """Test passing around of opaque pointers""" import cppyy some_concrete_class = cppyy.gbl.some_concrete_class o = some_concrete_class() #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__)
def test12_actual_type(self): """Test that a pointer to base return does an auto-downcast""" import cppyy base_class = cppyy.gbl.base_class base_class.clone.__creates__ = True derived_class = cppyy.gbl.derived_class derived_class.clone.__creates__ = True 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 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) # regression (m_int is 0 by default, but its address is not) assert cppyy.addressof(f, 'm_int')
def test_casting(self): import cppyy from cppyy.gbl import Abstract, Concrete c = Concrete() assert 'Abstract' in Concrete.show_autocast.__doc__ d = c.show_autocast() assert type(d) == cppyy.gbl.Concrete from cppyy import addressof, bind_object e = bind_object(addressof(d), Abstract) assert type(e) == cppyy.gbl.Abstract
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 release(self, obj): try: objid = (OBJTYPE_CPPYY, cppyy.addressof(obj)) except TypeError: objid = (OBJTYPE_PYTHON, id(obj)) self._objects.setdefault(objid, [0, obj])[0] += 1 info = self._objects[objid] info[0] -= 1 if info[0] <= 0: del self._objects[objid] return True return False
def test12_actual_type(self): """Test that a pointer to base return does an auto-downcast""" import 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 test08_opaque_pointer_passing(self): """Test passing around of opaque pointers""" import cppyy # TODO: figure out CObject (see also test_advanced.py) s = cppyy.gbl.TString("Hello World!") #cobj = cppyy.as_cobject(s) addr = cppyy.addressof(s) #assert s == cppyy.bind_object(cobj, s.__class__) #assert s == cppyy.bind_object(cobj, "TString") assert s == cppyy.bind_object(addr, s.__class__) assert s == cppyy.bind_object(addr, "TString")
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__() 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 __repr__(self): return "<{} object at 0x{:x}>".format(self.__class__.__name__, cppyy.addressof(self))
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
def setUserPointer(self, obj): global userpointers userpointers[cppyy.addressof(self)] = obj
def getUserPointer(self): global userpointers return userpointers.get(cppyy.addressof(self))
def address_equality_test(a, b): assert cppyy.addressof(a) == cppyy.addressof(b) b2 = cppyy.bind_object(a, cppyy_test_data) assert b is b2 # memory regulator recycles b3 = cppyy.bind_object(cppyy.addressof(a), cppyy_test_data) assert b is b3 # likewise
def wrapper(*args, **kwargs): result = func(*args, **kwargs) if cppyy.addressof(result) == 0: return None return result