Ejemplo n.º 1
0
def test_cast_opaque_ptr():
    O = lltype.GcOpaqueType('O')
    Q = lltype.GcOpaqueType('Q')
    S = lltype.GcStruct('S', ('x', lltype.Signed))

    def fn():
        s = lltype.malloc(S)
        o = lltype.cast_opaque_ptr(lltype.Ptr(O), s)
        q = lltype.cast_opaque_ptr(lltype.Ptr(Q), o)
        p = lltype.cast_opaque_ptr(lltype.Ptr(S), q)
        return p == s

    res = interpret(fn, [])
    assert res is True

    O1 = lltype.OpaqueType('O')
    S1 = lltype.Struct('S1', ('x', lltype.Signed))
    s1 = lltype.malloc(S1, immortal=True)

    def fn1():
        o1 = lltype.cast_opaque_ptr(lltype.Ptr(O1), s1)
        p1 = lltype.cast_opaque_ptr(lltype.Ptr(S1), o1)
        return p1 == s1

    res = interpret(fn1, [])
    assert res is True
Ejemplo n.º 2
0
def test_opaque():
    S = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
    O = lltype.GcOpaqueType('O')
    s = lltype.malloc(S)
    adr = cast_ptr_to_adr(s)
    o = cast_adr_to_ptr(adr, lltype.Ptr(O))
    assert lltype.cast_opaque_ptr(lltype.Ptr(S), o) == s
    adr2 = cast_ptr_to_adr(o)
    s2 = cast_adr_to_ptr(adr2, lltype.Ptr(S))
    assert s2 == s
Ejemplo n.º 3
0
def test_odd_ints_opaque():
    T = lltype.GcStruct('T')
    Q = lltype.GcOpaqueType('Q')
    PT = lltype.Ptr(T)
    PQ = lltype.Ptr(Q)

    def fn(n):
        t = lltype.cast_int_to_ptr(PT, n)
        assert lltype.typeOf(t) == PT
        assert lltype.cast_ptr_to_int(t) == n
        o = lltype.cast_opaque_ptr(PQ, t)
        assert lltype.cast_ptr_to_int(o) == n

    fn(13)
    interpret(fn, [11521])
Ejemplo n.º 4
0
# ____________________________________________________________


class NullAddressError(Exception):
    pass


class DanglingPointerError(Exception):
    pass


NULL = fakeaddress(None)
Address = lltype.Primitive("Address", NULL)

# GCREF is similar to Address but it is GC-aware
GCREF = lltype.Ptr(lltype.GcOpaqueType('GCREF'))

# A placeholder for any type that is a GcArray of pointers.
# This can be used in the symbolic offsets above to access such arrays
# in a generic way.
GCARRAY_OF_PTR = lltype.GcArray(GCREF, hints={'placeholder': True})
gcarrayofptr_lengthoffset = ArrayLengthOffset(GCARRAY_OF_PTR)
gcarrayofptr_itemsoffset = ArrayItemsOffset(GCARRAY_OF_PTR)
gcarrayofptr_singleitemoffset = ItemOffset(GCARRAY_OF_PTR.OF)


def array_type_match(A1, A2):
    return A1 == A2 or (A2 == GCARRAY_OF_PTR and isinstance(
        A1, lltype.GcArray) and isinstance(A1.OF, lltype.Ptr)
                        and not A1._hints.get('nolength'))