def test_arrayofstruct(self): S1 = lltype.Struct('S1', ('x', lltype.Signed)) A = lltype.Array(S1, hints={'nolength': True}) a = lltype.malloc(A, 5, flavor='raw') a[0].x = 100 a[1].x = 101 a[2].x = 102 a[3].x = 103 a[4].x = 104 ac = lltype2ctypes(a, normalize=False) assert ac.contents.items[0].x == 100 assert ac.contents.items[2].x == 102 ac.contents.items[3].x += 500 assert a[3].x == 603 a[4].x += 600 assert ac.contents.items[4].x == 704 a1 = ctypes2lltype(lltype.Ptr(A), ac) assert a1 == a assert a1[2].x == 102 aitem1 = ctypes2lltype(lltype.Ptr(S1), ctypes.pointer(ac.contents.items[1])) assert aitem1.x == 101 assert aitem1 == a1[1] lltype.free(a, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_primitive(): assert lltype2ctypes(5) == 5 assert lltype2ctypes('?') == ord('?') assert lltype2ctypes('\xE0') == 0xE0 assert ctypes2lltype(lltype.Signed, 5) == 5 assert ctypes2lltype(lltype.Char, ord('a')) == 'a' assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF'
def test_arrayofstruct(self): S1 = lltype.Struct('S1', ('x', lltype.Signed)) A = lltype.Array(S1, hints={'nolength': True}) a = lltype.malloc(A, 5, flavor='raw') a[0].x = 100 a[1].x = 101 a[2].x = 102 a[3].x = 103 a[4].x = 104 ac = lltype2ctypes(a, normalize=False) assert ac.contents.items[0].x == 100 assert ac.contents.items[2].x == 102 ac.contents.items[3].x += 500 assert a[3].x == 603 a[4].x += 600 assert ac.contents.items[4].x == 704 a1 = ctypes2lltype(lltype.Ptr(A), ac) assert a1 == a assert a1[2].x == 102 aitem1 = ctypes2lltype(lltype.Ptr(S1), ctypes.pointer(ac.contents.items[1])) assert aitem1.x == 101 assert aitem1 == a1[1] lltype.free(a, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_gcref_truth(self): p0 = ctypes.c_void_p(0) ref0 = ctypes2lltype(llmemory.GCREF, p0) assert not ref0 p1234 = ctypes.c_void_p(1234) ref1234 = ctypes2lltype(llmemory.GCREF, p1234) assert p1234
def test_gcref_truth(self): p0 = ctypes.c_void_p(0) ref0 = ctypes2lltype(llmemory.GCREF, p0) assert not ref0 p1234 = ctypes.c_void_p(1234) ref1234 = ctypes2lltype(llmemory.GCREF, p1234) assert p1234
def test_gcref_forth_and_back(self): cp = ctypes.c_void_p(1234) v = ctypes2lltype(llmemory.GCREF, cp) assert lltype2ctypes(v).value == cp.value v1 = ctypes2lltype(llmemory.GCREF, cp) assert v == v1 assert v v2 = ctypes2lltype(llmemory.GCREF, ctypes.c_void_p(1235)) assert v2 != v
def test_gcref_forth_and_back(self): cp = ctypes.c_void_p(1234) v = ctypes2lltype(llmemory.GCREF, cp) assert lltype2ctypes(v).value == cp.value v1 = ctypes2lltype(llmemory.GCREF, cp) assert v == v1 assert v v2 = ctypes2lltype(llmemory.GCREF, ctypes.c_void_p(1235)) assert v2 != v
def test_primitive(self): assert lltype2ctypes(5) == 5 assert lltype2ctypes('?') == ord('?') assert lltype2ctypes('\xE0') == 0xE0 assert lltype2ctypes(unichr(1234)) == 1234 assert ctypes2lltype(lltype.Signed, 5) == 5 assert ctypes2lltype(lltype.Char, ord('a')) == 'a' assert ctypes2lltype(lltype.UniChar, ord(u'x')) == u'x' assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF' assert lltype2ctypes(5.25) == 5.25 assert ctypes2lltype(lltype.Float, 5.25) == 5.25 assert lltype2ctypes(u'x') == ord(u'x') res = lltype2ctypes(rffi.r_singlefloat(-3.5)) assert isinstance(res, ctypes.c_float) assert res.value == -3.5 res = ctypes2lltype(lltype.SingleFloat, ctypes.c_float(-3.5)) assert isinstance(res, rffi.r_singlefloat) assert float(res) == -3.5 assert lltype2ctypes(rffi.r_ulong(-1)) == sys.maxint * 2 + 1 res = ctypes2lltype(lltype.Unsigned, sys.maxint * 2 + 1) assert (res, type(res)) == (rffi.r_ulong(-1), rffi.r_ulong) res = lltype2ctypes(llmemory.sizeof(lltype.Signed)) assert res == struct.calcsize("l") S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed)) res = lltype2ctypes(llmemory.sizeof(S)) assert res == struct.calcsize("ll") p = lltype.nullptr(S) cptr = lltype2ctypes(p) assert not cptr py.test.raises(ValueError, 'cptr.contents') # NULL pointer access res = ctypes2lltype(lltype.Ptr(S), cptr) assert res == p assert not ALLOCATED # detects memory leaks in the test
def test_primitive(self): assert lltype2ctypes(5) == 5 assert lltype2ctypes('?') == ord('?') assert lltype2ctypes('\xE0') == 0xE0 assert lltype2ctypes(unichr(1234)) == 1234 assert ctypes2lltype(lltype.Signed, 5) == 5 assert ctypes2lltype(lltype.Char, ord('a')) == 'a' assert ctypes2lltype(lltype.UniChar, ord(u'x')) == u'x' assert ctypes2lltype(lltype.Char, 0xFF) == '\xFF' assert lltype2ctypes(5.25) == 5.25 assert ctypes2lltype(lltype.Float, 5.25) == 5.25 assert lltype2ctypes(u'x') == ord(u'x') res = lltype2ctypes(rffi.r_singlefloat(-3.5)) assert isinstance(res, ctypes.c_float) assert res.value == -3.5 res = ctypes2lltype(lltype.SingleFloat, ctypes.c_float(-3.5)) assert isinstance(res, rffi.r_singlefloat) assert float(res) == -3.5 assert lltype2ctypes(rffi.r_ulong(-1)) == sys.maxint * 2 + 1 res = ctypes2lltype(lltype.Unsigned, sys.maxint * 2 + 1) assert (res, type(res)) == (rffi.r_ulong(-1), rffi.r_ulong) res = lltype2ctypes(llmemory.sizeof(lltype.Signed)) assert res == struct.calcsize("l") S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed)) res = lltype2ctypes(llmemory.sizeof(S)) assert res == struct.calcsize("ll") p = lltype.nullptr(S) cptr = lltype2ctypes(p) assert not cptr py.test.raises(ValueError, 'cptr.contents') # NULL pointer access res = ctypes2lltype(lltype.Ptr(S), cptr) assert res == p assert not ALLOCATED # detects memory leaks in the test
def test_substructures(self): S1 = lltype.Struct('S1', ('x', lltype.Signed)) BIG = lltype.Struct('BIG', ('s1a', S1), ('s1b', S1)) s = lltype.malloc(BIG, flavor='raw') s.s1a.x = 123 s.s1b.x = 456 sc = lltype2ctypes(s) assert sc.contents.s1a.x == 123 assert sc.contents.s1b.x == 456 sc.contents.s1a.x += 1 sc.contents.s1b.x += 10 assert s.s1a.x == 124 assert s.s1b.x == 466 s.s1a.x += 3 s.s1b.x += 30 assert sc.contents.s1a.x == 127 assert sc.contents.s1b.x == 496 lltype.free(s, flavor='raw') s = lltype.malloc(BIG, flavor='raw') s1ac = lltype2ctypes(s.s1a) s1ac.contents.x = 53 sc = lltype2ctypes(s) assert sc.contents.s1a.x == 53 sc.contents.s1a.x += 1 assert s1ac.contents.x == 54 assert s.s1a.x == 54 s.s1a.x += 2 assert s1ac.contents.x == 56 assert sc.contents.s1a.x == 56 sc.contents.s1a.x += 3 assert s1ac.contents.x == 59 assert s.s1a.x == 59 t = ctypes2lltype(lltype.Ptr(BIG), sc) assert t == s assert t.s1a == s.s1a assert t.s1a.x == 59 s.s1b.x = 8888 assert t.s1b == s.s1b assert t.s1b.x == 8888 t1 = ctypes2lltype(lltype.Ptr(S1), s1ac) assert t.s1a == t1 assert t1.x == 59 t1.x += 1 assert sc.contents.s1a.x == 60 lltype.free(s, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_substructures(self): S1 = lltype.Struct('S1', ('x', lltype.Signed)) BIG = lltype.Struct('BIG', ('s1a', S1), ('s1b', S1)) s = lltype.malloc(BIG, flavor='raw') s.s1a.x = 123 s.s1b.x = 456 sc = lltype2ctypes(s) assert sc.contents.s1a.x == 123 assert sc.contents.s1b.x == 456 sc.contents.s1a.x += 1 sc.contents.s1b.x += 10 assert s.s1a.x == 124 assert s.s1b.x == 466 s.s1a.x += 3 s.s1b.x += 30 assert sc.contents.s1a.x == 127 assert sc.contents.s1b.x == 496 lltype.free(s, flavor='raw') s = lltype.malloc(BIG, flavor='raw') s1ac = lltype2ctypes(s.s1a) s1ac.contents.x = 53 sc = lltype2ctypes(s) assert sc.contents.s1a.x == 53 sc.contents.s1a.x += 1 assert s1ac.contents.x == 54 assert s.s1a.x == 54 s.s1a.x += 2 assert s1ac.contents.x == 56 assert sc.contents.s1a.x == 56 sc.contents.s1a.x += 3 assert s1ac.contents.x == 59 assert s.s1a.x == 59 t = ctypes2lltype(lltype.Ptr(BIG), sc) assert t == s assert t.s1a == s.s1a assert t.s1a.x == 59 s.s1b.x = 8888 assert t.s1b == s.s1b assert t.s1b.x == 8888 t1 = ctypes2lltype(lltype.Ptr(S1), s1ac) assert t.s1a == t1 assert t1.x == 59 t1.x += 1 assert sc.contents.s1a.x == 60 lltype.free(s, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_cstruct_to_ll(): S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed)) s = lltype.malloc(S, flavor='raw') s2 = lltype.malloc(S, flavor='raw') s.x = 123 sc = lltype2ctypes(s) t = ctypes2lltype(lltype.Ptr(S), sc) assert lltype.typeOf(t) == lltype.Ptr(S) assert s == t assert not (s != t) assert t == s assert not (t != s) assert t != lltype.nullptr(S) assert not (t == lltype.nullptr(S)) assert lltype.nullptr(S) != t assert not (lltype.nullptr(S) == t) assert t != s2 assert not (t == s2) assert s2 != t assert not (s2 == t) assert t.x == 123 t.x += 1 assert s.x == 124 s.x += 1 assert t.x == 125 lltype.free(s, flavor='raw') lltype.free(s2, flavor='raw')
def test_forced_ptr_cast(self): import array A = lltype.Array(lltype.Signed, hints={'nolength': True}) B = lltype.Array(lltype.Char, hints={'nolength': True}) a = lltype.malloc(A, 10, flavor='raw') for i in range(10): a[i] = i*i b = rffi.cast(lltype.Ptr(B), a) checker = array.array('l') for i in range(10): checker.append(i*i) expected = checker.tostring() for i in range(len(expected)): assert b[i] == expected[i] c = rffi.cast(rffi.VOIDP, a) addr = lltype2ctypes(c) #assert addr == ctypes.addressof(a._obj._ctypes_storage) d = ctypes2lltype(rffi.VOIDP, addr) assert lltype.typeOf(d) == rffi.VOIDP assert c == d e = rffi.cast(lltype.Ptr(A), d) for i in range(10): assert e[i] == i*i c = lltype.nullptr(rffi.VOIDP.TO) addr = rffi.cast(lltype.Signed, c) assert addr == 0 lltype.free(a, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_carray_to_ll(self): A = lltype.Array(lltype.Signed, hints={'nolength': True}) a = lltype.malloc(A, 10, flavor='raw') a2 = lltype.malloc(A, 10, flavor='raw') a[0] = 100 a[1] = 101 a[2] = 110 ac = lltype2ctypes(a) b = ctypes2lltype(lltype.Ptr(A), ac) assert lltype.typeOf(b) == lltype.Ptr(A) assert b == a assert not (b != a) assert a == b assert not (a != b) assert b != lltype.nullptr(A) assert not (b == lltype.nullptr(A)) assert lltype.nullptr(A) != b assert not (lltype.nullptr(A) == b) assert b != a2 assert not (b == a2) assert a2 != b assert not (a2 == b) assert b[2] == 110 b[2] *= 2 assert a[2] == 220 a[2] *= 3 assert b[2] == 660 lltype.free(a, flavor='raw') lltype.free(a2, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_carray_to_ll(): A = lltype.Array(lltype.Signed, hints={'nolength': True}) a = lltype.malloc(A, 10, flavor='raw') a2 = lltype.malloc(A, 10, flavor='raw') a[0] = 100 a[1] = 101 a[2] = 110 ac = lltype2ctypes(a) b = ctypes2lltype(lltype.Ptr(A), ac) assert lltype.typeOf(b) == lltype.Ptr(A) assert b == a assert not (b != a) assert a == b assert not (a != b) assert b != lltype.nullptr(A) assert not (b == lltype.nullptr(A)) assert lltype.nullptr(A) != b assert not (lltype.nullptr(A) == b) assert b != a2 assert not (b == a2) assert a2 != b assert not (a2 == b) assert b[2] == 110 b[2] *= 2 assert a[2] == 220 a[2] *= 3 assert b[2] == 660 lltype.free(a, flavor='raw') lltype.free(a2, flavor='raw')
def test_forced_ptr_cast(self): import array A = lltype.Array(lltype.Signed, hints={'nolength': True}) B = lltype.Array(lltype.Char, hints={'nolength': True}) a = lltype.malloc(A, 10, flavor='raw') for i in range(10): a[i] = i * i b = rffi.cast(lltype.Ptr(B), a) checker = array.array('l') for i in range(10): checker.append(i * i) expected = checker.tostring() for i in range(len(expected)): assert b[i] == expected[i] c = rffi.cast(rffi.VOIDP, a) addr = lltype2ctypes(c) #assert addr == ctypes.addressof(a._obj._ctypes_storage) d = ctypes2lltype(rffi.VOIDP, addr) assert lltype.typeOf(d) == rffi.VOIDP assert c == d e = rffi.cast(lltype.Ptr(A), d) for i in range(10): assert e[i] == i * i c = lltype.nullptr(rffi.VOIDP.TO) addr = rffi.cast(lltype.Signed, c) assert addr == 0 lltype.free(a, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_cstruct_to_ll(self): S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed)) s = lltype.malloc(S, flavor='raw') s2 = lltype.malloc(S, flavor='raw') s.x = 123 sc = lltype2ctypes(s) t = ctypes2lltype(lltype.Ptr(S), sc) assert lltype.typeOf(t) == lltype.Ptr(S) assert s == t assert not (s != t) assert t == s assert not (t != s) assert t != lltype.nullptr(S) assert not (t == lltype.nullptr(S)) assert lltype.nullptr(S) != t assert not (lltype.nullptr(S) == t) assert t != s2 assert not (t == s2) assert s2 != t assert not (s2 == t) assert t.x == 123 t.x += 1 assert s.x == 124 s.x += 1 assert t.x == 125 lltype.free(s, flavor='raw') lltype.free(s2, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_gcref_casts(self): p0 = ctypes.c_void_p(0) ref0 = ctypes2lltype(llmemory.GCREF, p0) assert lltype.cast_ptr_to_int(ref0) == 0 assert llmemory.cast_ptr_to_adr(ref0) == llmemory.NULL NODE = lltype.GcStruct('NODE') assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref0) == lltype.nullptr(NODE) node = lltype.malloc(NODE) ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node) intval = rffi.cast(lltype.Signed, node) intval1 = rffi.cast(lltype.Signed, ref1) assert intval == intval1 ref2 = ctypes2lltype(llmemory.GCREF, intval1) assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref2) == node
def test_gcref_casts(self): p0 = ctypes.c_void_p(0) ref0 = ctypes2lltype(llmemory.GCREF, p0) assert lltype.cast_ptr_to_int(ref0) == 0 assert llmemory.cast_ptr_to_adr(ref0) == llmemory.NULL NODE = lltype.GcStruct('NODE') assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref0) == lltype.nullptr(NODE) node = lltype.malloc(NODE) ref1 = lltype.cast_opaque_ptr(llmemory.GCREF, node) intval = rffi.cast(lltype.Signed, node) intval1 = rffi.cast(lltype.Signed, ref1) assert intval == intval1 ref2 = ctypes2lltype(llmemory.GCREF, intval1) assert lltype.cast_opaque_ptr(lltype.Ptr(NODE), ref2) == node
def test_indirect_recursive_struct_more(self): NODE = lltype.ForwardReference() NODE2 = lltype.Struct('NODE2', ('ping', lltype.Ptr(NODE))) NODE.become(lltype.Struct('NODE', ('pong', NODE2))) # Building NODE2 first used to fail. get_ctypes_type(NODE2) CNODEPTR = get_ctypes_type(NODE) pc = CNODEPTR() pc.pong.ping = ctypes.pointer(pc) p = ctypes2lltype(lltype.Ptr(NODE), ctypes.pointer(pc)) assert p.pong.ping == p
def test_indirect_recursive_struct_more(self): NODE = lltype.ForwardReference() NODE2 = lltype.Struct('NODE2', ('ping', lltype.Ptr(NODE))) NODE.become(lltype.Struct('NODE', ('pong', NODE2))) # Building NODE2 first used to fail. get_ctypes_type(NODE2) CNODEPTR = get_ctypes_type(NODE) pc = CNODEPTR() pc.pong.ping = ctypes.pointer(pc) p = ctypes2lltype(lltype.Ptr(NODE), ctypes.pointer(pc)) assert p.pong.ping == p
def test_c_callback_with_void_arg_2(self): ftest = [] def f(x): ftest.append(x) F = lltype.FuncType([lltype.Void], lltype.Void) fn = lltype.functionptr(F, 'askjh', _callable=f, _void0=-5) fn(-5) assert ftest == [-5] fn2 = lltype2ctypes(fn) fn2() assert ftest == [-5, -5] fn3 = ctypes2lltype(lltype.Ptr(F), fn2) fn3(-5) assert ftest == [-5, -5, -5]
def test_recursive_struct_more(self): NODE = lltype.ForwardReference() NODE.become(lltype.Struct('NODE', ('value', lltype.Signed), ('next', lltype.Ptr(NODE)))) CNODEPTR = get_ctypes_type(NODE) pc = CNODEPTR() pc.value = 42 pc.next = ctypes.pointer(pc) p = ctypes2lltype(lltype.Ptr(NODE), ctypes.pointer(pc)) assert p.value == 42 assert p.next == p pc2 = lltype2ctypes(p) assert pc2.contents.value == 42 assert pc2.contents.next.contents.value == 42
def test_funcptr2(self): FUNCTYPE = lltype.FuncType([rffi.CCHARP], lltype.Signed) cstrlen = standard_c_lib.strlen llstrlen = ctypes2lltype(lltype.Ptr(FUNCTYPE), cstrlen) assert lltype.typeOf(llstrlen) == lltype.Ptr(FUNCTYPE) p = rffi.str2charp("hi there") res = llstrlen(p) assert res == 8 cstrlen2 = lltype2ctypes(llstrlen) cp = lltype2ctypes(p) assert cstrlen2.restype == ctypes.c_long res = cstrlen2(cp) assert res == 8 rffi.free_charp(p) assert not ALLOCATED # detects memory leaks in the test
def test_funcptr1(self): def dummy(n): return n + 1 FUNCTYPE = lltype.FuncType([lltype.Signed], lltype.Signed) cdummy = lltype2ctypes(llhelper(lltype.Ptr(FUNCTYPE), dummy)) assert isinstance(cdummy, ctypes.CFUNCTYPE(ctypes.c_long, ctypes.c_long)) res = cdummy(41) assert res == 42 lldummy = ctypes2lltype(lltype.Ptr(FUNCTYPE), cdummy) assert lltype.typeOf(lldummy) == lltype.Ptr(FUNCTYPE) res = lldummy(41) assert res == 42 assert not ALLOCATED # detects memory leaks in the test
def test_funcptr2(self): FUNCTYPE = lltype.FuncType([rffi.CCHARP], lltype.Signed) cstrlen = standard_c_lib.strlen llstrlen = ctypes2lltype(lltype.Ptr(FUNCTYPE), cstrlen) assert lltype.typeOf(llstrlen) == lltype.Ptr(FUNCTYPE) p = rffi.str2charp("hi there") res = llstrlen(p) assert res == 8 cstrlen2 = lltype2ctypes(llstrlen) cp = lltype2ctypes(p) assert cstrlen2.restype == ctypes.c_long res = cstrlen2(cp) assert res == 8 rffi.free_charp(p) assert not ALLOCATED # detects memory leaks in the test
def test_recursive_struct_more(self): NODE = lltype.ForwardReference() NODE.become( lltype.Struct('NODE', ('value', lltype.Signed), ('next', lltype.Ptr(NODE)))) CNODEPTR = get_ctypes_type(NODE) pc = CNODEPTR() pc.value = 42 pc.next = ctypes.pointer(pc) p = ctypes2lltype(lltype.Ptr(NODE), ctypes.pointer(pc)) assert p.value == 42 assert p.next == p pc2 = lltype2ctypes(p) assert pc2.contents.value == 42 assert pc2.contents.next.contents.value == 42
def test_funcptr1(self): def dummy(n): return n+1 FUNCTYPE = lltype.FuncType([lltype.Signed], lltype.Signed) cdummy = lltype2ctypes(llhelper(lltype.Ptr(FUNCTYPE), dummy)) assert isinstance(cdummy, ctypes.CFUNCTYPE(ctypes.c_long, ctypes.c_long)) res = cdummy(41) assert res == 42 lldummy = ctypes2lltype(lltype.Ptr(FUNCTYPE), cdummy) assert lltype.typeOf(lldummy) == lltype.Ptr(FUNCTYPE) res = lldummy(41) assert res == 42 assert not ALLOCATED # detects memory leaks in the test
def test_arrayoffloat(self): a = lltype.malloc(rffi.FLOATP.TO, 3, flavor='raw') a[0] = rffi.r_singlefloat(0.0) a[1] = rffi.r_singlefloat(1.1) a[2] = rffi.r_singlefloat(2.2) ac = lltype2ctypes(a, normalize=False) assert ac.contents.items[0] == 0.0 assert abs(ac.contents.items[1] - 1.1) < 1E-6 assert abs(ac.contents.items[2] - 2.2) < 1E-6 b = ctypes2lltype(rffi.FLOATP, ac) assert isinstance(b[0], rffi.r_singlefloat) assert float(b[0]) == 0.0 assert isinstance(b[1], rffi.r_singlefloat) assert abs(float(b[1]) - 1.1) < 1E-6 assert isinstance(b[2], rffi.r_singlefloat) assert abs(float(b[2]) - 2.2) < 1E-6
def test_arrayoffloat(self): a = lltype.malloc(rffi.FLOATP.TO, 3, flavor='raw') a[0] = rffi.r_singlefloat(0.0) a[1] = rffi.r_singlefloat(1.1) a[2] = rffi.r_singlefloat(2.2) ac = lltype2ctypes(a, normalize=False) assert ac.contents.items[0] == 0.0 assert abs(ac.contents.items[1] - 1.1) < 1E-6 assert abs(ac.contents.items[2] - 2.2) < 1E-6 b = ctypes2lltype(rffi.FLOATP, ac) assert isinstance(b[0], rffi.r_singlefloat) assert float(b[0]) == 0.0 assert isinstance(b[1], rffi.r_singlefloat) assert abs(float(b[1]) - 1.1) < 1E-6 assert isinstance(b[2], rffi.r_singlefloat) assert abs(float(b[2]) - 2.2) < 1E-6
def test_c_callback_with_void_arg_2(self): ftest = [] def f(x): ftest.append(x) F = lltype.FuncType([lltype.Void], lltype.Void) fn = lltype.functionptr(F, 'askjh', _callable=f, _void0=-5) fn(-5) assert ftest == [-5] fn2 = lltype2ctypes(fn) fn2() assert ftest == [-5, -5] fn3 = ctypes2lltype(lltype.Ptr(F), fn2) fn3(-5) assert ftest == [-5, -5, -5]
def test_opaque_tagged_pointers(self): from pypy.rpython.annlowlevel import cast_base_ptr_to_instance from pypy.rpython.annlowlevel import cast_instance_to_base_ptr from pypy.rpython.lltypesystem import rclass class Opaque(object): llopaque = True def hide(self): ptr = cast_instance_to_base_ptr(self) return lltype.cast_opaque_ptr(llmemory.GCREF, ptr) @staticmethod def show(gcref): ptr = lltype.cast_opaque_ptr(lltype.Ptr(rclass.OBJECT), gcref) return cast_base_ptr_to_instance(Opaque, ptr) opaque = Opaque() round = ctypes2lltype(llmemory.GCREF, lltype2ctypes(opaque.hide())) assert Opaque.show(round) is opaque
def test_ll2ctypes_array_from_c(self): from pypy.rpython.lltypesystem import rffi, ll2ctypes A = rffi.CArray(Char) a = malloc(A, 6, flavor='raw', immortal=True) a[0] = 'a' a[1] = 'b' a[2] = 'c' a[3] = 'd' a[4] = '\x00' a[5] = '\x00' # side effects when converting to c structure c = ll2ctypes.lltype2ctypes(a) a = ll2ctypes.ctypes2lltype(Ptr(A), c) def llf(): s = '' for i in range(4): s += a[i] print s return s == 'abcd' fn = self.getcompiled(llf) assert fn()
def test_ll2ctypes_array_from_c(self): from pypy.rpython.lltypesystem import rffi, ll2ctypes A = rffi.CArray(Char) a = malloc(A, 6, flavor='raw') a[0] = 'a' a[1] = 'b' a[2] = 'c' a[3] = 'd' a[4] = '\x00' a[5] = '\x00' # side effects when converting to c structure c = ll2ctypes.lltype2ctypes(a) a = ll2ctypes.ctypes2lltype(Ptr(A), c) def llf(): s = '' for i in range(4): s += a[i] print s return s == 'abcd' fn = self.getcompiled(llf) assert fn()
def test_varsized_struct(self): S = lltype.Struct('S', ('x', lltype.Signed), ('a', lltype.Array(lltype.Char))) s1 = lltype.malloc(S, 6, flavor='raw') s1.x = 5 s1.a[2] = 'F' sc = lltype2ctypes(s1, normalize=False) assert isinstance(sc.contents, ctypes.Structure) assert sc.contents.x == 5 assert sc.contents.a.length == 6 assert sc.contents.a.items[2] == ord('F') sc.contents.a.items[3] = ord('P') assert s1.a[3] == 'P' s1.a[1] = 'y' assert sc.contents.a.items[1] == ord('y') # now go back to lltype... res = ctypes2lltype(lltype.Ptr(S), sc) assert res == s1 assert res.x == 5 assert len(res.a) == 6 lltype.free(s1, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_with_explicit_length(self): A = lltype.Array(lltype.Signed) a1 = lltype.malloc(A, 5, flavor='raw') a1[0] = 42 c1 = lltype2ctypes(a1, normalize=False) assert c1.contents.length == 5 assert c1.contents.items[0] == 42 res = ctypes2lltype(lltype.Ptr(A), c1) assert res == a1 assert len(res) == 5 assert res[0] == 42 res[0] += 1 assert c1.contents.items[0] == 43 assert a1[0] == 43 a1[0] += 2 assert c1.contents.items[0] == 45 assert a1[0] == 45 c1.contents.items[0] += 3 assert res[0] == 48 assert a1[0] == 48 lltype.free(a1, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_with_explicit_length(self): A = lltype.Array(lltype.Signed) a1 = lltype.malloc(A, 5, flavor='raw') a1[0] = 42 c1 = lltype2ctypes(a1, normalize=False) assert c1.contents.length == 5 assert c1.contents.items[0] == 42 res = ctypes2lltype(lltype.Ptr(A), c1) assert res == a1 assert len(res) == 5 assert res[0] == 42 res[0] += 1 assert c1.contents.items[0] == 43 assert a1[0] == 43 a1[0] += 2 assert c1.contents.items[0] == 45 assert a1[0] == 45 c1.contents.items[0] += 3 assert res[0] == 48 assert a1[0] == 48 lltype.free(a1, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_varsized_struct(self): S = lltype.Struct('S', ('x', lltype.Signed), ('a', lltype.Array(lltype.Char))) s1 = lltype.malloc(S, 6, flavor='raw') s1.x = 5 s1.a[2] = 'F' sc = lltype2ctypes(s1, normalize=False) assert isinstance(sc.contents, ctypes.Structure) assert sc.contents.x == 5 assert sc.contents.a.length == 6 assert sc.contents.a.items[2] == ord('F') sc.contents.a.items[3] = ord('P') assert s1.a[3] == 'P' s1.a[1] = 'y' assert sc.contents.a.items[1] == ord('y') # now go back to lltype... res = ctypes2lltype(lltype.Ptr(S), sc) assert res == s1 assert res.x == 5 assert len(res.a) == 6 lltype.free(s1, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
def test_typedef(self): assert ctypes2lltype(lltype.Typedef(lltype.Signed, 'test'), 6) == 6 assert ctypes2lltype(lltype.Typedef(lltype.Float, 'test2'), 3.4) == 3.4 assert get_ctypes_type(lltype.Signed) == get_ctypes_type( lltype.Typedef(lltype.Signed, 'test3'))
def build_bridge(space): "NOT_RPYTHON" from pypy.module.cpyext.pyobject import make_ref export_symbols = list(FUNCTIONS) + SYMBOLS_C + list(GLOBALS) from pypy.translator.c.database import LowLevelDatabase db = LowLevelDatabase() generate_macros(export_symbols, rename=True, do_deref=True) # Structure declaration code members = [] structindex = {} for name, func in sorted(FUNCTIONS.iteritems()): restype, args = c_function_signature(db, func) members.append('%s (*%s)(%s);' % (restype, name, args)) structindex[name] = len(structindex) structmembers = '\n'.join(members) struct_declaration_code = """\ struct PyPyAPI { %(members)s } _pypyAPI; struct PyPyAPI* pypyAPI = &_pypyAPI; """ % dict(members=structmembers) functions = generate_decls_and_callbacks(db, export_symbols) global_objects = [] for name, (typ, expr) in GLOBALS.iteritems(): if "#" in name: continue if typ == 'PyDateTime_CAPI*': continue elif name.startswith('PyExc_'): global_objects.append('%s _%s;' % (typ[:-1], name)) else: global_objects.append('%s %s = NULL;' % (typ, name)) global_code = '\n'.join(global_objects) prologue = "#include <Python.h>\n" code = (prologue + struct_declaration_code + global_code + '\n' + '\n'.join(functions)) eci = build_eci(True, export_symbols, code) eci = eci.compile_shared_lib(outputfilename=str(udir / "module_cache" / "pypyapi")) modulename = py.path.local(eci.libraries[-1]) run_bootstrap_functions(space) # load the bridge, and init structure import ctypes bridge = ctypes.CDLL(str(modulename), mode=ctypes.RTLD_GLOBAL) space.fromcache(State).install_dll(eci) # populate static data for name, (typ, expr) in GLOBALS.iteritems(): from pypy.module import cpyext w_obj = eval(expr) if name.endswith('#'): name = name[:-1] isptr = False else: isptr = True if name.startswith('PyExc_'): isptr = False INTERPLEVEL_API[name] = w_obj name = name.replace('Py', 'PyPy') if isptr: ptr = ctypes.c_void_p.in_dll(bridge, name) if typ == 'PyObject*': value = make_ref(space, w_obj) elif typ == 'PyDateTime_CAPI*': value = w_obj else: assert False, "Unknown static pointer: %s %s" % (typ, name) ptr.value = ctypes.cast(ll2ctypes.lltype2ctypes(value), ctypes.c_void_p).value elif typ in ('PyObject*', 'PyTypeObject*'): if name.startswith('PyPyExc_'): # we already have the pointer in_dll = ll2ctypes.get_ctypes_type(PyObject).in_dll( bridge, name) py_obj = ll2ctypes.ctypes2lltype(PyObject, in_dll) else: # we have a structure, get its address in_dll = ll2ctypes.get_ctypes_type(PyObject.TO).in_dll( bridge, name) py_obj = ll2ctypes.ctypes2lltype(PyObject, ctypes.pointer(in_dll)) from pypy.module.cpyext.pyobject import (track_reference, get_typedescr) w_type = space.type(w_obj) typedescr = get_typedescr(w_type.instancetypedef) py_obj.c_ob_refcnt = 1 py_obj.c_ob_type = rffi.cast(PyTypeObjectPtr, make_ref(space, w_type)) typedescr.attach(space, py_obj, w_obj) track_reference(space, py_obj, w_obj) else: assert False, "Unknown static object: %s %s" % (typ, name) pypyAPI = ctypes.POINTER(ctypes.c_void_p).in_dll(bridge, 'pypyAPI') # implement structure initialization code for name, func in FUNCTIONS.iteritems(): if name.startswith('cpyext_'): # XXX hack continue pypyAPI[structindex[name]] = ctypes.cast( ll2ctypes.lltype2ctypes(func.get_llhelper(space)), ctypes.c_void_p) setup_va_functions(eci) setup_init_functions(eci) return modulename.new(ext='')
def build_bridge(space): "NOT_RPYTHON" from pypy.module.cpyext.pyobject import make_ref export_symbols = list(FUNCTIONS) + SYMBOLS_C + list(GLOBALS) from pypy.translator.c.database import LowLevelDatabase db = LowLevelDatabase() generate_macros(export_symbols, rename=True, do_deref=True) # Structure declaration code members = [] structindex = {} for name, func in sorted(FUNCTIONS.iteritems()): restype, args = c_function_signature(db, func) members.append('%s (*%s)(%s);' % (restype, name, args)) structindex[name] = len(structindex) structmembers = '\n'.join(members) struct_declaration_code = """\ struct PyPyAPI { %(members)s } _pypyAPI; struct PyPyAPI* pypyAPI = &_pypyAPI; """ % dict(members=structmembers) functions = generate_decls_and_callbacks(db, export_symbols) global_objects = [] for name, (typ, expr) in GLOBALS.iteritems(): if "#" in name: continue if typ == 'PyDateTime_CAPI*': continue elif name.startswith('PyExc_'): global_objects.append('%s _%s;' % (typ[:-1], name)) else: global_objects.append('%s %s = NULL;' % (typ, name)) global_code = '\n'.join(global_objects) prologue = "#include <Python.h>\n" code = (prologue + struct_declaration_code + global_code + '\n' + '\n'.join(functions)) eci = build_eci(True, export_symbols, code) eci = eci.compile_shared_lib( outputfilename=str(udir / "module_cache" / "pypyapi")) modulename = py.path.local(eci.libraries[-1]) run_bootstrap_functions(space) # load the bridge, and init structure import ctypes bridge = ctypes.CDLL(str(modulename), mode=ctypes.RTLD_GLOBAL) space.fromcache(State).install_dll(eci) # populate static data for name, (typ, expr) in GLOBALS.iteritems(): from pypy.module import cpyext w_obj = eval(expr) if name.endswith('#'): name = name[:-1] isptr = False else: isptr = True if name.startswith('PyExc_'): isptr = False INTERPLEVEL_API[name] = w_obj name = name.replace('Py', 'PyPy') if isptr: ptr = ctypes.c_void_p.in_dll(bridge, name) if typ == 'PyObject*': value = make_ref(space, w_obj) elif typ == 'PyDateTime_CAPI*': value = w_obj else: assert False, "Unknown static pointer: %s %s" % (typ, name) ptr.value = ctypes.cast(ll2ctypes.lltype2ctypes(value), ctypes.c_void_p).value elif typ in ('PyObject*', 'PyTypeObject*'): if name.startswith('PyPyExc_'): # we already have the pointer in_dll = ll2ctypes.get_ctypes_type(PyObject).in_dll(bridge, name) py_obj = ll2ctypes.ctypes2lltype(PyObject, in_dll) else: # we have a structure, get its address in_dll = ll2ctypes.get_ctypes_type(PyObject.TO).in_dll(bridge, name) py_obj = ll2ctypes.ctypes2lltype(PyObject, ctypes.pointer(in_dll)) from pypy.module.cpyext.pyobject import ( track_reference, get_typedescr) w_type = space.type(w_obj) typedescr = get_typedescr(w_type.instancetypedef) py_obj.c_ob_refcnt = 1 py_obj.c_ob_type = rffi.cast(PyTypeObjectPtr, make_ref(space, w_type)) typedescr.attach(space, py_obj, w_obj) track_reference(space, py_obj, w_obj) else: assert False, "Unknown static object: %s %s" % (typ, name) pypyAPI = ctypes.POINTER(ctypes.c_void_p).in_dll(bridge, 'pypyAPI') # implement structure initialization code for name, func in FUNCTIONS.iteritems(): if name.startswith('cpyext_'): # XXX hack continue pypyAPI[structindex[name]] = ctypes.cast( ll2ctypes.lltype2ctypes(func.get_llhelper(space)), ctypes.c_void_p) setup_va_functions(eci) setup_init_functions(eci) return modulename.new(ext='')