Beispiel #1
0
def EnumKey(space, w_hkey, index):
    """string = EnumKey(key, index) - Enumerates subkeys of an open registry key.

key is an already open key, or any one of the predefined HKEY_* constants.
index is an integer that identifies the index of the key to retrieve.

The function retrieves the name of one subkey each time it is called.
It is typically called repeatedly until an EnvironmentError exception is
raised, indicating no more values are available."""
    hkey = hkey_w(w_hkey, space)
    null_dword = lltype.nullptr(rwin32.LPDWORD.TO)

    # max key name length is 255
    buf = lltype.malloc(rffi.CCHARP.TO, 256, flavor="raw")
    try:
        retValueSize = lltype.malloc(rwin32.LPDWORD.TO, 1, flavor="raw")
        try:
            retValueSize[0] = 256  # includes NULL terminator
            ret = rwinreg.RegEnumKeyEx(
                hkey, index, buf, retValueSize, null_dword, None, null_dword, lltype.nullptr(rwin32.PFILETIME.TO)
            )
            if ret != 0:
                raiseWindowsError(space, ret, "RegEnumKeyEx")
            return space.wrap(rffi.charp2str(buf))
        finally:
            lltype.free(retValueSize, flavor="raw")
    finally:
        lltype.free(buf, flavor="raw")
Beispiel #2
0
    def getdefaultlocale(space):
        encoding = "cp%d" % GetACP()

        BUFSIZE = 50
        buf_lang = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw')
        buf_country = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw')

        try:
            if (GetLocaleInfo(LOCALE_USER_DEFAULT,
                              LOCALE_SISO639LANGNAME,
                              buf_lang, BUFSIZE) and
                GetLocaleInfo(LOCALE_USER_DEFAULT,
                              LOCALE_SISO3166CTRYNAME,
                              buf_country, BUFSIZE)):
                lang = rffi.charp2str(buf_lang)
                country = rffi.charp2str(buf_country)
                return space.newtuple([space.wrap("%s_%s" % (lang, country)),
                                       space.wrap(encoding)])

            # If we end up here, this windows version didn't know about
            # ISO639/ISO3166 names (it's probably Windows 95).  Return the
            # Windows language identifier instead (a hexadecimal number)
            elif GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
                               buf_lang, BUFSIZE):
                lang = rffi.charp2str(buf_lang)
                return space.newtuple([space.wrap("0x%s" % lang),
                                       space.wrap(encoding)])
            else:
                return space.newtuple([space.w_None, space.wrap(encoding)])
        finally:
            lltype.free(buf_lang, flavor='raw')
            lltype.free(buf_country, flavor='raw')
Beispiel #3
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')
Beispiel #4
0
def ll_frame_switch(targetstate):
    if global_state.restart_substate == -1:
        # normal entry point for a call to state.switch()
        # first unwind the stack
        u = UnwindException()
        s = lltype.malloc(SWITCH_STATE)
        s.header.f_restart = INDEX_SWITCH
        s.c = lltype.cast_opaque_ptr(SAVED_REFERENCE, targetstate)
        add_frame_state(u, s.header)
        raise u
    elif global_state.restart_substate == 0:
        # STATE 0: we didn't do anything so far, but the stack is unwound
        global_state.restart_substate = -1
        # grab the frame corresponding to ourself
        # the 'targetstate' local is garbage here, it must be read back from
        # 's.c' where we saved it by the normal entry point above
        mystate = global_state.top
        s = lltype.cast_pointer(lltype.Ptr(SWITCH_STATE), mystate)
        targetstate = lltype.cast_opaque_ptr(lltype.Ptr(STATE_HEADER), s.c)
        # prepare a new saved state for the future switch() back,
        # which will go to STATE 1 below
        sourcestate = lltype.malloc(EMPTY_STATE).header
        sourcestate.f_back = mystate.f_back
        sourcestate.f_restart = INDEX_SWITCH + 1
        global_state.top = targetstate
        global_state.retval_ref = lltype.cast_opaque_ptr(SAVED_REFERENCE, sourcestate)
        raise UnwindException()  # this jumps to targetstate
    else:
        # STATE 1: switching back into a tasklet suspended by
        # a call to switch()
        global_state.top = frame.null_state
        global_state.restart_substate = -1
        origin_state = lltype.cast_opaque_ptr(frame.OPAQUE_STATE_HEADER_PTR, fetch_retval_ref())
        return origin_state  # a normal return into the current tasklet,
Beispiel #5
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')
Beispiel #6
0
Datei: env.py Projekt: ieure/pypy
def get_darwin_sysctl_signed(sysctl_name):
    rval_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw')
    try:
        len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
        try:
            size = rffi.sizeof(rffi.LONGLONG)
            rval_p[0] = rffi.cast(rffi.LONGLONG, 0)
            len_p[0] = rffi.cast(rffi.SIZE_T, size)
            # XXX a hack for llhelper not being robust-enough
            result = sysctlbyname(sysctl_name,
                                  rffi.cast(rffi.VOIDP, rval_p),
                                  len_p,
                                  lltype.nullptr(rffi.VOIDP.TO),
                                  rffi.cast(rffi.SIZE_T, 0))
            rval = 0
            if (rffi.cast(lltype.Signed, result) == 0 and
                rffi.cast(lltype.Signed, len_p[0]) == size):
                rval = rffi.cast(lltype.Signed, rval_p[0])
                if rffi.cast(rffi.LONGLONG, rval) != rval_p[0]:
                    rval = 0    # overflow!
            return rval
        finally:
            lltype.free(len_p, flavor='raw')
    finally:
        lltype.free(rval_p, flavor='raw')
