Example #1
0
 def fromint(intval):
     if intval < 0:
         sign = -1
         ival = r_uint(-intval)
     elif intval > 0:
         sign = 1
         ival = r_uint(intval)
     else:
         return rbigint([0], 0)
     # Count the number of Python digits.
     # We used to pick 5 ("big enough for anything"), but that's a
     # waste of time and space given that 5*15 = 75 bits are rarely
     # needed.
     t = ival
     ndigits = 0
     while t:
         ndigits += 1
         t >>= SHIFT
     v = rbigint([0] * ndigits, sign)
     t = ival
     p = 0
     while t:
         v.digits[p] = intmask(t & MASK)
         t >>= SHIFT
         p += 1
     return v
Example #2
0
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] = '-'
        j = 1
    else:
        j = 0
    while j < len:
        result.chars[j] = temp[len-j-1]
        j += 1
    return result
Example #3
0
def ll_dict_lookup(d, key, hash):
    DICT = lltype.typeOf(d).TO
    entries = d.entries
    mask = len(entries) - 1
    i = hash & mask
    # do the first try before any looping
    if entries.valid(i):
        checkingkey = entries[i].key
        if checkingkey == key:
            return i  # found the entry
        if d.keyeq is not None and entries.hash(i) == hash:
            # correct hash, maybe the key is e.g. a different pointer to
            # an equal object
            found = d.keyeq(checkingkey, key)
            if DICT.paranoia:
                if (entries != d.entries or not entries.valid(i)
                        or entries[i].key != checkingkey):
                    # the compare did major nasty stuff to the dict: start over
                    return ll_dict_lookup(d, key, hash)
            if found:
                return i  # found the entry
        freeslot = -1
    elif entries.everused(i):
        freeslot = i
    else:
        return i  # pristine entry -- lookup failed

    # In the loop, a deleted entry (everused and not valid) is by far
    # (factor of 100s) the least likely outcome, so test for that last.
    perturb = r_uint(hash)
    while 1:
        # compute the next index using unsigned arithmetic
        i = r_uint(i)
        i = (i << 2) + i + perturb + 1
        i = intmask(i) & mask
        # keep 'i' as a signed number here, to consistently pass signed
        # arguments to the small helper methods.
        if not entries.everused(i):
            if freeslot == -1:
                freeslot = i
            return freeslot
        elif entries.valid(i):
            checkingkey = entries[i].key
            if checkingkey == key:
                return i
            if d.keyeq is not None and entries.hash(i) == hash:
                # correct hash, maybe the key is e.g. a different pointer to
                # an equal object
                found = d.keyeq(checkingkey, key)
                if DICT.paranoia:
                    if (entries != d.entries or not entries.valid(i)
                            or entries[i].key != checkingkey):
                        # the compare did major nasty stuff to the dict:
                        # start over
                        return ll_dict_lookup(d, key, hash)
                if found:
                    return i  # found the entry
        elif freeslot == -1:
            freeslot = i
        perturb >>= PERTURB_SHIFT
Example #4
0
 def test_uintmask(self):
     assert rbigint.fromint(-1).uintmask() == r_uint(-1)
     assert rbigint.fromint(0).uintmask() == r_uint(0)
     assert (rbigint.fromint(sys.maxint).uintmask() ==
             r_uint(sys.maxint))
     assert (rbigint.fromlong(sys.maxint+1).uintmask() ==
             r_uint(-sys.maxint-1))
Example #5
0
 def fromint(intval):
     if intval < 0:
         sign = -1
         ival = r_uint(-intval)
     elif intval > 0:
         sign = 1
         ival = r_uint(intval)
     else:
         return rbigint()
     # Count the number of Python digits.
     # We used to pick 5 ("big enough for anything"), but that's a
     # waste of time and space given that 5*15 = 75 bits are rarely
     # needed.
     t = ival
     ndigits = 0
     while t:
         ndigits += 1
         t >>= SHIFT
     v = rbigint([0] * ndigits, sign)
     t = ival
     p = 0
     while t:
         v._setdigit(p, t)
         t >>= SHIFT
         p += 1
     return v
Example #6
0
def ll_int2hex(i, addPrefix):
    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] = hex_chars[i & 0xf]
            i >>= 4
            len += 1
    len += sign
    if addPrefix:
        len += 2
    result = mallocstr(len)
    result.hash = 0
    j = 0
    if sign:
        result.chars[0] = '-'
        j = 1
    if addPrefix:
        result.chars[j] = '0'
        result.chars[j+1] = 'x'
        j += 2
    while j < len:
        result.chars[j] = temp[len-j-1]
        j += 1
    return result
