Ejemplo n.º 1
0
 def test_array_type_bug(self):
     A = lltype.Array(lltype.Signed)
     a1 = lltype.malloc(A, 0, flavor='raw')
     a2 = lltype.malloc(A, 0, flavor='raw')
     c1 = lltype2ctypes(a1)
     c2 = lltype2ctypes(a2)
     assert type(c1) is type(c2)
Ejemplo n.º 2
0
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'
Ejemplo n.º 3
0
 def test_array_type_bug(self):
     A = lltype.Array(lltype.Signed)
     a1 = lltype.malloc(A, 0, flavor='raw')
     a2 = lltype.malloc(A, 0, flavor='raw')
     c1 = lltype2ctypes(a1)
     c2 = lltype2ctypes(a2)
     assert type(c1) is type(c2)
Ejemplo n.º 4
0
 def test_recursive_struct(self):
     SX = lltype.ForwardReference()
     S1 = lltype.Struct('S1', ('p', lltype.Ptr(SX)), ('x', lltype.Signed))
     SX.become(S1)
     # a chained list
     s1 = lltype.malloc(S1, flavor='raw')
     s2 = lltype.malloc(S1, flavor='raw')
     s3 = lltype.malloc(S1, flavor='raw')
     s1.x = 111
     s2.x = 222
     s3.x = 333
     s1.p = s2
     s2.p = s3
     s3.p = lltype.nullptr(S1)
     sc1 = lltype2ctypes(s1)
     sc2 = sc1.contents.p
     sc3 = sc2.contents.p
     assert not sc3.contents.p
     assert sc1.contents.x == 111
     assert sc2.contents.x == 222
     assert sc3.contents.x == 333
     sc3.contents.x += 1
     assert s3.x == 334
     s3.x += 2
     assert sc3.contents.x == 336
     lltype.free(s1, flavor='raw')
     lltype.free(s2, flavor='raw')
     lltype.free(s3, flavor='raw')
     # a self-cycle
     s1 = lltype.malloc(S1, flavor='raw')
     s1.x = 12
     s1.p = s1
     sc1 = lltype2ctypes(s1)
     assert sc1.contents.x == 12
     assert (ctypes.addressof(sc1.contents.p.contents) == ctypes.addressof(
         sc1.contents))
     s1.x *= 5
     assert sc1.contents.p.contents.p.contents.p.contents.x == 60
     lltype.free(s1, flavor='raw')
     # a longer cycle
     s1 = lltype.malloc(S1, flavor='raw')
     s2 = lltype.malloc(S1, flavor='raw')
     s1.x = 111
     s1.p = s2
     s2.x = 222
     s2.p = s1
     sc1 = lltype2ctypes(s1)
     assert sc1.contents.x == 111
     assert sc1.contents.p.contents.x == 222
     assert (ctypes.addressof(sc1.contents.p.contents) != ctypes.addressof(
         sc1.contents))
     assert (ctypes.addressof(
         sc1.contents.p.contents.p.contents) == ctypes.addressof(
             sc1.contents))
     lltype.free(s1, flavor='raw')
     lltype.free(s2, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 5
0
 def test_recursive_struct(self):
     SX = lltype.ForwardReference()
     S1 = lltype.Struct('S1', ('p', lltype.Ptr(SX)), ('x', lltype.Signed))
     SX.become(S1)
     # a chained list
     s1 = lltype.malloc(S1, flavor='raw')
     s2 = lltype.malloc(S1, flavor='raw')
     s3 = lltype.malloc(S1, flavor='raw')
     s1.x = 111
     s2.x = 222
     s3.x = 333
     s1.p = s2
     s2.p = s3
     s3.p = lltype.nullptr(S1)
     sc1 = lltype2ctypes(s1)
     sc2 = sc1.contents.p
     sc3 = sc2.contents.p
     assert not sc3.contents.p
     assert sc1.contents.x == 111
     assert sc2.contents.x == 222
     assert sc3.contents.x == 333
     sc3.contents.x += 1
     assert s3.x == 334
     s3.x += 2
     assert sc3.contents.x == 336
     lltype.free(s1, flavor='raw')
     lltype.free(s2, flavor='raw')
     lltype.free(s3, flavor='raw')
     # a self-cycle
     s1 = lltype.malloc(S1, flavor='raw')
     s1.x = 12
     s1.p = s1
     sc1 = lltype2ctypes(s1)
     assert sc1.contents.x == 12
     assert (ctypes.addressof(sc1.contents.p.contents) ==
             ctypes.addressof(sc1.contents))
     s1.x *= 5
     assert sc1.contents.p.contents.p.contents.p.contents.x == 60
     lltype.free(s1, flavor='raw')
     # a longer cycle
     s1 = lltype.malloc(S1, flavor='raw')
     s2 = lltype.malloc(S1, flavor='raw')
     s1.x = 111
     s1.p = s2
     s2.x = 222
     s2.p = s1
     sc1 = lltype2ctypes(s1)
     assert sc1.contents.x == 111
     assert sc1.contents.p.contents.x == 222
     assert (ctypes.addressof(sc1.contents.p.contents) !=
             ctypes.addressof(sc1.contents))
     assert (ctypes.addressof(sc1.contents.p.contents.p.contents) ==
             ctypes.addressof(sc1.contents))
     lltype.free(s1, flavor='raw')
     lltype.free(s2, flavor='raw')
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 6
0
 def test_array_type_bug(self):
     A = lltype.Array(lltype.Signed)
     a1 = lltype.malloc(A, 0, flavor='raw')
     a2 = lltype.malloc(A, 0, flavor='raw')
     c1 = lltype2ctypes(a1)
     c2 = lltype2ctypes(a2)
     assert type(c1) is type(c2)
     lltype.free(a1, flavor='raw')
     lltype.free(a2, flavor='raw')
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 7
0
 def test_array_type_bug(self):
     A = lltype.Array(lltype.Signed)
     a1 = lltype.malloc(A, 0, flavor='raw')
     a2 = lltype.malloc(A, 0, flavor='raw')
     c1 = lltype2ctypes(a1)
     c2 = lltype2ctypes(a2)
     assert type(c1) is type(c2)
     lltype.free(a1, flavor='raw')
     lltype.free(a2, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 8
0
    def test_convert_subarray(self):
        A = lltype.GcArray(lltype.Signed)
        a = lltype.malloc(A, 20)
        inside = lltype.direct_ptradd(lltype.direct_arrayitems(a), 3)
 
        lltype2ctypes(inside)

        start = rffi.cast(lltype.Signed, lltype.direct_arrayitems(a))
        inside_int = rffi.cast(lltype.Signed, inside)

        assert inside_int == start+rffi.sizeof(lltype.Signed)*3
Ejemplo n.º 9
0
    def test_convert_subarray(self):
        A = lltype.GcArray(lltype.Signed)
        a = lltype.malloc(A, 20)
        inside = lltype.direct_ptradd(lltype.direct_arrayitems(a), 3)

        lltype2ctypes(inside)

        start = rffi.cast(lltype.Signed, lltype.direct_arrayitems(a))
        inside_int = rffi.cast(lltype.Signed, inside)

        assert inside_int == start + rffi.sizeof(lltype.Signed) * 3
Ejemplo n.º 10
0
 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
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
0
    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
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
 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
Ejemplo n.º 15
0
    def test_uninitialized2ctypes(self):
        # for now, uninitialized fields are filled with 0xDD in the ctypes data
        def checkobj(o, size):
            p = ctypes.cast(ctypes.c_void_p(ctypes.addressof(o)),
                            ctypes.POINTER(ctypes.c_ubyte*size))
            for i in range(size):
                assert p.contents[i] == 0xDD

        def checkval(v, fmt):
            res = struct.pack(fmt, v)
            assert res == "\xDD" * len(res)

        checkval(uninitialized2ctypes(rffi.CHAR), 'B')
        checkval(uninitialized2ctypes(rffi.SHORT), 'h')
        checkval(uninitialized2ctypes(rffi.INT), 'i')
        checkval(uninitialized2ctypes(rffi.UINT), 'I')
        checkval(uninitialized2ctypes(rffi.LONGLONG), 'q')
        checkval(uninitialized2ctypes(rffi.DOUBLE), 'd')
        checkobj(uninitialized2ctypes(rffi.INTP),
                 ctypes.sizeof(ctypes.c_void_p))
        checkobj(uninitialized2ctypes(rffi.CCHARP),
                 ctypes.sizeof(ctypes.c_void_p))

        S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
        s = lltype.malloc(S, flavor='raw')
        sc = lltype2ctypes(s)
        checkval(sc.contents.x, 'l')
        checkval(sc.contents.y, 'l')
        lltype.free(s, flavor='raw')
        assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 16
0
 def test_storage_stays_around(self):
     data = "hello, world!" * 100
     A = lltype.Array(rffi.CHAR, hints={'nolength': True})
     S = lltype.Struct('S', ('a', lltype.Ptr(A)))
     s = lltype.malloc(S, flavor='raw')
     lltype2ctypes(s)  # force it to escape
     s.a = lltype.malloc(A, len(data), flavor='raw')
     # the storage for the array should not be freed by lltype even
     # though the _ptr object appears to go away here
     for i in xrange(len(data)):
         s.a[i] = data[i]
     for i in xrange(len(data)):
         assert s.a[i] == data[i]
     lltype.free(s.a, flavor='raw')
     lltype.free(s, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 17
0
 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
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
0
 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
Ejemplo n.º 20
0
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')
Ejemplo n.º 21
0
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')
Ejemplo n.º 22
0
    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
Ejemplo n.º 23
0
 def test_storage_stays_around(self):
     data = "hello, world!" * 100
     A = lltype.Array(rffi.CHAR, hints={'nolength': True})
     S = lltype.Struct('S', ('a', lltype.Ptr(A)))
     s = lltype.malloc(S, flavor='raw')
     lltype2ctypes(s)     # force it to escape
     s.a = lltype.malloc(A, len(data), flavor='raw')
     # the storage for the array should not be freed by lltype even
     # though the _ptr object appears to go away here
     for i in xrange(len(data)):
         s.a[i] = data[i]
     for i in xrange(len(data)):
         assert s.a[i] == data[i]
     lltype.free(s.a, flavor='raw')
     lltype.free(s, flavor='raw')
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 24
0
 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
Ejemplo n.º 25
0
    def test_uninitialized2ctypes(self):
        # for now, uninitialized fields are filled with 0xDD in the ctypes data
        def checkobj(o, size):
            p = ctypes.cast(ctypes.c_void_p(ctypes.addressof(o)),
                            ctypes.POINTER(ctypes.c_ubyte * size))
            for i in range(size):
                assert p.contents[i] == 0xDD

        def checkval(v, fmt):
            res = struct.pack(fmt, v)
            assert res == "\xDD" * len(res)

        checkval(uninitialized2ctypes(rffi.CHAR), 'B')
        checkval(uninitialized2ctypes(rffi.SHORT), 'h')
        checkval(uninitialized2ctypes(rffi.INT), 'i')
        checkval(uninitialized2ctypes(rffi.UINT), 'I')
        checkval(uninitialized2ctypes(rffi.LONGLONG), 'q')
        checkval(uninitialized2ctypes(rffi.DOUBLE), 'd')
        checkobj(uninitialized2ctypes(rffi.INTP),
                 ctypes.sizeof(ctypes.c_void_p))
        checkobj(uninitialized2ctypes(rffi.CCHARP),
                 ctypes.sizeof(ctypes.c_void_p))

        S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
        s = lltype.malloc(S, flavor='raw')
        sc = lltype2ctypes(s)
        checkval(sc.contents.x, 'l')
        checkval(sc.contents.y, 'l')
        lltype.free(s, flavor='raw')
        assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 26
0
 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
Ejemplo n.º 27
0
 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
Ejemplo n.º 28
0
    def test_prebuilt_ll2ctypes_array(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
        ll2ctypes.lltype2ctypes(a)
        def llf():
            s = ''
            for i in range(4):
                s += a[i]
            return 'abcd' == s

        fn = self.getcompiled(llf)
        assert fn()
Ejemplo n.º 29
0
    def test_prebuilt_ll2ctypes_array(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
        ll2ctypes.lltype2ctypes(a)
        def llf():
            s = ''
            for i in range(4):
                s += a[i]
            return 'abcd' == s

        fn = self.getcompiled(llf)
        assert fn()
Ejemplo n.º 30
0
 def test_array_inside_struct(self):
     # like rstr.STR, but not Gc
     STR = lltype.Struct('STR', ('x', lltype.Signed), ('y', lltype.Array(lltype.Char)))
     a = lltype.malloc(STR, 3, flavor='raw')
     a.y[0] = 'x'
     a.y[1] = 'y'
     a.y[2] = 'z'
     ac = lltype2ctypes(a)
     assert ac.contents.y.length == 3
     assert ac.contents.y.items[2] == ord('z')
     lltype.free(a, flavor='raw')
     assert not ALLOCATED
Ejemplo n.º 31
0
 def test_struct_ptrs(self):
     S2 = lltype.Struct('S2', ('y', lltype.Signed))
     S1 = lltype.Struct('S', ('x', lltype.Signed), ('p', lltype.Ptr(S2)))
     s1 = lltype.malloc(S1, flavor='raw')
     s2a = lltype.malloc(S2, flavor='raw')
     s2b = lltype.malloc(S2, flavor='raw')
     s2a.y = ord('a')
     s2b.y = ord('b')
     sc1 = lltype2ctypes(s1)
     sc1.contents.x = 50
     assert s1.x == 50
     sc1.contents.p = lltype2ctypes(s2a)
     assert s1.p == s2a
     s1.p.y -= 32
     assert sc1.contents.p.contents.y == ord('A')
     s1.p = s2b
     sc1.contents.p.contents.y -= 32
     assert s2b.y == ord('B')
     lltype.free(s1, flavor='raw')
     lltype.free(s2a, flavor='raw')
     lltype.free(s2b, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 32
0
 def test_struct_ptrs(self):
     S2 = lltype.Struct('S2', ('y', lltype.Signed))
     S1 = lltype.Struct('S', ('x', lltype.Signed), ('p', lltype.Ptr(S2)))
     s1 = lltype.malloc(S1, flavor='raw')
     s2a = lltype.malloc(S2, flavor='raw')
     s2b = lltype.malloc(S2, flavor='raw')
     s2a.y = ord('a')
     s2b.y = ord('b')
     sc1 = lltype2ctypes(s1)
     sc1.contents.x = 50
     assert s1.x == 50
     sc1.contents.p = lltype2ctypes(s2a)
     assert s1.p == s2a
     s1.p.y -= 32
     assert sc1.contents.p.contents.y == ord('A')
     s1.p = s2b
     sc1.contents.p.contents.y -= 32
     assert s2b.y == ord('B')
     lltype.free(s1, flavor='raw')
     lltype.free(s2a, flavor='raw')
     lltype.free(s2b, flavor='raw')
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 33
0
 def test_array_inside_struct(self):
     # like rstr.STR, but not Gc
     STR = lltype.Struct('STR', ('x', lltype.Signed),
                         ('y', lltype.Array(lltype.Char)))
     a = lltype.malloc(STR, 3, flavor='raw')
     a.y[0] = 'x'
     a.y[1] = 'y'
     a.y[2] = 'z'
     ac = lltype2ctypes(a)
     assert ac.contents.y.length == 3
     assert ac.contents.y.items[2] == ord('z')
     lltype.free(a, flavor='raw')
     assert not ALLOCATED
Ejemplo n.º 34
0
 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
Ejemplo n.º 35
0
def test_charp():
    s = rffi.str2charp("hello")
    sc = lltype2ctypes(s, normalize=False)
    assert sc.contents.items[0] == ord('h')
    assert sc.contents.items[1] == ord('e')
    assert sc.contents.items[2] == ord('l')
    assert sc.contents.items[3] == ord('l')
    assert sc.contents.items[4] == ord('o')
    assert sc.contents.items[5] == 0
    assert not hasattr(sc.contents, 'length')
    sc.contents.items[1] = ord('E')
    assert s[1] == 'E'
    s[0] = 'H'
    assert sc.contents.items[0] == ord('H')
Ejemplo n.º 36
0
 def test_unicharp(self):
     SP = rffi.CArrayPtr(lltype.UniChar)
     s = lltype.malloc(SP.TO, 4, flavor='raw')
     s[0] = u'x'
     s[1] = u'y'
     s[2] = u'z'
     s[3] = u'\x00'
     sc = lltype2ctypes(s, normalize=False)
     assert sc.contents.items[0] == ord(u'x')
     assert sc.contents.items[1] == ord(u'y')
     assert sc.contents.items[2] == ord(u'z')
     assert not hasattr(sc.contents, 'length')
     lltype.free(s, flavor='raw')
     assert not ALLOCATED
Ejemplo n.º 37
0
 def test_unicharp(self):
     SP = rffi.CArrayPtr(lltype.UniChar)
     s = lltype.malloc(SP.TO, 4, flavor='raw')
     s[0] = u'x'
     s[1] = u'y'
     s[2] = u'z'
     s[3] = u'\x00'
     sc = lltype2ctypes(s, normalize=False)
     assert sc.contents.items[0] == ord(u'x')
     assert sc.contents.items[1] == ord(u'y')
     assert sc.contents.items[2] == ord(u'z')
     assert not hasattr(sc.contents, 'length')
     lltype.free(s, flavor='raw')
     assert not ALLOCATED
Ejemplo n.º 38
0
def test_simple_struct():
    S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
    s = lltype.malloc(S, flavor='raw')
    s.x = 123
    sc = lltype2ctypes(s)
    assert isinstance(sc.contents, ctypes.Structure)
    assert sc.contents.x == 123
    sc.contents.x = 456
    assert s.x == 456
    s.x = 789
    assert sc.contents.x == 789
    s.y = 52
    assert sc.contents.y == 52
    lltype.free(s, flavor='raw')
Ejemplo n.º 39
0
 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]
Ejemplo n.º 40
0
    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
Ejemplo n.º 41
0
 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
Ejemplo n.º 42
0
    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
Ejemplo n.º 43
0
def test_array_nolength():
    A = lltype.Array(lltype.Signed, hints={'nolength': True})
    a = lltype.malloc(A, 10, flavor='raw')
    a[0] = 100
    a[1] = 101
    a[2] = 102
    ac = lltype2ctypes(a, normalize=False)
    assert isinstance(ac.contents, ctypes.Structure)
    assert ac.contents.items[1] == 101
    ac.contents.items[2] = 456
    assert a[2] == 456
    a[3] = 789
    assert ac.contents.items[3] == 789
    assert ctypes.sizeof(ac.contents) == 10 * ctypes.sizeof(ctypes.c_long)
    lltype.free(a, flavor='raw')
Ejemplo n.º 44
0
 def test_simple_struct(self):
     S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
     s = lltype.malloc(S, flavor='raw')
     s.x = 123
     sc = lltype2ctypes(s)
     assert isinstance(sc.contents, ctypes.Structure)
     assert sc.contents.x == 123
     sc.contents.x = 456
     assert s.x == 456
     s.x = 789
     assert sc.contents.x == 789
     s.y = 52
     assert sc.contents.y == 52
     lltype.free(s, flavor='raw')
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 45
0
 def test_array_inside_struct(self):
     # like rstr.STR, but not Gc
     STR = lltype.Struct('STR', ('x', lltype.Signed), ('y', lltype.Array(lltype.Char)))
     a = lltype.malloc(STR, 3, flavor='raw')
     a.y[0] = 'x'
     a.y[1] = 'y'
     a.y[2] = 'z'
     # we need to pass normalize=False, otherwise 'ac' is returned of
     # a normalized standard type, which complains about IndexError
     # when doing 'ac.contents.y.items[2]'.
     ac = lltype2ctypes(a, normalize=False)
     assert ac.contents.y.length == 3
     assert ac.contents.y.items[2] == ord('z')
     lltype.free(a, flavor='raw')
     assert not ALLOCATED
Ejemplo n.º 46
0
 def test_simple_struct(self):
     S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
     s = lltype.malloc(S, flavor='raw')
     s.x = 123
     sc = lltype2ctypes(s)
     assert isinstance(sc.contents, ctypes.Structure)
     assert sc.contents.x == 123
     sc.contents.x = 456
     assert s.x == 456
     s.x = 789
     assert sc.contents.x == 789
     s.y = 52
     assert sc.contents.y == 52
     lltype.free(s, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 47
0
def test_simple_array():
    A = lltype.Array(lltype.Signed)
    a = lltype.malloc(A, 10, flavor='raw')
    a[0] = 100
    a[1] = 101
    a[2] = 102
    ac = lltype2ctypes(a, normalize=False)
    assert isinstance(ac.contents, ctypes.Structure)
    assert ac.contents.length == 10
    assert ac.contents.items[1] == 101
    ac.contents.items[2] = 456
    assert a[2] == 456
    a[3] = 789
    assert ac.contents.items[3] == 789
    lltype.free(a, flavor='raw')
Ejemplo n.º 48
0
 def test_charp(self):
     s = rffi.str2charp("hello")
     sc = lltype2ctypes(s, normalize=False)
     assert sc.contents.items[0] == ord('h')
     assert sc.contents.items[1] == ord('e')
     assert sc.contents.items[2] == ord('l')
     assert sc.contents.items[3] == ord('l')
     assert sc.contents.items[4] == ord('o')
     assert sc.contents.items[5] == 0
     assert not hasattr(sc.contents, 'length')
     sc.contents.items[1] = ord('E')
     assert s[1] == 'E'
     s[0] = 'H'
     assert sc.contents.items[0] == ord('H')
     rffi.free_charp(s)
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 49
0
 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
Ejemplo n.º 50
0
 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
Ejemplo n.º 51
0
 def test_simple_array(self):
     A = lltype.Array(lltype.Signed)
     a = lltype.malloc(A, 10, flavor='raw')
     a[0] = 100
     a[1] = 101
     a[2] = 102
     ac = lltype2ctypes(a, normalize=False)
     assert isinstance(ac.contents, ctypes.Structure)
     assert ac.contents.length == 10
     assert ac.contents.items[1] == 101
     ac.contents.items[2] = 456
     assert a[2] == 456
     a[3] = 789
     assert ac.contents.items[3] == 789
     lltype.free(a, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 52
0
    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]
Ejemplo n.º 53
0
 def test_array_inside_struct(self):
     # like rstr.STR, but not Gc
     STR = lltype.Struct('STR', ('x', lltype.Signed),
                         ('y', lltype.Array(lltype.Char)))
     a = lltype.malloc(STR, 3, flavor='raw')
     a.y[0] = 'x'
     a.y[1] = 'y'
     a.y[2] = 'z'
     # we need to pass normalize=False, otherwise 'ac' is returned of
     # a normalized standard type, which complains about IndexError
     # when doing 'ac.contents.y.items[2]'.
     ac = lltype2ctypes(a, normalize=False)
     assert ac.contents.y.length == 3
     assert ac.contents.y.items[2] == ord('z')
     lltype.free(a, flavor='raw')
     assert not ALLOCATED
Ejemplo n.º 54
0
 def test_charp(self):
     s = rffi.str2charp("hello")
     sc = lltype2ctypes(s, normalize=False)
     assert sc.contents.items[0] == ord('h')
     assert sc.contents.items[1] == ord('e')
     assert sc.contents.items[2] == ord('l')
     assert sc.contents.items[3] == ord('l')
     assert sc.contents.items[4] == ord('o')
     assert sc.contents.items[5] == 0
     assert not hasattr(sc.contents, 'length')
     sc.contents.items[1] = ord('E')
     assert s[1] == 'E'
     s[0] = 'H'
     assert sc.contents.items[0] == ord('H')
     rffi.free_charp(s)
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 55
0
 def test_array_nolength(self):
     A = lltype.Array(lltype.Signed, hints={'nolength': True})
     a = lltype.malloc(A, 10, flavor='raw')
     a[0] = 100
     a[1] = 101
     a[2] = 102
     ac = lltype2ctypes(a, normalize=False)
     assert isinstance(ac.contents, ctypes.Structure)
     assert ac.contents.items[1] == 101
     ac.contents.items[2] = 456
     assert a[2] == 456
     a[3] = 789
     assert ac.contents.items[3] == 789
     assert ctypes.sizeof(ac.contents) == 10 * ctypes.sizeof(ctypes.c_long)
     lltype.free(a, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 56
0
 def test_simple_array(self):
     A = lltype.Array(lltype.Signed)
     a = lltype.malloc(A, 10, flavor='raw')
     a[0] = 100
     a[1] = 101
     a[2] = 102
     ac = lltype2ctypes(a, normalize=False)
     assert isinstance(ac.contents, ctypes.Structure)
     assert ac.contents.length == 10
     assert ac.contents._fields_[0] == ('length', ctypes.c_long)
     assert ac.contents.items[1] == 101
     ac.contents.items[2] = 456
     assert a[2] == 456
     a[3] = 789
     assert ac.contents.items[3] == 789
     lltype.free(a, flavor='raw')
     assert not ALLOCATED     # detects memory leaks in the test
Ejemplo n.º 57
0
 def test_indirect_recursive_struct(self):
     S2Forward = lltype.ForwardReference()
     S1 = lltype.Struct('S1', ('p', lltype.Ptr(S2Forward)))
     A2 = lltype.Array(lltype.Ptr(S1), hints={'nolength': True})
     S2 = lltype.Struct('S2', ('a', lltype.Ptr(A2)))
     S2Forward.become(S2)
     s1 = lltype.malloc(S1, flavor='raw')
     a2 = lltype.malloc(A2, 10, flavor='raw')
     s2 = lltype.malloc(S2, flavor='raw')
     s2.a = a2
     a2[5] = s1
     s1.p = s2
     ac2 = lltype2ctypes(a2, normalize=False)
     sc1 = ac2.contents.items[5]
     sc2 = sc1.contents.p
     assert (ctypes.addressof(sc2.contents.a.contents) == ctypes.addressof(
         ac2.contents))
     lltype.free(s1, flavor='raw')
     lltype.free(a2, flavor='raw')
     lltype.free(s2, flavor='raw')
     assert not ALLOCATED  # detects memory leaks in the test
Ejemplo n.º 58
0
 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
Ejemplo n.º 59
0
 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
Ejemplo n.º 60
0
    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()