Beispiel #7
0
    def __init__(self, name, argtypes, restype, flags=FUNCFLAG_CDECL):
        self.name = name
        self.argtypes = argtypes
        self.restype = restype
        self.flags = flags
        argnum = len(argtypes)
        self.ll_argtypes = lltype.malloc(FFI_TYPE_PP.TO, argnum, flavor='raw')
        for i in range(argnum):
            self.ll_argtypes[i] = argtypes[i]
        self.ll_cif = lltype.malloc(FFI_CIFP.TO, flavor='raw')

        if _WIN32 and (flags & FUNCFLAG_CDECL == 0):
            cc = FFI_STDCALL
        else:
            cc = FFI_DEFAULT_ABI

        if _MSVC:
            # This little trick works correctly with MSVC.
            # It returns small structures in registers
            if r_uint(restype.c_type) == FFI_TYPE_STRUCT:
                if restype.c_size <= 4:
                    restype = ffi_type_sint32
                elif restype.c_size <= 8:
                    restype = ffi_type_sint64
                
        res = c_ffi_prep_cif(self.ll_cif, cc,
                             rffi.cast(rffi.UINT, argnum), restype,
                             self.ll_argtypes)
        if not res == FFI_OK:
            raise OSError(-1, "Wrong typedef")
Beispiel #8
0
    def test_prebuilt_list_of_addresses(self):
        from pypy.rpython.lltypesystem import llmemory

        TP = lltype.Struct('x', ('y', lltype.Signed))
        a = lltype.malloc(TP, flavor='raw', immortal=True)
        b = lltype.malloc(TP, flavor='raw', immortal=True)
        c = lltype.malloc(TP, flavor='raw', immortal=True)
        a_a = llmemory.cast_ptr_to_adr(a)
        a0 = llmemory.cast_ptr_to_adr(a)
        assert a_a is not a0
        assert a_a == a0
        a_b = llmemory.cast_ptr_to_adr(b)
        a_c = llmemory.cast_ptr_to_adr(c)

        d = {a_a: 3, a_b: 4, a_c: 5}
        d[a0] = 8

        def func(i):
            if i == 0:
                ptr = a
            else:
                ptr = b
            return d[llmemory.cast_ptr_to_adr(ptr)]

        py.test.raises(TypeError, self.interpret, func, [0])
Beispiel #9
0
def test_fakeadr_eq():
    S = lltype.GcStruct("S", ("x", lltype.Signed), ("y", lltype.Signed))
    s = lltype.malloc(S)

    assert cast_ptr_to_adr(s) == cast_ptr_to_adr(s)

    adr1 = cast_ptr_to_adr(s) + FieldOffset(S, "x")
    adr2 = cast_ptr_to_adr(s) + FieldOffset(S, "y")
    adr3 = cast_ptr_to_adr(s) + FieldOffset(S, "y")
    assert adr1 != adr2
    assert adr2 == adr3

    A = lltype.GcArray(lltype.Char)
    a = lltype.malloc(A, 5)
    adr1 = cast_ptr_to_adr(a) + ArrayLengthOffset(A)
    adr2 = cast_ptr_to_adr(a) + ArrayLengthOffset(A)
    assert adr1 == adr2

    adr1 = cast_ptr_to_adr(a) + ArrayItemsOffset(A)
    adr2 = cast_ptr_to_adr(a) + ArrayItemsOffset(A)
    assert adr1 == adr2
    adr2 += ItemOffset(lltype.Char, 0)
    assert adr1 == adr2

    adr1 += ItemOffset(lltype.Char, 2)
    adr2 += ItemOffset(lltype.Char, 3)
    assert adr1 != adr2
    adr2 += ItemOffset(lltype.Char, -1)
    assert adr1 == adr2
Beispiel #10
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)
Beispiel #11
0
 def f(n):
     while n > 0:
         myjitdriver.can_enter_jit(n=n)
         myjitdriver.jit_merge_point(n=n)
         xy = XY()
         xy.next1 = lltype.malloc(A, 0)
         xy.next2 = lltype.malloc(A, 0)
         xy.next3 = lltype.malloc(A, 0)
         xy.next4 = lltype.malloc(A, 0)
         xy.next5 = lltype.malloc(A, 0)
         xy.n = n
         exctx.topframeref = vref = virtual_ref(xy)
         if n % 6 == 0:
             xy.next1 = lltype.nullptr(A)
             xy.next2 = lltype.nullptr(A)
             xy.next3 = lltype.nullptr(A)
             externalfn(n)
         n -= 1
         exctx.topframeref = vref_None
         xy.next1 = lltype.nullptr(A)
         xy.next2 = lltype.nullptr(A)
         xy.next3 = lltype.nullptr(A)
         xy.next4 = lltype.nullptr(A)
         xy.next5 = lltype.nullptr(A)
         virtual_ref_finish(vref, xy)
     return exctx.m
