Example #1
0
    def test_interior_ptrs(self):
        from pypy.rpython.lltypesystem.lltype import Struct, GcStruct, GcArray
        from pypy.rpython.lltypesystem.lltype import Array, Signed, malloc

        S1 = Struct("S1", ('x', Signed))
        T1 = GcStruct("T1", ('s', S1))

        def f1():
            t = malloc(T1)
            t.s.x = 1
            return t.s.x

        S2 = Struct("S2", ('x', Signed))
        T2 = GcArray(S2)

        def f2():
            t = malloc(T2, 1)
            t[0].x = 1
            return t[0].x

        S3 = Struct("S3", ('x', Signed))
        T3 = GcStruct("T3", ('items', Array(S3)))

        def f3():
            t = malloc(T3, 1)
            t.items[0].x = 1
            return t.items[0].x

        S4 = Struct("S4", ('x', Signed))
        T4 = Struct("T4", ('s', S4))
        U4 = GcArray(T4)

        def f4():
            u = malloc(U4, 1)
            u[0].s.x = 1
            return u[0].s.x

        S5 = Struct("S5", ('x', Signed))
        T5 = GcStruct("T5", ('items', Array(S5)))

        def f5():
            t = malloc(T5, 1)
            return len(t.items)

        T6 = GcStruct("T6", ('s', Array(Signed)))

        def f6():
            t = malloc(T6, 1)
            t.s[0] = 1
            return t.s[0]

        def func():
            return (f1() * 100000 + f2() * 10000 + f3() * 1000 + f4() * 100 +
                    f5() * 10 + f6())

        assert func() == 111111
        run = self.runner(func)
        res = run([])
        assert res == 111111
Example #2
0
 def get_itemarray_lowleveltype(self):
     ITEM = self.item_repr.lowleveltype
     ITEMARRAY = GcArray(
         ITEM,
         adtmeths=ADTIFixedList({
             "ll_newlist": ll_fixed_newlist,
             "ll_newlist_hint": ll_fixed_newlist,
             "ll_newemptylist": ll_fixed_newemptylist,
             "ll_length": ll_fixed_length,
             "ll_items": ll_fixed_items,
             ##"list_builder": self.list_builder,
             "ITEM": ITEM,
             "ll_getitem_fast": ll_fixed_getitem_fast,
             "ll_setitem_fast": ll_fixed_setitem_fast,
         }))
     return ITEMARRAY
Example #3
0
 def make_types(cls, ndim, ITEM):
     DATA_PTR = Ptr(FixedSizeArray(ITEM, 1))
     ITEMARRAY = GcArray(ITEM, hints={'nolength': True})
     INDEXARRAY = FixedSizeArray(NPY_INTP, ndim)
     STRUCT = GcStruct(
         "array",
         ("data", Ptr(ITEMARRAY)),  # pointer to raw data buffer 
         ("dataptr", DATA_PTR),  # pointer to first element
         ("ndim", Signed),  # number of dimensions
         ("shape", INDEXARRAY),  # size in each dimension
         ("strides", INDEXARRAY),  # elements to jump to get to the
         # next element in each dimension
         adtmeths=ADTIArray({
             "ll_allocate": ll_allocate,
         }),
     )
     ARRAY = Ptr(STRUCT)
     return ARRAY, INDEXARRAY
Example #4
0
STR.become(
    GcStruct('rpy_string', ('hash', Signed),
             ('chars', Array(Char, hints={'immutable': True})),
             adtmeths={
                 'malloc': staticAdtMethod(mallocstr),
                 'empty': staticAdtMethod(emptystrfun)
             }))
UNICODE.become(
    GcStruct('rpy_unicode', ('hash', Signed),
             ('chars', Array(UniChar, hints={'immutable': True})),
             adtmeths={
                 'malloc': staticAdtMethod(mallocunicode),
                 'empty': staticAdtMethod(emptyunicodefun)
             }))
SIGNED_ARRAY = GcArray(Signed)
CONST_STR_CACHE = WeakValueDictionary()
CONST_UNICODE_CACHE = WeakValueDictionary()


class BaseLLStringRepr(Repr):
    def convert_const(self, value):
        if value is None:
            return nullptr(self.lowleveltype.TO)
        #value = getattr(value, '__self__', value)  # for bound string methods
        if not isinstance(value, self.basetype):
            raise TyperError("not a str: %r" % (value, ))
        try:
            return self.CACHE[value]
        except KeyError:
            p = self.malloc(len(value))
Example #5
0
from pypy.rpython.lltypesystem.lltype import GcArray, Array, Char, malloc
from pypy.rlib.rarithmetic import r_uint, formatd

CHAR_ARRAY = GcArray(Char)

def ll_int_str(repr, i):
    return ll_int2dec(i)

def ll_int2dec(i):
    from pypy.rpython.lltypesystem.rstr import mallocstr
    temp = malloc(CHAR_ARRAY, 20)
    len = 0
    sign = 0
    if i < 0:
        sign = 1
        i = r_uint(-i)
    else:
        i = r_uint(i)
    if i == 0:
        len = 1
        temp[0] = '0'
    else:
        while i:
            temp[len] = chr(i%10+ord('0'))
            i //= 10
            len += 1
    len += sign
    result = mallocstr(len)
    result.hash = 0
    if sign:
        result.chars[0] = '-'