Example #7
0
 def test_unsigned(self):
     for op, fn in [('x + y', lambda x, y: x + y),
                    ('x - y', lambda x, y: x - y),
                    ('x * y', lambda x, y: x * y),
                    ('x // y', lambda x, y: x // y),
                    ('x % y', lambda x, y: x % y),
                    ('x << y', lambda x, y: x << y),
                    ('x >> y', lambda x, y: x >> y),
                    ('x ^ y', lambda x, y: x ^ y),
                    ('x & y', lambda x, y: x & y),
                    ('x | y', lambda x, y: x | y),
                    ('-y', lambda x, y: -y),
                    ('~y', lambda x, y: ~y),
                    ]:
         fp = self.rgen(fn, [r_uint, r_uint])
         print op
         fn1 = lambda x, y: fn(r_uint(x), r_uint(y))
         fp1 = lambda x, y: r_uint(fp(x, y))
         assert fp1(40, 2) == fn1(40, 2)
         assert fp1(25, 3) == fn1(25, 3)
         assert fp1(40, 2) == fn1(40, 2)
         assert fp1(25, 3) == fn1(25, 3)
         assert fp1(149, 32) == fn1(149, 32)
         assert fp1(149, 33) == fn1(149, 33)
         assert fp1(149, 65) == fn1(149, 65)
         assert fp1(149, 150) == fn1(149, 150)
         big = r_uint(-42)
         assert fp1(big, 3) == fn1(big, 3)
         if op not in ('x << y', 'x >> y'):
             assert fp1(38, big) == fn1(38, big)
             assert fp1(big-5, big-12) == fn1(big-5, big-12)
Example #8
0
 def test_unsigned(self):
     for op, fn in [
         ("x + y", lambda x, y: x + y),
         ("x - y", lambda x, y: x - y),
         ("x * y", lambda x, y: x * y),
         ("x // y", lambda x, y: x // y),
         ("x % y", lambda x, y: x % y),
         ("x << y", lambda x, y: x << y),
         ("x >> y", lambda x, y: x >> y),
         ("x ^ y", lambda x, y: x ^ y),
         ("x & y", lambda x, y: x & y),
         ("x | y", lambda x, y: x | y),
         ("-y", lambda x, y: -y),
         ("~y", lambda x, y: ~y),
     ]:
         fp = self.rgen(fn, [r_uint, r_uint])
         print op
         fn1 = lambda x, y: fn(r_uint(x), r_uint(y))
         fp1 = lambda x, y: r_uint(fp(x, y))
         assert fp1(40, 2) == fn1(40, 2)
         assert fp1(25, 3) == fn1(25, 3)
         assert fp1(40, 2) == fn1(40, 2)
         assert fp1(25, 3) == fn1(25, 3)
         assert fp1(149, 32) == fn1(149, 32)
         assert fp1(149, 33) == fn1(149, 33)
         assert fp1(149, 65) == fn1(149, 65)
         assert fp1(149, 150) == fn1(149, 150)
         big = r_uint(-42)
         assert fp1(big, 3) == fn1(big, 3)
         if op not in ("x << y", "x >> y"):
             assert fp1(38, big) == fn1(38, big)
             assert fp1(big - 5, big - 12) == fn1(big - 5, big - 12)
 def test_uintmask(self):
     assert rbigint.fromint(-1).uintmask() == r_uint(-1)
     assert rbigint.fromint(0).uintmask() == r_uint(0)
     assert (rbigint.fromint(sys.maxint).uintmask() ==
             r_uint(sys.maxint))
     assert (rbigint.fromlong(sys.maxint+1).uintmask() ==
             r_uint(-sys.maxint-1))
Example #10
0
def get_len_of_range(lo, hi, step):
    """
    Return number of items in range/xrange (lo, hi, step).
    Raise ValueError if step == 0 and OverflowError if the true value is too
    large to fit in a signed long.
    """

    # If lo >= hi, the range is empty.
    # Else if n values are in the range, the last one is
    # lo + (n-1)*step, which must be <= hi-1.  Rearranging,
    # n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
    # the proper value.  Since lo < hi in this case, hi-lo-1 >= 0, so
    # the RHS is non-negative and so truncation is the same as the
    # floor.  Letting M be the largest positive long, the worst case
    # for the RHS numerator is hi=M, lo=-M-1, and then
    # hi-lo-1 = M-(-M-1)-1 = 2*M.  Therefore unsigned long has enough
    # precision to compute the RHS exactly.
    if step == 0:
        raise ValueError
    elif step < 0:
        lo, hi, step = hi, lo, -step
    if lo < hi:
        uhi = r_uint(hi)
        ulo = r_uint(lo)
        diff = uhi - ulo - 1
        n = intmask(diff // r_uint(step) + 1)
        if n < 0:
            raise OverflowError
    else:
        n = 0
    return n
Example #11
0
def ll_int2oct(i, addPrefix):
    from pypy.rpython.lltypesystem.rstr import mallocstr
    if i == 0:
        result = mallocstr(1)
        result.hash = 0
        result.chars[0] = '0'
        return result
    temp = malloc(CHAR_ARRAY, 25)
    len = 0
    sign = 0
    if i < 0:
        sign = 1
        i = r_uint(-i)
    else:
        i = r_uint(i)
    while i:
        temp[len] = hex_chars[i & 0x7]
        i >>= 3
        len += 1
    len += sign
    if addPrefix:
        len += 1
    result = mallocstr(len)
    result.hash = 0
    j = 0
    if sign:
        result.chars[0] = '-'
        j = 1
    if addPrefix:
        result.chars[j] = '0'
        j += 1
    while j < len:
        result.chars[j] = temp[len-j-1]
        j += 1
    return result
Example #12
0
def test_invert():
    def f(x):
        return ~x

    res = interpret(f, [3])
    assert res == ~3
    assert interpret(f, [r_uint(3)]) == ~r_uint(3)
Example #13
0
def ll_dict_lookup(d, key, hash):
    DICT = lltype.typeOf(d).TO
    entries = d.entries
    mask = len(entries) - 1
    i = hash & mask
    # do the first try before any looping 
    if entries.valid(i):
        checkingkey = entries[i].key
        if checkingkey == key:
            return i   # found the entry
        if d.keyeq is not None and entries.hash(i) == hash:
            # correct hash, maybe the key is e.g. a different pointer to
            # an equal object
            found = d.keyeq(checkingkey, key)
            if DICT.paranoia:
                if (entries != d.entries or
                    not entries.valid(i) or entries[i].key != checkingkey):
                    # the compare did major nasty stuff to the dict: start over
                    return ll_dict_lookup(d, key, hash)
            if found:
                return i   # found the entry
        freeslot = -1
    elif entries.everused(i):
        freeslot = i
    else:
        return i    # pristine entry -- lookup failed

    # In the loop, a deleted entry (everused and not valid) is by far
    # (factor of 100s) the least likely outcome, so test for that last.
    perturb = r_uint(hash) 
    while 1: 
        # compute the next index using unsigned arithmetic
        i = r_uint(i)
        i = (i << 2) + i + perturb + 1
        i = intmask(i) & mask
        # keep 'i' as a signed number here, to consistently pass signed
        # arguments to the small helper methods.
        if not entries.everused(i):
            if freeslot == -1:
                freeslot = i
            return freeslot
        elif entries.valid(i):
            checkingkey = entries[i].key
            if checkingkey == key:
                return i
            if d.keyeq is not None and entries.hash(i) == hash:
                # correct hash, maybe the key is e.g. a different pointer to
                # an equal object
                found = d.keyeq(checkingkey, key)
                if DICT.paranoia:
                    if (entries != d.entries or
                        not entries.valid(i) or entries[i].key != checkingkey):
                        # the compare did major nasty stuff to the dict:
                        # start over
                        return ll_dict_lookup(d, key, hash)
                if found:
                    return i   # found the entry
        elif freeslot == -1:
            freeslot = i 
        perturb >>= PERTURB_SHIFT
Example #14
0
 def test_unsigned(self):
     for op, fn in [
         ('x + y', lambda x, y: x + y),
         ('x - y', lambda x, y: x - y),
         ('x * y', lambda x, y: x * y),
         ('x // y', lambda x, y: x // y),
         ('x % y', lambda x, y: x % y),
         ('x << y', lambda x, y: x << y),
         ('x >> y', lambda x, y: x >> y),
         ('x ^ y', lambda x, y: x ^ y),
         ('x & y', lambda x, y: x & y),
         ('x | y', lambda x, y: x | y),
         ('-y', lambda x, y: -y),
         ('~y', lambda x, y: ~y),
     ]:
         fp = self.rgen(fn, [r_uint, r_uint])
         print op
         fn1 = lambda x, y: fn(r_uint(x), r_uint(y))
         fp1 = lambda x, y: r_uint(fp(x, y))
         assert fp1(40, 2) == fn1(40, 2)
         assert fp1(25, 3) == fn1(25, 3)
         assert fp1(40, 2) == fn1(40, 2)
         assert fp1(25, 3) == fn1(25, 3)
         assert fp1(149, 32) == fn1(149, 32)
         assert fp1(149, 33) == fn1(149, 33)
         assert fp1(149, 65) == fn1(149, 65)
         assert fp1(149, 150) == fn1(149, 150)
         big = r_uint(-42)
         assert fp1(big, 3) == fn1(big, 3)
         if op not in ('x << y', 'x >> y'):
             assert fp1(38, big) == fn1(38, big)
             assert fp1(big - 5, big - 12) == fn1(big - 5, big - 12)
Example #15
0
def get_len_of_range(space, lo, hi, step):
    """
    Return number of items in range/xrange (lo, hi, step).
    Raise ValueError if step == 0 and OverflowError if the true value is too
    large to fit in a signed long.
    """

    # If lo >= hi, the range is empty.
    # Else if n values are in the range, the last one is
    # lo + (n-1)*step, which must be <= hi-1.  Rearranging,
    # n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
    # the proper value.  Since lo < hi in this case, hi-lo-1 >= 0, so
    # the RHS is non-negative and so truncation is the same as the
    # floor.  Letting M be the largest positive long, the worst case
    # for the RHS numerator is hi=M, lo=-M-1, and then
    # hi-lo-1 = M-(-M-1)-1 = 2*M.  Therefore unsigned long has enough
    # precision to compute the RHS exactly.
    if step == 0:
        raise OperationError(space.w_ValueError,
                             space.wrap("step argument must not be zero"))
    elif step < 0:
        lo, hi, step = hi, lo, -step
    if lo < hi:
        uhi = r_uint(hi)
        ulo = r_uint(lo)
        diff = uhi - ulo - 1
        n = intmask(diff // r_uint(step) + 1)
        if n < 0:
            raise OperationError(space.w_OverflowError,
                                 space.wrap("result has too many items"))
    else:
        n = 0
    return n
Example #16
0
 def _index_cache(self, selector):
     space = self.space
     cache = space.fromcache(IndexCache)
     SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp
     SHIFT1 = SHIFT2 - 5
     attrs_as_int = objectmodel.current_object_addr_as_int(self)
     # ^^^Note: see comment in typeobject.py for
     # _pure_lookup_where_with_method_cache()
     hash_selector = objectmodel.compute_hash(selector)
     product = intmask(attrs_as_int * hash_selector)
     index_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2
     # ^^^Note2: same comment too
     cached_attr = cache.attrs[index_hash]
     if cached_attr is self:
         cached_selector = cache.selectors[index_hash]
         if cached_selector == selector:
             index = cache.indices[index_hash]
             if space.config.objspace.std.withmethodcachecounter:
                 name = selector[0]
                 cache.hits[name] = cache.hits.get(name, 0) + 1
             return index
     index = self._index(selector)
     cache.attrs[index_hash] = self
     cache.selectors[index_hash] = selector
     cache.indices[index_hash] = index
     if space.config.objspace.std.withmethodcachecounter:
         name = selector[0]
         cache.misses[name] = cache.misses.get(name, 0) + 1
     return index
Example #17
0
 def test_cast_primitive(self):
     from pypy.rpython.annlowlevel import LowLevelAnnotatorPolicy
     def llf(u):
         return lltype.cast_primitive(lltype.Signed, u)
     res = self.interpret(llf, [r_uint(-1)], policy=LowLevelAnnotatorPolicy())
     assert res == -1
     res = self.interpret(llf, ['x'], policy=LowLevelAnnotatorPolicy())
     assert res == ord('x')
     def llf(v):
         return lltype.cast_primitive(lltype.Unsigned, v)
     res = self.interpret(llf, [-1], policy=LowLevelAnnotatorPolicy())
     assert res == r_uint(-1)
     res = self.interpret(llf, [u'x'], policy=LowLevelAnnotatorPolicy())
     assert res == ord(u'x')
     res = self.interpret(llf, [1.0], policy=LowLevelAnnotatorPolicy())
     assert res == r_uint(1)
     def llf(v):
         return lltype.cast_primitive(lltype.Char, v)
     res = self.interpret(llf, [ord('x')], policy=LowLevelAnnotatorPolicy())
     assert res == 'x'
     def llf(v):
         return lltype.cast_primitive(lltype.UniChar, v)
     res = self.interpret(llf, [ord('x')], policy=LowLevelAnnotatorPolicy())
     assert res == u'x'
     def llf(v):
         return lltype.cast_primitive(rffi.SHORT, v)
     res = self.interpret(llf, [123], policy=LowLevelAnnotatorPolicy())
     assert res == 123
     def llf(v):
         return lltype.cast_primitive(lltype.Signed, v)
     res = self.interpret(llf, [rffi.r_short(123)], policy=LowLevelAnnotatorPolicy())
     assert res == 123
Example #18
0
    def test_blit_rect(self):
        surface = RSDL.CreateRGBSurface(0, 150, 50, 32,
                                        r_uint(0x000000FF),
                                        r_uint(0x0000FF00),
                                        r_uint(0x00FF0000),
                                        r_uint(0xFF000000))
        fmt = surface.c_format
        color = RSDL.MapRGB(fmt, 255, 0, 0)
        RSDL.FillRect(surface, lltype.nullptr(RSDL.Rect), color)
        
        paintrect = RSDL_helper.mallocrect(75, 0, 150, 50)
        dstrect = lltype.malloc(RSDL.Rect, flavor='raw')
        try:
            color = RSDL.MapRGB(fmt, 255, 128, 0)
            RSDL.FillRect(surface, paintrect, color)

            rffi.setintfield(dstrect, 'c_x',  10)
            rffi.setintfield(dstrect, 'c_y',  10)
            rffi.setintfield(dstrect, 'c_w', 150)
            rffi.setintfield(dstrect, 'c_h',  50)
            RSDL.BlitSurface(surface, lltype.nullptr(RSDL.Rect), self.screen, dstrect)
            RSDL.Flip(self.screen)
        finally:
            lltype.free(dstrect, flavor='raw')
            lltype.free(paintrect, flavor='raw')
        RSDL.FreeSurface(surface)
        self.check("Half Red/Orange rectangle(150px * 50px) at the top left, 10 pixels from the border")
Example #19
0
def test_invert():
    def f(x):
        return ~x

    res = interpret(f, [3])
    assert res == ~3
    assert interpret(f, [r_uint(3)]) == ~r_uint(3)
Example #20
0
def min_max_acc_method(size, signed):
    if signed:
        min = -(2 ** (8*size-1))
        max = (2 ** (8*size-1)) - 1
        if size <= native_int_size:
            accept_method = 'accept_int_arg'
            min = int(min)
            max = int(max)
        else:
            accept_method = 'accept_longlong_arg'
            min = r_longlong(min)
            max = r_longlong(max)
    else:
        min = 0
        max = (2 ** (8*size)) - 1
        if size < native_int_size:
            accept_method = 'accept_int_arg'
        elif size == native_int_size:
            accept_method = 'accept_uint_arg'
            min = r_uint(min)
            max = r_uint(max)
        else:
            accept_method = 'accept_ulonglong_arg'
            min = r_ulonglong(min)
            max = r_ulonglong(max)
    return min, max, accept_method
Example #21
0
def wrapint(space, x, w_symbolic=False, w_s=''):
    if space.config.objspace.std.withsmallint:
        from pypy.objspace.std.smallintobject import W_SmallIntObject
        try:
            return W_SmallIntObject(x)
        except OverflowError:
            from pypy.objspace.std.intobject import W_IntObject
            return W_IntObject(x, w_symbolic, w_s)
    elif space.config.objspace.std.withprebuiltint:
        from pypy.objspace.std.intobject import W_IntObject
        lower = space.config.objspace.std.prebuiltintfrom
        upper =  space.config.objspace.std.prebuiltintto
        # use r_uint to perform a single comparison (this whole function
        # is getting inlined into every caller so keeping the branching
        # to a minimum is a good idea)
        index = r_uint(x - lower)
        if index >= r_uint(upper - lower):
            w_res = instantiate(W_IntObject)
        else:
            w_res = W_IntObject.PREBUILT[index]
        # obscure hack to help the CPU cache: we store 'x' even into
        # a prebuilt integer's intval.  This makes sure that the intval
        # field is present in the cache in the common case where it is
        # quickly reused.  (we could use a prefetch hint if we had that)
        w_res.intval = x
        return w_res
    else:
        from pypy.objspace.std.intobject import W_IntObject
        return W_IntObject(x, w_symbolic, w_s)
Example #22
0
def min_max_acc_method(size, signed):
    if signed:
        min = -(2**(8 * size - 1))
        max = (2**(8 * size - 1)) - 1
        if size <= native_int_size:
            accept_method = 'accept_int_arg'
            min = int(min)
            max = int(max)
        else:
            accept_method = 'accept_longlong_arg'
            min = r_longlong(min)
            max = r_longlong(max)
    else:
        min = 0
        max = (2**(8 * size)) - 1
        if size < native_int_size:
            accept_method = 'accept_int_arg'
        elif size == native_int_size:
            accept_method = 'accept_uint_arg'
            min = r_uint(min)
            max = r_uint(max)
        else:
            accept_method = 'accept_ulonglong_arg'
            min = r_ulonglong(min)
            max = r_ulonglong(max)
    return min, max, accept_method
Example #23
0
 def _index_cache(self, selector):
     space = self.space
     cache = space.fromcache(IndexCache)
     SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp
     SHIFT1 = SHIFT2 - 5
     attrs_as_int = objectmodel.current_object_addr_as_int(self)
     # ^^^Note: see comment in typeobject.py for
     # _pure_lookup_where_with_method_cache()
     hash_selector = objectmodel.compute_hash(selector)
     product = intmask(attrs_as_int * hash_selector)
     index_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2
     # ^^^Note2: same comment too
     cached_attr = cache.attrs[index_hash]
     if cached_attr is self:
         cached_selector = cache.selectors[index_hash]
         if cached_selector == selector:
             index = cache.indices[index_hash]
             if space.config.objspace.std.withmethodcachecounter:
                 name = selector[0]
                 cache.hits[name] = cache.hits.get(name, 0) + 1
             return index
     index = self._index(selector)
     cache.attrs[index_hash] = self
     cache.selectors[index_hash] = selector
     cache.indices[index_hash] = index
     if space.config.objspace.std.withmethodcachecounter:
         name = selector[0]
         cache.misses[name] = cache.misses.get(name, 0) + 1
     return index
Example #24
0
 def _GetContents(self, fp):
     endrec = _EndRecData(fp)
     if not endrec:
         raise BadZipfile, "File is not a zip file"
     size_cd = endrec.stuff[5]             # bytes in central directory
     offset_cd = endrec.stuff[6]   # offset of central directory
     self.comment = endrec.comment
     x = endrec.filesize - size_cd
     concat = x - offset_cd
     self.start_dir = offset_cd + concat
     fp.seek(self.start_dir, 0)
     total = 0
     while total < size_cd:
         centdir = fp.read(46)
         total = total + 46
         if centdir[0:4] != stringCentralDir:
             raise BadZipfile, "Bad magic number for central directory"
         centdir = runpack(structCentralDir, centdir)
         filename = fp.read(centdir[_CD_FILENAME_LENGTH])
         # Create ZipInfo instance to store file information
         x = RZipInfo(filename)
         x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
         x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
         total = (total + centdir[_CD_FILENAME_LENGTH]
                  + centdir[_CD_EXTRA_FIELD_LENGTH]
                  + centdir[_CD_COMMENT_LENGTH])
         x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat
         # file_offset must be computed below...
         (x.create_version, x.create_system, x.extract_version, x.reserved,
             x.flag_bits, x.compress_type, t, d,
             crc, x.compress_size, x.file_size) = centdir[1:12]
         x.CRC = r_uint(crc) & r_uint(0xffffffff)
         x.dostime = t
         x.dosdate = d
         x.volume, x.internal_attr, x.external_attr = centdir[15:18]
         # Convert date/time code to (year, month, day, hour, min, sec)
         x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
                                  t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
         self.filelist.append(x)
         self.NameToInfo[x.filename] = x
     for data in self.filelist:
         fp.seek(data.header_offset, 0)
         fheader = fp.read(30)
         if fheader[0:4] != stringFileHeader:
             raise BadZipfile, "Bad magic number for file header"
         fheader = runpack(structFileHeader, fheader)
         # file_offset is computed here, since the extra field for
         # the central directory and for the local file header
         # refer to different fields, and they can have different
         # lengths
         data.file_offset = (data.header_offset + 30
                             + fheader[_FH_FILENAME_LENGTH]
                             + fheader[_FH_EXTRA_FIELD_LENGTH])
         fname = fp.read(fheader[_FH_FILENAME_LENGTH])
         if fname != data.orig_filename:
             raise BadZipfile, \
                   'File name in directory "%s" and header "%s" differ.' % (
                       data.orig_filename, fname)
     fp.seek(self.start_dir, 0)
Example #25
0
 def _GetContents(self, fp):
     endrec = _EndRecData(fp)
     if not endrec:
         raise BadZipfile, "File is not a zip file"
     size_cd = endrec.stuff[5]  # bytes in central directory
     offset_cd = endrec.stuff[6]  # offset of central directory
     self.comment = endrec.comment
     x = endrec.filesize - size_cd
     concat = x - offset_cd
     self.start_dir = offset_cd + concat
     fp.seek(self.start_dir, 0)
     total = 0
     while total < size_cd:
         centdir = fp.read(46)
         total = total + 46
         if centdir[0:4] != stringCentralDir:
             raise BadZipfile, "Bad magic number for central directory"
         centdir = runpack(structCentralDir, centdir)
         filename = fp.read(centdir[_CD_FILENAME_LENGTH])
         # Create ZipInfo instance to store file information
         x = RZipInfo(filename)
         x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
         x.comment = fp.read(centdir[_CD_COMMENT_LENGTH])
         total = (total + centdir[_CD_FILENAME_LENGTH] +
                  centdir[_CD_EXTRA_FIELD_LENGTH] +
                  centdir[_CD_COMMENT_LENGTH])
         x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat
         # file_offset must be computed below...
         (x.create_version, x.create_system, x.extract_version, x.reserved,
          x.flag_bits, x.compress_type, t, d, crc, x.compress_size,
          x.file_size) = centdir[1:12]
         x.CRC = r_uint(crc) & r_uint(0xffffffff)
         x.dostime = t
         x.dosdate = d
         x.volume, x.internal_attr, x.external_attr = centdir[15:18]
         # Convert date/time code to (year, month, day, hour, min, sec)
         x.date_time = ((d >> 9) + 1980, (d >> 5) & 0xF, d & 0x1F, t >> 11,
                        (t >> 5) & 0x3F, (t & 0x1F) * 2)
         self.filelist.append(x)
         self.NameToInfo[x.filename] = x
     for data in self.filelist:
         fp.seek(data.header_offset, 0)
         fheader = fp.read(30)
         if fheader[0:4] != stringFileHeader:
             raise BadZipfile, "Bad magic number for file header"
         fheader = runpack(structFileHeader, fheader)
         # file_offset is computed here, since the extra field for
         # the central directory and for the local file header
         # refer to different fields, and they can have different
         # lengths
         data.file_offset = (data.header_offset + 30 +
                             fheader[_FH_FILENAME_LENGTH] +
                             fheader[_FH_EXTRA_FIELD_LENGTH])
         fname = fp.read(fheader[_FH_FILENAME_LENGTH])
         if fname != data.orig_filename:
             raise BadZipfile, \
                   'File name in directory "%s" and header "%s" differ.' % (
                       data.orig_filename, fname)
     fp.seek(self.start_dir, 0)
Example #26
0
 def f(x):
     if x == r_uint(3):
         return 9
     elif x == r_uint(9):
         return 27
     elif x == r_uint(27):
         return 3
     return 0
Example #27
0
    def test_uint_floordiv(self):
        from pypy.rlib.rarithmetic import r_uint
        
        def f(a, b):
            return a/b

        res = self.interp_operations(f, [r_uint(4), r_uint(3)])
        assert res == 1
Example #28
0
def test_crc32():
    """
    When called with a string, rzlib.crc32 should compute its CRC32 and
    return it as a unsigned 32 bit integer.
    """
    assert rzlib.crc32('') == r_uint(0)
    assert rzlib.crc32('\0') == r_uint(3523407757)
    assert rzlib.crc32('hello, world.') == r_uint(3358036098)
Example #29
0
 def f(x):
     if x == r_uint(3):
         return 9
     elif x == r_uint(9):
         return 27
     elif x == r_uint(27):
         return 3
     return 0
Example #30
0
def test_crc32():
    """
    When called with a string, rzlib.crc32 should compute its CRC32 and
    return it as a unsigned 32 bit integer.
    """
    assert rzlib.crc32('') == r_uint(0)
    assert rzlib.crc32('\0') == r_uint(3523407757)
    assert rzlib.crc32('hello, world.') == r_uint(3358036098)
Example #31
0
    def test_uint_floordiv(self):
        from pypy.rlib.rarithmetic import r_uint

        def f(a, b):
            return a / b

        res = self.interp_operations(f, [r_uint(4), r_uint(3)])
        assert res == 1
Example #32
0
 def test_unsigned(self):
     space = self.space
     self.check(app_types.uint, space.wrap(42), r_uint(42))
     self.check(app_types.uint, space.wrap(-1), r_uint(sys.maxint * 2 + 1))
     self.check(app_types.uint, space.wrap(sys.maxint * 3),
                r_uint(sys.maxint - 2))
     self.check(app_types.ulong, space.wrap(sys.maxint + 12),
                r_uint(sys.maxint + 12))
     self.check(app_types.ulong, space.wrap(sys.maxint * 2 + 3), r_uint(1))
Example #33
0
def test_adler32():
    """
    When called with a string, zlib.crc32 should compute its adler 32
    checksum and return it as an unsigned 32 bit integer.
    """
    assert rzlib.adler32('') == r_uint(1)
    assert rzlib.adler32('\0') == r_uint(65537)
    assert rzlib.adler32('hello, world.') == r_uint(571147447)
    assert rzlib.adler32('x' * 23) == r_uint(2172062409)
Example #34
0
 def test_unsigned(self):
     space = self.space
     self.check(app_types.uint, space.wrap(42), r_uint(42))
     self.check(app_types.uint, space.wrap(-1), r_uint(sys.maxint*2 +1))
     self.check(app_types.uint, space.wrap(sys.maxint*3),
                r_uint(sys.maxint - 2))
     self.check(app_types.ulong, space.wrap(sys.maxint+12),
                r_uint(sys.maxint+12))
     self.check(app_types.ulong, space.wrap(sys.maxint*2+3), r_uint(1))
Example #35
0
def test_adler32():
    """
    When called with a string, zlib.crc32 should compute its adler 32
    checksum and return it as an unsigned 32 bit integer.
    """
    assert rzlib.adler32('') == r_uint(1)
    assert rzlib.adler32('\0') == r_uint(65537)
    assert rzlib.adler32('hello, world.') == r_uint(571147447)
    assert rzlib.adler32('x' * 23) == r_uint(2172062409)
Example #36
0
def crc32(s, crc=0):
    result = 0
    crc = ~r_uint(crc) & 0xffffffffL
    for c in s:
        crc = rcrc_32_tab[(crc ^ r_uint(ord(c))) & 0xffL] ^ (crc >> 8)
        #/* Note:  (crc >> 8) MUST zero fill on left

        result = crc ^ 0xffffffffL
    
    return result
Example #37
0
def crc32(s, crc=0):
    result = 0
    crc = ~r_uint(crc) & 0xffffffffL
    for c in s:
        crc = rcrc_32_tab[(crc ^ r_uint(ord(c))) & 0xffL] ^ (crc >> 8)
        #/* Note:  (crc >> 8) MUST zero fill on left

        result = crc ^ 0xffffffffL

    return result
Example #38
0
def test_surface_basic():
    assert RSDL.Init(RSDL.INIT_VIDEO) >= 0
    surface = RSDL.CreateRGBSurface(0, 150, 50, 32, r_uint(0x000000FF),
                                    r_uint(0x0000FF00), r_uint(0x00FF0000),
                                    r_uint(0xFF000000))
    assert surface
    assert rffi.getintfield(surface, 'c_w') == 150
    assert rffi.getintfield(surface, 'c_h') == 50
    RSDL.FreeSurface(surface)
    RSDL.Quit()
Example #39
0
 def __init__(self, large_alloc_size = LARGE_ALLOC_SIZE,
                    min_fragment     = MIN_FRAGMENT,
                    num_indices      = NUM_INDICES):
     self.total_memory_allocated = r_uint(0)
     self.total_mallocs = r_uint(0)
     self.large_alloc_size = large_alloc_size
     self.min_fragment = min_fragment
     self.num_indices = num_indices
     self.free_blocks = {}      # map {start: stop}
     self.free_blocks_end = {}  # map {stop: start}
     self.blocks_by_size = [[] for i in range(self.num_indices)]
Example #40
0
    def _init(self):
        "Initialisation."
        self.count = r_ulonglong(0)   # total number of bytes
        self.input = ""   # pending unprocessed data, < 64 bytes

        # Initial 160 bit message digest (5 times 32 bit).
        self.H0 = r_uint(0x67452301L)
        self.H1 = r_uint(0xEFCDAB89L)
        self.H2 = r_uint(0x98BADCFEL)
        self.H3 = r_uint(0x10325476L)
        self.H4 = r_uint(0xC3D2E1F0L)
Example #41
0
File: rsha.py Project: njues/Sypy
def _string2uintlist(s, start, count, result):
    """Build a list of count r_uint's by unpacking the string
    s[start:start+4*count] in big-endian order.
    """
    for i in range(count):
        p = start + i * 4
        x = r_uint(ord(s[p + 3]))
        x |= r_uint(ord(s[p + 2])) << 8
        x |= r_uint(ord(s[p + 1])) << 16
        x |= r_uint(ord(s[p])) << 24
        result[i] = x
Example #42
0
File: rmd5.py Project: alkorzt/pypy
def _string2uintlist(s, start, count, result):
    """Build a list of count r_uint's by unpacking the string
    s[start:start+4*count] in little-endian order.
    """
    for i in range(count):
        p = start + i * 4
        x = r_uint(ord(s[p]))
        x |= r_uint(ord(s[p+1])) << 8
        x |= r_uint(ord(s[p+2])) << 16
        x |= r_uint(ord(s[p+3])) << 24
        result[i] = x
Example #43
0
    def _init(self):
        """Set this object to an initial empty state.
        """
        self.count = r_ulonglong(0)   # total number of bytes
        self.input = ""   # pending unprocessed data, < 64 bytes

        # Load magic initialization constants.
        self.A = r_uint(0x67452301L)
        self.B = r_uint(0xefcdab89L)
        self.C = r_uint(0x98badcfeL)
        self.D = r_uint(0x10325476L)
Example #44
0
 def ll_lookup_clean(d, hash):
     entries = d.entries
     mask = len(entries) - 1
     i = hash & mask
     perturb = r_uint(hash)
     while entries.everused(i):
         i = r_uint(i)
         i = (i << 2) + i + perturb + 1
         i = intmask(i) & mask
         perturb >>= LLOrderedDict.PERTURB_SHIFT
     return i
Example #45
0
 def ll_lookup_clean(d, hash):
     entries = d.entries
     mask = len(entries) - 1
     i = hash & mask
     perturb = r_uint(hash)
     while entries.everused(i):
         i = r_uint(i)
         i = (i << 2) + i + perturb + 1
         i = intmask(i) & mask
         perturb >>= LLOrderedDict.PERTURB_SHIFT
     return i
Example #46
0
 def _touint_helper(self):
     x = r_uint(0)
     i = self._numdigits() - 1
     while i >= 0:
         prev = x
         x = r_uint((x << SHIFT) + self.digits[i])
         if (x >> SHIFT) != prev:
             raise OverflowError(
                     "long int too large to convert to unsigned int")
         i -= 1
     return x
Example #47
0
    def semlock_acquire(self, space, block, w_timeout):
        if not block:
            full_msecs = 0
        elif space.is_w(w_timeout, space.w_None):
            full_msecs = rwin32.INFINITE
        else:
            timeout = space.float_w(w_timeout)
            timeout *= 1000.0
            if timeout < 0.0:
                timeout = 0.0
            elif timeout >= 0.5 * rwin32.INFINITE:  # 25 days
                raise OperationError(space.w_OverflowError,
                                     space.wrap("timeout is too large"))
            full_msecs = r_uint(int(timeout + 0.5))

        # check whether we can acquire without blocking
        res = rwin32.WaitForSingleObject(self.handle, 0)

        if res != rwin32.WAIT_TIMEOUT:
            return True

        msecs = full_msecs
        start = _GetTickCount()

        while True:
            from pypy.module.rctime.interp_time import State
            interrupt_event = space.fromcache(State).get_interrupt_event()
            handles = [self.handle, interrupt_event]

            # do the wait
            rwin32.ResetEvent(interrupt_event)
            res = rwin32.WaitForMultipleObjects(handles, timeout=msecs)

            if res != rwin32.WAIT_OBJECT_0 + 1:
                break

            # got SIGINT so give signal handler a chance to run
            time.sleep(0.001)

            # if this is main thread let KeyboardInterrupt be raised
            _check_signals(space)

            # recalculate timeout
            if msecs != rwin32.INFINITE:
                ticks = _GetTickCount()
                if r_uint(ticks - start) >= full_msecs:
                    return False
                msecs = full_msecs - r_uint(ticks - start)

        # handle result
        if res != rwin32.WAIT_TIMEOUT:
            return True
        return False
Example #48
0
def test_primitive_is_true():
    def var_is_true(v):
        return bool(v)
    f = compile_function(var_is_true, [int])
    assert f(256)
    assert not f(0)
    f = compile_function(var_is_true, [r_uint])
    assert f(r_uint(256))
    assert not f(r_uint(0))
    f = compile_function(var_is_true, [float])
    assert f(256.0)
    assert not f(0.0)
Example #49
0
 def __init__(self,
              large_alloc_size=LARGE_ALLOC_SIZE,
              min_fragment=MIN_FRAGMENT,
              num_indices=NUM_INDICES):
     self.total_memory_allocated = r_uint(0)
     self.total_mallocs = r_uint(0)
     self.large_alloc_size = large_alloc_size
     self.min_fragment = min_fragment
     self.num_indices = num_indices
     self.free_blocks = {}  # map {start: stop}
     self.free_blocks_end = {}  # map {stop: start}
     self.blocks_by_size = [[] for i in range(self.num_indices)]
Example #50
0
    def semlock_acquire(self, space, block, w_timeout):
        if not block:
            full_msecs = 0
        elif space.is_w(w_timeout, space.w_None):
            full_msecs = rwin32.INFINITE
        else:
            timeout = space.float_w(w_timeout)
            timeout *= 1000.0
            if timeout < 0.0:
                timeout = 0.0
            elif timeout >= 0.5 * rwin32.INFINITE: # 25 days
                raise OperationError(space.w_OverflowError,
                                     space.wrap("timeout is too large"))
            full_msecs = int(timeout + 0.5)

        # check whether we can acquire without blocking
        res = rwin32.WaitForSingleObject(self.handle, 0)

        if res != rwin32.WAIT_TIMEOUT:
            return True

        msecs = r_uint(full_msecs)
        start = _GetTickCount()

        while True:
            from pypy.module.rctime.interp_time import State
            interrupt_event = space.fromcache(State).get_interrupt_event()
            handles = [self.handle, interrupt_event]

            # do the wait
            rwin32.ResetEvent(interrupt_event)
            res = rwin32.WaitForMultipleObjects(handles, timeout=msecs)

            if res != rwin32.WAIT_OBJECT_0 + 1:
                break

            # got SIGINT so give signal handler a chance to run
            time.sleep(0.001)

            # if this is main thread let KeyboardInterrupt be raised
            _check_signals(space)

            # recalculate timeout
            if msecs != rwin32.INFINITE:
                ticks = _GetTickCount()
                if r_uint(ticks - start) >= full_msecs:
                    return False
                msecs = r_uint(full_msecs - (ticks - start))

        # handle result
        if res != rwin32.WAIT_TIMEOUT:
            return True
        return False
Example #51
0
def ll_delitem(func, l, index):
    if func is dum_checkidx:
        length = l.ll_length()
        if r_uint(index) >= r_uint(length):  # see comments in ll_getitem().
            index = r_uint(index) + r_uint(length)
            if index >= r_uint(length):
                raise IndexError
            index = intmask(index)
    else:
        if index < 0:
            index += l.ll_length()
            ll_assert(index >= 0, "negative list delitem index out of bound")
    ll_delitem_nonneg(dum_nocheck, l, index)
Example #52
0
def test_crc32_start_value():
    """
    When called with a string and an integer, zlib.crc32 should compute the
    CRC32 of the string using the integer as the starting value.
    """
    assert rzlib.crc32('', 42) == r_uint(42)
    assert rzlib.crc32('\0', 42) == r_uint(163128923)
    assert rzlib.crc32('hello, world.', 42) == r_uint(1090960721)
    hello = 'hello, '
    hellocrc = rzlib.crc32(hello)
    world = 'world.'
    helloworldcrc = rzlib.crc32(world, hellocrc)
    assert helloworldcrc == rzlib.crc32(hello + world)
Example #53
0
def _string2uintlist(s, start=0, count=16):
    """Build a list of count r_uint's by unpacking the string
    s[start:start+4*count] in little-endian order.
    """
    result = []
    for i in range(count):
        p = start + i * 4
        x = r_uint(ord(s[p]))
        x |= r_uint(ord(s[p + 1])) << 8
        x |= r_uint(ord(s[p + 2])) << 16
        x |= r_uint(ord(s[p + 3])) << 24
        result.append(x)
    return result