Beispiel #12
0
    def __init__(self, name, argtypes, restype, flags=FUNCFLAG_CDECL):
        self.name = name
        self.argtypes = argtypes
        self.restype = restype
        self.flags = flags
        argnum = len(argtypes)
        self.ll_argtypes = lltype.malloc(
            FFI_TYPE_PP.TO, argnum, flavor="raw", track_allocation=False
        )  # freed by the __del__
        for i in range(argnum):
            self.ll_argtypes[i] = argtypes[i]
        self.ll_cif = lltype.malloc(FFI_CIFP.TO, flavor="raw", track_allocation=False)  # freed by the __del__

        if _MSVC:
            # This little trick works correctly with MSVC.
            # It returns small structures in registers
            if intmask(restype.c_type) == FFI_TYPE_STRUCT:
                if restype.c_size <= 4:
                    restype = ffi_type_sint32
                elif restype.c_size <= 8:
                    restype = ffi_type_sint64

        res = c_ffi_prep_cif(
            self.ll_cif,
            rffi.cast(rffi.USHORT, get_call_conv(flags, False)),
            rffi.cast(rffi.UINT, argnum),
            restype,
            self.ll_argtypes,
        )
        if not res == FFI_OK:
            raise OSError(-1, "Wrong typedef")
Beispiel #13
0
 def f(n):
     xy2 = self.setup2()
     xy2.inst_x = 10
     xy2.inst_l1 = lltype.malloc(ARRAY, 1)
     xy2.inst_l1[0] = 1982731
     xy2.inst_l2 = lltype.malloc(ARRAY, 1)
     xy2.inst_l2[0] = 10000
     other = self.setup2()
     other.inst_x = 15
     other.inst_l1 = lltype.malloc(ARRAY, 2)
     other.inst_l1[0] = 189182
     other.inst_l1[1] = 58421
     other.inst_l2 = lltype.malloc(ARRAY, 2)
     other.inst_l2[0] = 181
     other.inst_l2[1] = 189
     while n > 0:
         myjitdriver.can_enter_jit(xy2=xy2, n=n, other=other)
         myjitdriver.jit_merge_point(xy2=xy2, n=n, other=other)
         promote_virtualizable(other, 'inst_l2')
         length = len(other.inst_l2)       # getfield_gc/arraylen_gc
         value = other.inst_l2[0]          # getfield_gc/getarrayitem_gc
         other.inst_l2[0] = value + length # getfield_gc/setarrayitem_gc
         promote_virtualizable(xy2, 'inst_l2')
         xy2.inst_l2[0] = value + 100      # virtualized away
         n -= 1
     promote_virtualizable(xy2, 'inst_l2')                
     return xy2.inst_l2[0]
Beispiel #14
0
    def walk_stack_from(self):
        curframe = lltype.malloc(WALKFRAME, flavor='raw')
        otherframe = lltype.malloc(WALKFRAME, flavor='raw')

        # Walk over all the pieces of stack.  They are in a circular linked
        # list of structures of 7 words, the 2 first words being prev/next.
        # The anchor of this linked list is:
        anchor = llmemory.cast_ptr_to_adr(gcrootanchor)
        initialframedata = anchor.address[1]
        stackscount = 0
        while initialframedata != anchor:     # while we have not looped back
            self.fill_initial_frame(curframe, initialframedata)
            # Loop over all the frames in the stack
            while self.walk_to_parent_frame(curframe, otherframe):
                swap = curframe
                curframe = otherframe    # caller becomes callee
                otherframe = swap
            # Then proceed to the next piece of stack
            initialframedata = initialframedata.address[1]
            stackscount += 1
        #
        expected = rffi.stackcounter.stacks_counter
        ll_assert(not (stackscount < expected), "non-closed stacks around")
        ll_assert(not (stackscount > expected), "stacks counter corruption?")
        lltype.free(otherframe, flavor='raw')
        lltype.free(curframe, flavor='raw')
 def f():
     s1 = lltype.malloc(S)
     llop.keepalive(lltype.Void, s1)
     s2 = lltype.malloc(S)
     llop.keepalive(lltype.Void, s1)
     llop.keepalive(lltype.Void, s2)
     return lltype.cast_ptr_to_int(s1) + lltype.cast_ptr_to_int(s2)
Beispiel #16
0
 def f():
     t.p = lltype.malloc(S)
     t.p.x = 43
     for i in range(2500000):
         s = lltype.malloc(S)
         s.x = i
     return t.p.x
Beispiel #17
0
        def time_time_llimpl():
            void = lltype.nullptr(rffi.VOIDP.TO)
            result = -1.0
            if self.HAVE_GETTIMEOFDAY:
                t = lltype.malloc(self.TIMEVAL, flavor='raw')

                errcode = -1
                if self.GETTIMEOFDAY_NO_TZ:
                    errcode = c_gettimeofday(t)
                else:
                    errcode = c_gettimeofday(t, void)

                if rffi.cast(rffi.LONG, errcode) == 0:
                    result = decode_timeval(t)
                lltype.free(t, flavor='raw')
            if result != -1:
                return result
            if self.HAVE_FTIME:
                t = lltype.malloc(self.TIMEB, flavor='raw')
                c_ftime(t)
                result = (float(intmask(t.c_time)) +
                          float(intmask(t.c_millitm)) * 0.001)
                lltype.free(t, flavor='raw')
                return result
            return float(c_time(void))
Beispiel #18
0
 def estimate_best_nursery_size():
     """Try to estimate the best nursery size at run-time, depending
     on the machine we are running on.
     """
     L2cache = 0
     l2cache_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw')
     try:
         len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
         try:
             size = rffi.sizeof(rffi.LONGLONG)
             l2cache_p[0] = rffi.cast(rffi.LONGLONG, 0)
             len_p[0] = rffi.cast(rffi.SIZE_T, size)
             result = sysctlbyname("hw.l2cachesize",
                                   rffi.cast(rffi.VOIDP, l2cache_p),
                                   len_p,
                                   lltype.nullptr(rffi.VOIDP.TO), 
                                   rffi.cast(rffi.SIZE_T, 0))
             if (rffi.cast(lltype.Signed, result) == 0 and
                 rffi.cast(lltype.Signed, len_p[0]) == size):
                 L2cache = rffi.cast(lltype.Signed, l2cache_p[0])
                 if rffi.cast(rffi.LONGLONG, L2cache) != l2cache_p[0]:
                     L2cache = 0    # overflow!
         finally:
             lltype.free(len_p, flavor='raw')
     finally:
         lltype.free(l2cache_p, flavor='raw')
     if L2cache > 0:
         return best_nursery_size_for_L2cache(L2cache)
     else:
         # Print a warning even in non-debug builds
         llop.debug_print(lltype.Void,
             "Warning: cannot find your CPU L2 cache size with sysctl()")
         return -1
Beispiel #19
0
 def convert_const(self, value):
     if self.ctypecheck(value):
         key = "by_id", id(value)
         keepalive = value
     else:
         if self.ownsmemory:
             raise TyperError("convert_const(%r) but repr owns memory" % (
                 value,))
         key = "by_value", value
         keepalive = None
     try:
         return self.const_cache[key][0]
     except KeyError:
         self.setup()
         p = lltype.malloc(self.r_memoryowner.lowleveltype.TO, zero=True)
         self.initialize_const(p, value)
         if self.ownsmemory:
             result = p
         else:
             # we must return a non-memory-owning box that keeps the
             # memory-owning box alive
             result = lltype.malloc(self.lowleveltype.TO, zero=True)
             result.c_data = p.c_data    # initialize c_data pointer
             result.c_data_owner_keepalive = p
         self.const_cache[key] = result, keepalive
         return result
Beispiel #20
0
    def getdefaultlocale():
        encoding = "cp%d" % GetACP()

        BUFSIZE = 50
        buf_lang = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor="raw")
        buf_country = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor="raw")

        try:
            if GetLocaleInfo(
                cConfig.LOCALE_USER_DEFAULT, cConfig.LOCALE_SISO639LANGNAME, buf_lang, BUFSIZE
            ) and GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT, cConfig.LOCALE_SISO3166CTRYNAME, buf_country, BUFSIZE):
                lang = rffi.charp2str(buf_lang)
                country = rffi.charp2str(buf_country)
                language = "%s_%s" % (lang, country)

            # If we end up here, this windows version didn't know about
            # ISO639/ISO3166 names (it's probably Windows 95).  Return the
            # Windows language identifier instead (a hexadecimal number)
            elif GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT, cConfig.LOCALE_IDEFAULTLANGUAGE, buf_lang, BUFSIZE):
                lang = rffi.charp2str(buf_lang)
                language = "0x%s" % (lang,)

            else:
                language = None
        finally:
            lltype.free(buf_lang, flavor="raw")
            lltype.free(buf_country, flavor="raw")

        return language, encoding
Beispiel #21
0
def test_gc_pointers_inside():
    from pypy.rpython import rclass
    PT = lltype.Ptr(lltype.GcStruct('T'))
    S1 = lltype.GcStruct('S', ('x', PT), ('y', PT))
    S2 = lltype.GcStruct('S', ('x', PT), ('y', PT),
                         hints={'immutable': True})
    accessor = rclass.FieldListAccessor()
    S3 = lltype.GcStruct('S', ('x', PT), ('y', PT),
                         hints={'immutable_fields': accessor})
    accessor.initialize(S3, {'x': IR_IMMUTABLE})
    #
    s1 = lltype.malloc(S1)
    adr = llmemory.cast_ptr_to_adr(s1)
    lst = list(gc_pointers_inside(s1._obj, adr, mutable_only=True))
    expected = [adr + llmemory.offsetof(S1, 'x'),
                adr + llmemory.offsetof(S1, 'y')]
    assert lst == expected or lst == expected[::-1]
    #
    s2 = lltype.malloc(S2)
    adr = llmemory.cast_ptr_to_adr(s2)
    lst = list(gc_pointers_inside(s2._obj, adr, mutable_only=True))
    assert lst == []
    #
    s3 = lltype.malloc(S3)
    adr = llmemory.cast_ptr_to_adr(s3)
    lst = list(gc_pointers_inside(s3._obj, adr, mutable_only=True))
    assert lst == [adr + llmemory.offsetof(S3, 'y')]
Beispiel #22
0
def test_getfield_pure():
    S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
    S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                         hints={'immutable': True})
    accessor = rclass.FieldListAccessor()
    #
    s1 = lltype.malloc(S1); s1.x = 45
    py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x')
    s2 = lltype.malloc(S2); s2.x = 45
    assert llop.getfield(lltype.Signed, s2, 'x') == 45
    #
    py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x')
    assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45
    #
    for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
                 rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
                 rclass.IR_QUASIIMMUTABLE_ARRAY]:
        #
        S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                             hints={'immutable_fields': accessor})
        accessor.initialize(S3, {'x': kind})
        s3 = lltype.malloc(S3); s3.x = 46; s3.y = 47
        if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]:
            assert llop.getfield(lltype.Signed, s3, 'x') == 46
            assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46
        else:
            py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x')
            py.test.raises(TypeError, llop.getinteriorfield,
                           lltype.Signed, s3, 'x')
        py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y')
        py.test.raises(TypeError, llop.getinteriorfield,
                       lltype.Signed, s3, 'y')
Beispiel #23
0
 def get_type_id(self, TYPE):
     try:
         return self.id_of_type[TYPE]
     except KeyError:
         assert self.can_add_new_types
         assert isinstance(TYPE, (lltype.GcStruct, lltype.GcArray))
         # Record the new type_id description as a TYPE_INFO structure.
         # build the TYPE_INFO structure
         if not TYPE._is_varsize():
             fullinfo = lltype.malloc(GCData.TYPE_INFO,
                                      immortal=True, zero=True)
             info = fullinfo
         else:
             fullinfo = lltype.malloc(GCData.VARSIZE_TYPE_INFO,
                                      immortal=True, zero=True)
             info = fullinfo.header
         type_id = self.type_info_group.add_member(fullinfo)
         if self.can_encode_type_shape:
             encode_type_shape(self, info, TYPE, type_id.index)
         else:
             self._pending_type_shapes.append((info, TYPE, type_id.index))
         # store it
         self.id_of_type[TYPE] = type_id
         self.add_vtable_after_typeinfo(TYPE)
         return type_id
Beispiel #24
0
def strxfrm(space, s):
    "string -> string. Returns a string that behaves for cmp locale-aware."
    n1 = len(s) + 1

    buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True)
    s_c = rffi.str2charp(s)
    try:
        n2 = _strxfrm(buf, s_c, n1) + 1
    finally:
        rffi.free_charp(s_c)
    if n2 > n1:
        # more space needed
        lltype.free(buf, flavor="raw")
        buf = lltype.malloc(rffi.CCHARP.TO, intmask(n2),
                            flavor="raw", zero=True)
        s_c = rffi.str2charp(s)
        try:
            _strxfrm(buf, s_c, n2)
        finally:
            rffi.free_charp(s_c)

    val = rffi.charp2str(buf)
    lltype.free(buf, flavor="raw")

    return space.wrap(val)
    def test_immutable_to_old_promotion(self):
        T_CHILD = lltype.Ptr(lltype.GcStruct('Child', ('field', lltype.Signed)))
        T_PARENT = lltype.Ptr(lltype.GcStruct('Parent', ('sub', T_CHILD)))
        child = lltype.malloc(T_CHILD.TO)
        child2 = lltype.malloc(T_CHILD.TO)
        parent = lltype.malloc(T_PARENT.TO)
        parent2 = lltype.malloc(T_PARENT.TO)
        parent.sub = child
        child.field = 3
        parent2.sub = child2
        child2.field = 8

        T_ALL = lltype.Ptr(lltype.GcArray(T_PARENT))
        all = lltype.malloc(T_ALL.TO, 2)
        all[0] = parent
        all[1] = parent2

        def f(x, y):
            res = all[x]
            #all[x] = lltype.nullptr(T_PARENT.TO)
            return res.sub.field

        run, transformer = self.runner(f, nbargs=2, transformer=True)
        run([1, 4])
        assert len(transformer.layoutbuilder.addresses_of_static_ptrs) == 0
        assert transformer.layoutbuilder.additional_roots_sources >= 4
Beispiel #26
0
def xxx_test_later_along_link():
    S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
    s1 = lltype.malloc(S1)
    s1.x = 123
    s2 = lltype.malloc(S1)
    s2.x = 60
    def fn(x, y):
        if x:
            x = s1.x
        else:
            x = s2.x
        y *= 2
        return (x+1) - y

    graph, t = get_graph(fn, [int, int])
    assert summary(graph) == {'int_is_true': 1,
                              'getfield': 2,
                              'int_mul': 1,
                              'int_add': 1,
                              'int_sub': 1}
    constant_fold_graph(graph)
    assert summary(graph) == {'int_is_true': 1,
                              'int_mul': 1,
                              'int_sub': 1}
    check_graph(graph, [-1], 124, t)
    check_graph(graph, [0], 61, t)
Beispiel #27
0
def test_multiple_incoming_links():
    S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
    s1 = lltype.malloc(S1)
    s1.x = 123
    s2 = lltype.malloc(S1)
    s2.x = 60
    s3 = lltype.malloc(S1)
    s3.x = 15
    def fn(x):
        y = x * 10
        if x == 1:
            x = s1.x
        elif x == 2:
            x = s2.x
        elif x == 3:
            x = s3.x
            y = s1.x
        return (x+1) + y

    graph, t = get_graph(fn, [int])
    constant_fold_graph(graph)
    assert summary(graph) == {'int_mul': 1, 'int_eq': 3, 'int_add': 2}
    for link in graph.iterlinks():
        if Constant(139) in link.args:
            break
    else:
        raise AssertionError("139 not found in the graph as a constant")
    for i in range(4):
        check_graph(graph, [i], fn(i), t)
Beispiel #28
0
def detect_floatformat():
    from pypy.rpython.lltypesystem import rffi, lltype
    buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw')
    rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0
    packed = rffi.charpsize2str(buf, 8)
    if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05":
        double_format = 'IEEE, big-endian'
    elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43":
        double_format = 'IEEE, little-endian'
    else:
        double_format = 'unknown'
    lltype.free(buf, flavor='raw')
    #
    buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw')
    rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0)
    packed = rffi.charpsize2str(buf, 4)
    if packed == "\x4b\x7f\x01\x02":
        float_format = 'IEEE, big-endian'
    elif packed == "\x02\x01\x7f\x4b":
        float_format = 'IEEE, little-endian'
    else:
        float_format = 'unknown'
    lltype.free(buf, flavor='raw')

    return double_format, float_format
Beispiel #29
0
    def test_iterkeys(self, space, api):
        w_dict = space.sys.getdict(space)
        py_dict = make_ref(space, w_dict)

        ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
        pkey = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
        pvalue = lltype.malloc(PyObjectP.TO, 1, flavor='raw')

        keys_w = []
        values_w = []
        try:
            ppos[0] = 0
            while api.PyDict_Next(w_dict, ppos, pkey, None):
                w_key = from_ref(space, pkey[0])
                keys_w.append(w_key)
            ppos[0] = 0
            while api.PyDict_Next(w_dict, ppos, None, pvalue):
                w_value = from_ref(space, pvalue[0])
                values_w.append(w_value)
        finally:
            lltype.free(ppos, flavor='raw')
            lltype.free(pkey, flavor='raw')
            lltype.free(pvalue, flavor='raw')

        api.Py_DecRef(py_dict) # release borrowed references

        assert space.eq_w(space.newlist(keys_w),
                          space.call_method(w_dict, "keys"))
        assert space.eq_w(space.newlist(values_w),
                          space.call_method(w_dict, "values"))
Beispiel #30
0
        def setlen(self, size, zero=False, overallocate=True):
            if size > 0:
                if size > self.allocated or size < self.allocated / 2:
                    if overallocate:
                        if size < 9:
                            some = 3
                        else:
                            some = 6
                        some += size >> 3
                    else:
                        some = 0
                    self.allocated = size + some
                    if zero:
                        new_buffer = lltype.malloc(mytype.arraytype,
                                                   self.allocated, flavor='raw',
                                                   add_memory_pressure=True,
                                                   zero=True)
                    else:
                        new_buffer = lltype.malloc(mytype.arraytype,
                                                   self.allocated, flavor='raw',
                                                   add_memory_pressure=True)
                        for i in range(min(size, self.len)):
                            new_buffer[i] = self.buffer[i]
                else:
                    self.len = size
                    return
            else:
                assert size == 0
                self.allocated = 0
                new_buffer = lltype.nullptr(mytype.arraytype)

            if self.buffer:
                lltype.free(self.buffer, flavor='raw')
            self.buffer = new_buffer
            self.len = size
Beispiel #31
0
 def f(x):
     t = lltype.malloc(T, 1)
     t[0].x = x
     return t[0].x
Beispiel #32
0
 def g():
     s = lltype.malloc(S)
     f(s)
     return s.x
Beispiel #33
0
 def f():
     return lltype.malloc(S)
Beispiel #34
0
 def f(x):
     s = lltype.malloc(S)
     s.x = x
     return s.x
Beispiel #35
0
 def g(n):
     s = lltype.malloc(S)
     s.x = n
Beispiel #36
0
 def f(x):
     u = lltype.malloc(U, 1)
     u[0].s.x = x
     return u[0].s.x
Beispiel #37
0
 def f(x):
     t = lltype.malloc(T, 1)
     t.items[0].x = x
     return t.items[0].x
Beispiel #38
0
 def f(x):
     t = lltype.malloc(T)
     s = t.s
     if x:
         s.x = x
     return t.s.x + s.x
Beispiel #39
0
 def fn():
     x = lltype.malloc(BIG)
     while x.z < 10:  # makes several blocks
         x.z += 3
     return x.z
Beispiel #40
0
 def f(x):
     t = lltype.malloc(T)
     t.s.x = x
     return t.s.x
Beispiel #41
0
 def fn():
     s1 = lltype.malloc(S)
     return lloperation.llop.ptr_eq(lltype.Bool, s1, s1)
Beispiel #42
0
 def fn():
     x = lltype.malloc(BIG)
     x.u1.a = 3
     x.u2.b = 6
     return x.u1.b * x.u2.a
Beispiel #43
0
 def fn():
     s = lltype.malloc(S)
     return lloperation.llop.ptr_iszero(lltype.Bool, s)
Beispiel #44
0
 def fn():
     s1 = lltype.malloc(S)
     s2 = lltype.malloc(S)
     return lloperation.llop.ptr_ne(lltype.Bool, s1, s2)
Beispiel #45
0
 def fn():
     s = lltype.malloc(S)
     s.x = 11
     p = lltype.direct_fieldptr(s, 'x')
     return p[0]
Beispiel #46
0
 def fn():
     s = lltype.malloc(S)
     null = lltype.nullptr(S)
     return lloperation.llop.ptr_ne(lltype.Bool, null, s)
Beispiel #47
0
 def fn(n1, n2):
     s = lltype.malloc(S)
     a = s.a
     a[0] = n1
     a[2] = n2
     return a[0] - a[2]
Beispiel #48
0
 def fn():
     s = lltype.malloc(S)
     s.a[index].n = 12
     return s.a[index].n
Beispiel #49
0
 def f(n):
     a = lltype.malloc(A)
     a.x = 15
     return g(a, n)
Beispiel #50
0
 def fn():
     b = lltype.malloc(BIG)
     g(b.s)
Beispiel #51
0
 def f(x):
     a = lltype.malloc(A)
     a.x = x
     y = g(a)
     return x - y
Beispiel #52
0
 def fn(n1, n2):
     b = lltype.malloc(BIG)
     b.z = n1
     b.s.x = n2
     return b.z - b.s.x
Beispiel #53
0
 def f(x):
     a = lltype.malloc(A)
     a.x = x
     g(a)
     return a.x
Beispiel #54
0
 def f(n):
     prebuilt_a.x = n
     a = lltype.malloc(A)
     a.x = 2
     a = g(a)
     return prebuilt_a.x * a.x
Beispiel #55
0
def is_subclass_of_object(TYPE):
    while isinstance(TYPE, lltype.GcStruct):
        if TYPE is rclass.OBJECT:
            return True
        _, TYPE = TYPE._first_struct()
    return False


########## weakrefs ##########
# framework: weakref objects are small structures containing only an address

WEAKREF = lltype.GcStruct("weakref", ("weakptr", llmemory.Address))
WEAKREFPTR = lltype.Ptr(WEAKREF)
sizeof_weakref = llmemory.sizeof(WEAKREF)
empty_weakref = lltype.malloc(WEAKREF, immortal=True)
empty_weakref.weakptr = llmemory.NULL
weakptr_offset = llmemory.offsetof(WEAKREF, "weakptr")


def ll_weakref_deref(wref):
    wref = llmemory.cast_weakrefptr_to_ptr(WEAKREFPTR, wref)
    return wref.weakptr


def convert_weakref_to(targetptr):
    # Prebuilt weakrefs don't really need to be weak at all,
    # but we need to emulate the structure expected by ll_weakref_deref().
    if not targetptr:
        return empty_weakref
    else:
Beispiel #56
0
 def f(x):
     a = lltype.malloc(A)
     a.x = x
     b = g(a)
     return a.x + b.x
Beispiel #57
0
def compress(space, data, compresslevel=9):
    """compress(data [, compresslevel=9]) -> string

    Compress data in one shot. If you want to compress data sequentially,
    use an instance of BZ2Compressor instead. The compresslevel parameter, if
    given, must be a number between 1 and 9."""

    if compresslevel < 1 or compresslevel > 9:
        raise OperationError(
            space.w_ValueError,
            space.wrap("compresslevel must be between 1 and 9"))

    bzs = lltype.malloc(bz_stream.TO, flavor='raw', zero=True)
    in_bufsize = len(data)
    # conforming to bz2 manual, this is large enough to fit compressed
    # data in one shot. We will check it later anyway.
    out_bufsize = in_bufsize + (in_bufsize / 100 + 1) + 600

    out_buf = lltype.malloc(rffi.CCHARP.TO,
                            out_bufsize,
                            flavor='raw',
                            zero=True)
    in_buf = lltype.malloc(rffi.CCHARP.TO, in_bufsize, flavor='raw')
    for i in range(in_bufsize):
        in_buf[i] = data[i]

    try:
        bzs.c_next_in = in_buf
        rffi.setintfield(bzs, 'c_avail_in', in_bufsize)
        bzs.c_next_out = out_buf
        rffi.setintfield(bzs, 'c_avail_out', out_bufsize)

        bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0)
        if bzerror != BZ_OK:
            _catch_bz2_error(space, bzerror)

        total_out = _bzs_total_out(bzs)
        temp = []
        while True:
            bzerror = BZ2_bzCompress(bzs, BZ_FINISH)
            if bzerror == BZ_STREAM_END:
                break
            elif bzerror != BZ_FINISH_OK:
                BZ2_bzCompressEnd(bzs)
                _catch_bz2_error(space, bzerror)

            if rffi.getintfield(bzs, 'c_avail_out') == 0:
                data = "".join(
                    [out_buf[i] for i in range(_bzs_total_out(bzs))])
                temp.append(data)

                lltype.free(out_buf, flavor='raw')
                out_bufsize = _new_buffer_size(out_bufsize)
                out_buf = lltype.malloc(rffi.CCHARP.TO,
                                        out_bufsize,
                                        flavor='raw',
                                        zero=True)
                bzs.c_next_out = out_buf
                rffi.setintfield(bzs, 'c_avail_out', out_bufsize)

        if temp:
            res = "".join(temp)

        if rffi.getintfield(bzs, 'c_avail_out'):
            size = _bzs_total_out(bzs) - total_out
            res = "".join([out_buf[i] for i in range(size)])
        else:
            total_out = _bzs_total_out(bzs)
            res = "".join([out_buf[i] for i in range(total_out)])

        BZ2_bzCompressEnd(bzs)
        return space.wrap(res)
    finally:
        lltype.free(bzs, flavor='raw')
        lltype.free(in_buf, flavor='raw')
        lltype.free(out_buf, flavor='raw')
Beispiel #58
0
 def make_type_info_group(self):
     self.type_info_group = llgroup.group("typeinfo")
     # don't use typeid 0, may help debugging
     DUMMY = lltype.Struct("dummy", ('x', lltype.Signed))
     dummy = lltype.malloc(DUMMY, immortal=True, zero=True)
     self.type_info_group.add_member(dummy)
Beispiel #59
0
    def decompress(self, data):
        """decompress(data) -> string

        Provide more data to the decompressor object. It will return chunks
        of decompressed data whenever possible. If you try to decompress data
        after the end of stream is found, EOFError will be raised. If any data
        was found after the end of stream, it'll be ignored and saved in
        unused_data attribute."""

        if data == '':
            return self.space.wrap('')
        if not self.running:
            raise OperationError(
                self.space.w_EOFError,
                self.space.wrap("end of stream was already found"))

        in_bufsize = len(data)
        in_buf = lltype.malloc(rffi.CCHARP.TO, in_bufsize, flavor='raw')
        for i in range(in_bufsize):
            in_buf[i] = data[i]

        out_bufsize = SMALLCHUNK
        out_buf = lltype.malloc(rffi.CCHARP.TO,
                                out_bufsize,
                                flavor='raw',
                                zero=True)

        try:

            self.bzs.c_next_in = in_buf
            rffi.setintfield(self.bzs, 'c_avail_in', in_bufsize)
            self.bzs.c_next_out = out_buf
            rffi.setintfield(self.bzs, 'c_avail_out', out_bufsize)

            temp = []
            while True:
                bzerror = BZ2_bzDecompress(self.bzs)
                if bzerror == BZ_STREAM_END:
                    if rffi.getintfield(self.bzs, 'c_avail_in') != 0:
                        unused = [
                            self.bzs.c_next_in[i] for i in range(
                                rffi.getintfield(self.bzs, 'c_avail_in'))
                        ]
                        self.unused_data = "".join(unused)
                    self.running = False
                    break
                if bzerror != BZ_OK:
                    _catch_bz2_error(self.space, bzerror)

                if rffi.getintfield(self.bzs, 'c_avail_in') == 0:
                    break
                elif rffi.getintfield(self.bzs, 'c_avail_out') == 0:
                    total_out = _bzs_total_out(self.bzs)
                    data = "".join([out_buf[i] for i in range(total_out)])
                    temp.append(data)

                    lltype.free(out_buf, flavor='raw')
                    out_bufsize = _new_buffer_size(out_bufsize)
                    out_buf = lltype.malloc(rffi.CCHARP.TO,
                                            out_bufsize,
                                            flavor='raw')
                    self.bzs.c_next_out = out_buf
                    rffi.setintfield(self.bzs, 'c_avail_out', out_bufsize)

            if temp:
                total_out = _bzs_total_out(self.bzs)
                data = "".join(
                    [out_buf[i] for i in range(total_out - len(temp[0]))])
                temp.append(data)
                return self.space.wrap("".join(temp))

            total_out = _bzs_total_out(self.bzs)
            res = "".join(
                [out_buf[i] for i in range(total_out) if out_buf[i] != '\x00'])
            return self.space.wrap(res)
        finally:
            lltype.free(in_buf, flavor='raw')
            lltype.free(out_buf, flavor='raw')
Beispiel #60
0
def decompress(space, data):
    """decompress(data) -> decompressed data

    Decompress data in one shot. If you want to decompress data sequentially,
    use an instance of BZ2Decompressor instead."""

    in_bufsize = len(data)
    if in_bufsize == 0:
        return space.wrap("")

    bzs = lltype.malloc(bz_stream.TO, flavor='raw', zero=True)
    in_buf = lltype.malloc(rffi.CCHARP.TO, in_bufsize, flavor='raw')
    for i in range(in_bufsize):
        in_buf[i] = data[i]

    out_bufsize = SMALLCHUNK
    out_buf = lltype.malloc(rffi.CCHARP.TO,
                            out_bufsize,
                            flavor='raw',
                            zero=True)
    try:

        bzs.c_next_in = in_buf
        rffi.setintfield(bzs, 'c_avail_in', in_bufsize)
        bzs.c_next_out = out_buf
        rffi.setintfield(bzs, 'c_avail_out', out_bufsize)

        bzerror = BZ2_bzDecompressInit(bzs, 0, 0)
        if bzerror != BZ_OK:
            _catch_bz2_error(space, bzerror)

        temp = []
        while True:
            bzerror = BZ2_bzDecompress(bzs)
            if bzerror == BZ_STREAM_END:
                break
            if bzerror != BZ_OK:
                BZ2_bzDecompressEnd(bzs)
                _catch_bz2_error(space, bzerror)

            if rffi.getintfield(bzs, 'c_avail_in') == 0:
                BZ2_bzDecompressEnd(bzs)
                raise OperationError(space.w_ValueError,
                                     space.wrap("couldn't find end of stream"))
            elif rffi.getintfield(bzs, 'c_avail_out') == 0:
                total_out = _bzs_total_out(bzs)
                data = "".join([out_buf[i] for i in range(total_out)])
                temp.append(data)

                lltype.free(out_buf, flavor='raw')
                out_bufsize = _new_buffer_size(out_bufsize)
                out_buf = lltype.malloc(rffi.CCHARP.TO,
                                        out_bufsize,
                                        flavor='raw',
                                        zero=True)
                bzs.c_next_out = out_buf
                rffi.setintfield(bzs, 'c_avail_out', out_bufsize)

        total_out = _bzs_total_out(bzs)
        if temp:
            data = "".join(
                [out_buf[i] for i in range(total_out - len(temp[0]))])
            temp.append(data)
            res = "".join(temp)
        else:
            res = "".join(
                [out_buf[i] for i in range(total_out) if out_buf[i] != '\x00'])

        BZ2_bzDecompressEnd(bzs)
        return space.wrap(res)
    finally:
        lltype.free(bzs, flavor='raw')
        lltype.free(out_buf, flavor='raw')
        lltype.free(in_buf, flavor='raw')