Beispiel #1
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 #2
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 #3
0
def _int_binary_operations():
    minint = -sys.maxint - 1
    # Test cases.  Note that for each operation there should be at least
    # one case in which the two input arguments are equal.
    for opnum, testcases in [
        (rop.INT_ADD, [(10, -2, 8), (-60, -60, -120)]),
        (rop.INT_SUB, [(10, -2, 12), (133, 133, 0)]),
        (rop.INT_MUL, [(-6, -3, 18), (15, 15, 225)]),
        (rop.INT_FLOORDIV, [(110, 3, 36), (-110, 3, -36), (110, -3, -36),
                            (-110, -3, 36), (-110, -1, 110),
                            (minint, 1, minint), (-87, -87, 1)]),
        (rop.INT_MOD, [(11, 3, 2), (-11, 3, -2), (11, -3, 2), (-11, -3, -2),
                       (-87, -87, 0)]),
        (rop.INT_AND, [(0xFF00, 0x0FF0, 0x0F00), (-111, -111, -111)]),
        (rop.INT_OR, [(0xFF00, 0x0FF0, 0xFFF0), (-111, -111, -111)]),
        (rop.INT_XOR, [(0xFF00, 0x0FF0, 0xF0F0), (-111, -111, 0)]),
        (rop.INT_LSHIFT, [(10, 4, 10 << 4), (-5, 2, -20), (-5, 0, -5),
                          (3, 3, 24)]),
        (rop.INT_RSHIFT, [(-17, 2, -5), (19, 1, 9), (3, 3, 0)]),
        (rop.UINT_RSHIFT, [(-1, 4, intmask(r_uint(-1) >> r_uint(4))),
                           (1, 4, intmask(r_uint(1) >> r_uint(4))),
                           (3, 3, 0)]),
        (rop.UINT_FLOORDIV, [(4, 3, intmask(r_uint(4) / r_uint(3))),
                             (1, -1, intmask(r_uint(1) / r_uint(-1))),
                             (110, 3, 36),
                             (-110, 3, intmask(r_uint(-110) / r_uint(3))),
                             (110, -3, intmask(r_uint(110) / r_uint(-3))),
                             (-110, -3, intmask(r_uint(-110) / r_uint(-3))),
                             (-110, -1, intmask(r_uint(-110) / r_uint(-1))),
                             (minint, 1, intmask(r_uint(minint) / r_uint(1))),
                             (-87, -87, intmask(r_uint(-87) / r_uint(-87)))])
    ]:
        for x, y, z in testcases:
            yield opnum, [x, y], z
Beispiel #4
0
def _PyLong_FromByteArray(space, bytes, n, little_endian, signed):
    little_endian = rffi.cast(lltype.Signed, little_endian)
    signed = rffi.cast(lltype.Signed, signed)

    result = rbigint()
    negative = False

    for i in range(0, n):
        if little_endian:
            c = intmask(bytes[i])
        else:
            c = intmask(bytes[n - i - 1])
        if i == 0 and signed and c & 0x80:
            negative = True
        if negative:
            c = c ^ 0xFF
        digit = rbigint.fromint(c)

        result = result.lshift(8)
        result = result.add(digit)

    if negative:
        result = result.neg()

    return space.newlong_from_rbigint(result)
Beispiel #5
0
    def test_id(self):
        from pypy.rlib.objectmodel import compute_unique_id
        from pypy.rlib.objectmodel import current_object_addr_as_int

        class A:
            pass

        def fn():
            a1 = A()
            a2 = A()
            return (compute_unique_id(a1), current_object_addr_as_int(a1),
                    compute_unique_id(a2), current_object_addr_as_int(a2))

        res = self.interpret(fn, [])
        x0, x1, x2, x3 = self.ll_unpack_tuple(res, 4)
        assert isinstance(x0, (int, r_longlong))
        assert isinstance(x1, int)
        assert isinstance(x2, (int, r_longlong))
        assert isinstance(x3, int)
        assert x0 != x2
        # the following checks are probably too precise, but work at
        # least on top of llinterp
        if type(self) is TestLLtype:
            assert x1 == intmask(x0)
            assert x3 == intmask(x2)
Beispiel #6
0
def _AsLong(v):
    """
    Get an integer from a bigint object.
    Raises OverflowError if overflow occurs.
    """
    # This version by Tim Peters
    i = len(v.digits) - 1
    sign = v.sign
    if not sign:
        return 0
    x = r_uint(0)
    while i >= 0:
        prev = x
        x = (x << SHIFT) + v.digits[i]
        if (x >> SHIFT) != prev:
            raise OverflowError
        i -= 1

    # Haven't lost any bits, but if the sign bit is set we're in
    # trouble *unless* this is the min negative number.  So,
    # trouble iff sign bit set && (positive || some bit set other
    # than the sign bit).
    if intmask(x) < 0 and (sign > 0 or (x << 1) != 0):
        raise OverflowError
    return intmask(x * sign)
Beispiel #7
0
def _AsLong(v):
    """
    Get an integer from a bigint object.
    Raises OverflowError if overflow occurs.
    """
    # This version by Tim Peters
    i = len(v.digits) - 1
    sign = v.sign
    if not sign:
        return 0
    x = r_uint(0)
    while i >= 0:
        prev = x
        x = (x << SHIFT) + v.digits[i]
        if (x >> SHIFT) != prev:
            raise OverflowError
        i -= 1

    # Haven't lost any bits, but if the sign bit is set we're in
    # trouble *unless* this is the min negative number.  So,
    # trouble iff sign bit set && (positive || some bit set other
    # than the sign bit).
    if intmask(x) < 0 and (sign > 0 or (x << 1) != 0):
        raise OverflowError
    return intmask(x * sign)
Beispiel #8
0
def _PyLong_FromByteArray(space, bytes, n, little_endian, signed):
    little_endian = rffi.cast(lltype.Signed, little_endian)
    signed = rffi.cast(lltype.Signed, signed)

    result = rbigint()
    negative = False

    for i in range(0, n):
        if little_endian:
            c = intmask(bytes[i])
        else:
            c = intmask(bytes[n - i - 1])
        if i == 0 and signed and c & 0x80:
            negative = True
        if negative:
            c = c ^ 0xFF
        digit = rbigint.fromint(c)

        result = result.lshift(8)
        result = result.add(digit)

    if negative:
        result = result.neg()

    return space.newlong_from_rbigint(result)
Beispiel #9
0
    def do_poll(self, space, timeout):
        from pypy.module._multiprocessing.interp_win32 import (
            _PeekNamedPipe, _GetTickCount, _Sleep)
        from pypy.rlib import rwin32
        from pypy.interpreter.error import wrap_windowserror
        bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1,
                                 flavor='raw')
        try:
            if not _PeekNamedPipe(self.handle, rffi.NULL, 0,
                                  lltype.nullptr(rwin32.LPDWORD.TO),
                                  bytes_ptr,
                                  lltype.nullptr(rwin32.LPDWORD.TO)):
                raise wrap_windowserror(space, rwin32.lastWindowsError())
            bytes = bytes_ptr[0]
        finally:
            lltype.free(bytes_ptr, flavor='raw')

        if timeout == 0.0:
            return bytes > 0

        block = timeout < 0
        if not block:
            # XXX does not check for overflow
            deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5)
        else:
            deadline = 0

        _Sleep(0)

        delay = 1
        while True:
            bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1,
                                     flavor='raw')
            try:
                if not _PeekNamedPipe(self.handle, rffi.NULL, 0,
                                      lltype.nullptr(rwin32.LPDWORD.TO),
                                      bytes_ptr,
                                      lltype.nullptr(rwin32.LPDWORD.TO)):
                    raise wrap_windowserror(space, rwin32.lastWindowsError())
                bytes = bytes_ptr[0]
            finally:
                lltype.free(bytes_ptr, flavor='raw')

            if bytes > 0:
                return True

            if not block:
                now = intmask(_GetTickCount())
                if now > deadline:
                    return False
                diff = deadline - now
                if delay > diff:
                    delay = diff
            else:
                delay += 1

            if delay >= 20:
                delay = 20
            _Sleep(delay)
Beispiel #10
0
 def func(no):
     m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
     f_size = os.fstat(no).st_size
     assert intmask(m.file_size()) == f_size == 6
     m.resize(10)
     f_size = os.fstat(no).st_size
     assert intmask(m.file_size()) == f_size == 10
     m.close()
Beispiel #11
0
 def func(no):
     m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
     f_size = os.fstat(no).st_size
     assert intmask(m.file_size()) == f_size == 6
     m.resize(10)
     f_size = os.fstat(no).st_size
     assert intmask(m.file_size()) == f_size == 10
     m.close()
Beispiel #12
0
 def test_truncate(self):
     def f(n):
         m = r_longlong(n) << 20
         return r_uint(m)
     res = self.interp_operations(f, [0x01234567])
     assert res == 0x56700000
     res = self.interp_operations(f, [0x56789ABC])
     assert intmask(res) == intmask(0xABC00000)
Beispiel #13
0
def _ssl_thread_locking_function(mode, n, filename, line):
    n = intmask(n)
    if n < 0 or n >= len(_ssl_locks):
        return

    if intmask(mode) & CRYPTO_LOCK:
        _ssl_locks[n].acquire(True)
    else:
        _ssl_locks[n].release()
Beispiel #14
0
def _ssl_thread_locking_function(mode, n, filename, line):
    n = intmask(n)
    if n < 0 or n >= len(_ssl_locks):
        return

    if intmask(mode) & CRYPTO_LOCK:
        _ssl_locks[n].acquire(True)
    else:
        _ssl_locks[n].release()
Beispiel #15
0
 def ll_hash(t):
     retval = 0x345678
     mult = 1000003
     for i, hash_func in autounrolling_funclist:
         attrname = 'item%d' % i
         item = getattr(t, attrname)
         retval = intmask((retval ^ hash_func(item)) * intmask(mult))
         mult = mult + 82520 + 2*len(items_r)
     return retval
Beispiel #16
0
    def test_truncate(self):
        def f(n):
            m = r_longlong(n) << 20
            return r_uint(m)

        res = self.interp_operations(f, [0x01234567])
        assert res == 0x56700000
        res = self.interp_operations(f, [0x56789ABC])
        assert intmask(res) == intmask(0xABC00000)
Beispiel #17
0
 def test_args_from_long(self):
     BASE = 1 << SHIFT
     assert rbigint.fromlong(0).eq(bigint([0], 0))
     assert rbigint.fromlong(17).eq(bigint([17], 1))
     assert rbigint.fromlong(BASE-1).eq(bigint([intmask(BASE-1)], 1))
     assert rbigint.fromlong(BASE).eq(bigint([0, 1], 1))
     assert rbigint.fromlong(BASE**2).eq(bigint([0, 0, 1], 1))
     assert rbigint.fromlong(-17).eq(bigint([17], -1))
     assert rbigint.fromlong(-(BASE-1)).eq(bigint([intmask(BASE-1)], -1))
     assert rbigint.fromlong(-BASE).eq(bigint([0, 1], -1))
     assert rbigint.fromlong(-(BASE**2)).eq(bigint([0, 0, 1], -1))
Beispiel #18
0
    def do_recv_string(self, space, buflength, maxlength):
        from pypy.module._multiprocessing.interp_win32 import (
            _ReadFile, _PeekNamedPipe, ERROR_BROKEN_PIPE, ERROR_MORE_DATA)
        from pypy.rlib import rwin32
        from pypy.interpreter.error import wrap_windowserror

        read_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO,
                                 1,
                                 flavor='raw')
        left_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO,
                                 1,
                                 flavor='raw')
        try:
            result = _ReadFile(self.handle, self.buffer,
                               min(self.BUFFER_SIZE, buflength), read_ptr,
                               rffi.NULL)
            if result:
                return intmask(read_ptr[0]), lltype.nullptr(rffi.CCHARP.TO)

            err = rwin32.GetLastError()
            if err == ERROR_BROKEN_PIPE:
                raise OperationError(space.w_EOFError, space.w_None)
            elif err != ERROR_MORE_DATA:
                raise wrap_windowserror(space, WindowsError(err, "_ReadFile"))

            # More data...
            if not _PeekNamedPipe(self.handle, rffi.NULL, 0,
                                  lltype.nullptr(rwin32.LPDWORD.TO),
                                  lltype.nullptr(rwin32.LPDWORD.TO), left_ptr):
                raise wrap_windowserror(space, rwin32.lastWindowsError())

            length = intmask(read_ptr[0] + left_ptr[0])
            if length > maxlength:  # bad message, close connection
                self.flags &= ~READABLE
                if self.flags == 0:
                    self.close()
                raise OperationError(space.w_IOError,
                                     space.wrap("bad message length"))

            newbuf = lltype.malloc(rffi.CCHARP.TO, length + 1, flavor='raw')
            for i in range(read_ptr[0]):
                newbuf[i] = self.buffer[i]

            result = _ReadFile(self.handle, rffi.ptradd(newbuf, read_ptr[0]),
                               left_ptr[0], read_ptr, rffi.NULL)
            if not result:
                rffi.free_charp(newbuf)
                raise wrap_windowserror(space, rwin32.lastWindowsError())

            assert read_ptr[0] == left_ptr[0]
            return length, newbuf
        finally:
            lltype.free(read_ptr, flavor='raw')
            lltype.free(left_ptr, flavor='raw')
Beispiel #19
0
    def do_recv_string(self, space, buflength, maxlength):
        from pypy.module._multiprocessing.interp_win32 import (
            _ReadFile, _PeekNamedPipe, ERROR_BROKEN_PIPE, ERROR_MORE_DATA)
        from pypy.rlib import rwin32
        from pypy.interpreter.error import wrap_windowserror

        read_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1,
                                 flavor='raw')
        left_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1,
                                 flavor='raw')
        try:
            result = _ReadFile(self.handle,
                               self.buffer, min(self.BUFFER_SIZE, buflength),
                               read_ptr, rffi.NULL)
            if result:
                return intmask(read_ptr[0]), lltype.nullptr(rffi.CCHARP.TO)

            err = rwin32.GetLastError()
            if err == ERROR_BROKEN_PIPE:
                raise OperationError(space.w_EOFError, space.w_None)
            elif err != ERROR_MORE_DATA:
                raise wrap_windowserror(space, WindowsError(err, "_ReadFile"))

            # More data...
            if not _PeekNamedPipe(self.handle, rffi.NULL, 0,
                                  lltype.nullptr(rwin32.LPDWORD.TO),
                                  lltype.nullptr(rwin32.LPDWORD.TO),
                                  left_ptr):
                raise wrap_windowserror(space, rwin32.lastWindowsError())

            length = intmask(read_ptr[0] + left_ptr[0])
            if length > maxlength: # bad message, close connection
                self.flags &= ~READABLE
                if self.flags == 0:
                    self.close()
                raise OperationError(space.w_IOError, space.wrap(
                    "bad message length"))

            newbuf = lltype.malloc(rffi.CCHARP.TO, length + 1, flavor='raw')
            for i in range(read_ptr[0]):
                newbuf[i] = self.buffer[i]

            result = _ReadFile(self.handle,
                               rffi.ptradd(newbuf, read_ptr[0]), left_ptr[0],
                               read_ptr, rffi.NULL)
            if not result:
                rffi.free_charp(newbuf)
                raise wrap_windowserror(space, rwin32.lastWindowsError())

            assert read_ptr[0] == left_ptr[0]
            return length, newbuf
        finally:
            lltype.free(read_ptr, flavor='raw')
            lltype.free(left_ptr, flavor='raw')
Beispiel #20
0
 def test_args_from_int(self):
     BASE = 1 << SHIFT
     assert rbigint.fromrarith_int(0).eq(rbigint([0], 0))
     assert rbigint.fromrarith_int(17).eq(rbigint([17], 1))
     assert rbigint.fromrarith_int(BASE-1).eq(rbigint([intmask(BASE-1)], 1))
     assert rbigint.fromrarith_int(BASE).eq(rbigint([0, 1], 1))
     assert rbigint.fromrarith_int(BASE**2).eq(rbigint([0, 0, 1], 1))
     assert rbigint.fromrarith_int(-17).eq(rbigint([17], -1))
     assert rbigint.fromrarith_int(-(BASE-1)).eq(rbigint([intmask(BASE-1)], -1))
     assert rbigint.fromrarith_int(-BASE).eq(rbigint([0, 1], -1))
     assert rbigint.fromrarith_int(-(BASE**2)).eq(rbigint([0, 0, 1], -1))
Beispiel #21
0
 def hash(self, storage):
     storage = self.unerase(storage)
     length = len(storage)
     if length == 0:
         return -1
     x = ord(storage[0]) << 7
     i = 0
     while i < length:
         x = intmask((1000003 * x) ^ ord(storage[i]))
         i += 1
     x ^= length
     return intmask(x)
Beispiel #22
0
 def hash(self, storage):
     storage = self.unerase(storage)
     length = len(storage)
     if length == 0:
         return -1
     x = ord(storage[0]) << 7
     i = 0
     while i < length:
         x = intmask((1000003 * x) ^ ord(storage[i]))
         i += 1
     x ^= length
     return intmask(x)
Beispiel #23
0
def make_struct_passwd(space, pw):
    w_passwd_struct = space.getattr(space.getbuiltinmodule('pwd'),
                                    space.wrap('struct_passwd'))
    w_tuple = space.newtuple([
        space.wrap(rffi.charp2str(pw.c_pw_name)),
        space.wrap(rffi.charp2str(pw.c_pw_passwd)),
        space.wrap(intmask(pw.c_pw_uid)),
        space.wrap(intmask(pw.c_pw_gid)),
        space.wrap(rffi.charp2str(pw.c_pw_gecos)),
        space.wrap(rffi.charp2str(pw.c_pw_dir)),
        space.wrap(rffi.charp2str(pw.c_pw_shell)),
        ])
    return space.call_function(w_passwd_struct, w_tuple)
Beispiel #24
0
def _hash_string(s):
    """The algorithm behind compute_hash() for a string or a unicode."""
    from pypy.rlib.rarithmetic import intmask
    length = len(s)
    if length == 0:
        return -1
    x = ord(s[0]) << 7
    i = 0
    while i < length:
        x = intmask((1000003*x) ^ ord(s[i]))
        i += 1
    x ^= length
    return intmask(x)
Beispiel #25
0
 def __init__(self, name, argtypes, restype, funcsym, flags=FUNCFLAG_CDECL, keepalive=None):
     # initialize each one of pointers with null
     AbstractFuncPtr.__init__(self, name, argtypes, restype, flags)
     self.keepalive = keepalive
     self.funcsym = funcsym
     self.argnum = len(self.argtypes)
     self.pushed_args = 0
     self.ll_args = lltype.malloc(rffi.VOIDPP.TO, self.argnum, flavor="raw")
     for i in range(self.argnum):
         # space for each argument
         self.ll_args[i] = lltype.malloc(rffi.VOIDP.TO, intmask(argtypes[i].c_size), flavor="raw")
     if restype != ffi_type_void:
         self.ll_result = lltype.malloc(rffi.VOIDP.TO, intmask(restype.c_size), flavor="raw")
Beispiel #26
0
def _hash_string(s):
    """The algorithm behind compute_hash() for a string or a unicode."""
    from pypy.rlib.rarithmetic import intmask
    length = len(s)
    if length == 0:
        return -1
    x = ord(s[0]) << 7
    i = 0
    while i < length:
        x = intmask((1000003*x) ^ ord(s[i]))
        i += 1
    x ^= length
    return intmask(x)
Beispiel #27
0
def _int_binary_operations():
    minint = -sys.maxint-1
    # Test cases.  Note that for each operation there should be at least
    # one case in which the two input arguments are equal.
    for opnum, testcases in [
        (rop.INT_ADD, [(10, -2, 8),
                       (-60, -60, -120)]),
        (rop.INT_SUB, [(10, -2, 12),
                       (133, 133, 0)]),
        (rop.INT_MUL, [(-6, -3, 18),
                       (15, 15, 225)]),
        (rop.INT_FLOORDIV, [(110, 3, 36),
                            (-110, 3, -36),
                            (110, -3, -36),
                            (-110, -3, 36),
                            (-110, -1, 110),
                            (minint, 1, minint),
                            (-87, -87, 1)]),
        (rop.INT_MOD, [(11, 3, 2),
                       (-11, 3, -2),
                       (11, -3, 2),
                       (-11, -3, -2),
                       (-87, -87, 0)]),
        (rop.INT_AND, [(0xFF00, 0x0FF0, 0x0F00),
                       (-111, -111, -111)]),
        (rop.INT_OR, [(0xFF00, 0x0FF0, 0xFFF0),
                      (-111, -111, -111)]),
        (rop.INT_XOR, [(0xFF00, 0x0FF0, 0xF0F0),
                       (-111, -111, 0)]),
        (rop.INT_LSHIFT, [(10, 4, 10<<4),
                          (-5, 2, -20),
                          (-5, 0, -5),
                          (3, 3, 24)]),
        (rop.INT_RSHIFT, [(-17, 2, -5),
                          (19, 1, 9),
                          (3, 3, 0)]),
        (rop.UINT_RSHIFT, [(-1, 4, intmask(r_uint(-1) >> r_uint(4))),
                           ( 1, 4, intmask(r_uint(1) >> r_uint(4))),
                           ( 3, 3, 0)]),
        (rop.UINT_FLOORDIV, [(4, 3, intmask(r_uint(4) / r_uint(3))),
                             (1, -1, intmask(r_uint(1) / r_uint(-1))),
                             (110, 3, 36),
                             (-110, 3, intmask(r_uint(-110) / r_uint(3))),
                             (110, -3, intmask(r_uint(110) / r_uint(-3))),
                             (-110, -3, intmask(r_uint(-110) / r_uint(-3))),
                             (-110, -1, intmask(r_uint(-110) / r_uint(-1))),
                             (minint, 1, intmask(r_uint(minint) / r_uint(1))),
                             (-87, -87, intmask(r_uint(-87) / r_uint(-87)))])
        ]:
        for x, y, z in testcases:
            yield opnum, [x, y], z
Beispiel #28
0
def digits_for_most_neg_long(l):
    # This helper only works if 'l' is the most negative integer of its
    # type, which in base 2 looks like: 1000000..0000
    digits = []
    while (intmask(l) & MASK) == 0:
        digits.append(0)
        l = l >> SHIFT
    # now 'l' looks like: ...111100000
    # turn it into:       ...000100000
    # to drop the extra unwanted 1's introduced by the signed right shift
    l = -intmask(l)
    assert l >= 0
    digits.append(l)
    return digits
Beispiel #29
0
 def ll_stat_result(stat0, stat1, stat2, stat3, stat4,
                    stat5, stat6, stat7, stat8, stat9):
     tup = lltype.malloc(STAT_RESULT)
     tup.item0 = intmask(stat0)
     tup.item1 = intmask(stat1)
     tup.item2 = intmask(stat2)
     tup.item3 = intmask(stat3)
     tup.item4 = intmask(stat4)
     tup.item5 = intmask(stat5)
     tup.item6 = intmask(stat6)
     tup.item7 = intmask(stat7)
     tup.item8 = intmask(stat8)
     tup.item9 = intmask(stat9)
     return tup
Beispiel #30
0
 def toint(self):
     """
     Get an integer from a bigint object.
     Raises OverflowError if overflow occurs.
     """
     x = self._touint_helper()
     # Haven't lost any bits, but if the sign bit is set we're in
     # trouble *unless* this is the min negative number.  So,
     # trouble iff sign bit set && (positive || some bit set other
     # than the sign bit).
     sign = self.sign
     if intmask(x) < 0 and (sign > 0 or (x << 1) != 0):
         raise OverflowError
     return intmask(x * sign)
Beispiel #31
0
def digits_for_most_neg_long(l):
    # This helper only works if 'l' is the most negative integer of its
    # type, which in base 2 looks like: 1000000..0000
    digits = []
    while (intmask(l) & MASK) == 0:
        digits.append(0)
        l = l >> SHIFT
    # now 'l' looks like: ...111100000
    # turn it into:       ...000100000
    # to drop the extra unwanted 1's introduced by the signed right shift
    l = -intmask(l)
    assert l >= 0
    digits.append(l)
    return digits
Beispiel #32
0
    def compute_additional_info(self):
        additional_info = GlobalRopeInfo()
        charbitmask = 0
        partial_hash = 0
        for c in self.u:
            ordc = ord(c)
            charbitmask |= intmask(1 << (ordc & 0x1F))
            partial_hash = (1000003*partial_hash) + ordc
        partial_hash = intmask(partial_hash)
        partial_hash |= HIGHEST_BIT_SET

        additional_info.charbitmask = intmask(charbitmask)
        additional_info.hash = partial_hash
        self._additional_info = additional_info
        return additional_info
Beispiel #33
0
    def compute_additional_info(self):
        additional_info = GlobalRopeInfo()
        charbitmask = 0
        partial_hash = 0
        for c in self.u:
            ordc = ord(c)
            charbitmask |= intmask(1 << (ordc & 0x1F))
            partial_hash = (1000003 * partial_hash) + ordc
        partial_hash = intmask(partial_hash)
        partial_hash |= HIGHEST_BIT_SET

        additional_info.charbitmask = intmask(charbitmask)
        additional_info.hash = partial_hash
        self._additional_info = additional_info
        return additional_info
Beispiel #34
0
def makeipaddr(name, result=None):
    # Convert a string specifying a host name or one of a few symbolic
    # names to an IPAddress instance.  This usually calls getaddrinfo()
    # to do the work; the names "" and "<broadcast>" are special.
    # If 'result' is specified it must be a prebuilt INETAddress or
    # INET6Address that is filled; otherwise a new INETXAddress is returned.
    if result is None:
        family = AF_UNSPEC
    else:
        family = result.family

    if len(name) == 0:
        info = getaddrinfo(None, "0",
                           family=family,
                           socktype=SOCK_DGRAM,   # dummy
                           flags=AI_PASSIVE,
                           address_to_fill=result)
        if len(info) > 1:
            raise RSocketError("wildcard resolved to "
                               "multiple addresses")
        return info[0][4]

    # IPv4 also supports the special name "<broadcast>".
    if name == '<broadcast>':
        return makeipv4addr(intmask(INADDR_BROADCAST), result)

    # "dd.dd.dd.dd" format.
    digits = name.split('.')
    if len(digits) == 4:
        try:
            d0 = int(digits[0])
            d1 = int(digits[1])
            d2 = int(digits[2])
            d3 = int(digits[3])
        except ValueError:
            pass
        else:
            if (0 <= d0 <= 255 and
                0 <= d1 <= 255 and
                0 <= d2 <= 255 and
                0 <= d3 <= 255):
                return makeipv4addr(intmask(htonl(
                    (intmask(d0 << 24)) | (d1 << 16) | (d2 << 8) | (d3 << 0))),
                                    result)

    # generic host name to IP conversion
    info = getaddrinfo(name, None, family=family, address_to_fill=result)
    return info[0][4]
Beispiel #35
0
def makeipaddr(name, result=None):
    # Convert a string specifying a host name or one of a few symbolic
    # names to an IPAddress instance.  This usually calls getaddrinfo()
    # to do the work; the names "" and "<broadcast>" are special.
    # If 'result' is specified it must be a prebuilt INETAddress or
    # INET6Address that is filled; otherwise a new INETXAddress is returned.
    if result is None:
        family = AF_UNSPEC
    else:
        family = result.family

    if len(name) == 0:
        info = getaddrinfo(
            None,
            "0",
            family=family,
            socktype=SOCK_DGRAM,  # dummy
            flags=AI_PASSIVE,
            address_to_fill=result)
        if len(info) > 1:
            raise RSocketError("wildcard resolved to " "multiple addresses")
        return info[0][4]

    # IPv4 also supports the special name "<broadcast>".
    if name == '<broadcast>':
        return makeipv4addr(intmask(INADDR_BROADCAST), result)

    # "dd.dd.dd.dd" format.
    digits = name.split('.')
    if len(digits) == 4:
        try:
            d0 = int(digits[0])
            d1 = int(digits[1])
            d2 = int(digits[2])
            d3 = int(digits[3])
        except ValueError:
            pass
        else:
            if (0 <= d0 <= 255 and 0 <= d1 <= 255 and 0 <= d2 <= 255
                    and 0 <= d3 <= 255):
                return makeipv4addr(
                    intmask(
                        htonl((intmask(d0 << 24)) | (d1 << 16) | (d2 << 8)
                              | (d3 << 0))), result)

    # generic host name to IP conversion
    info = getaddrinfo(name, None, family=family, address_to_fill=result)
    return info[0][4]
Beispiel #36
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
Beispiel #37
0
    def lookup_where_with_method_cache(w_self, name):
        space = w_self.space
        assert space.config.objspace.std.withmethodcache
        ec = space.getexecutioncontext()
        version_tag = w_self.version_tag
        if version_tag is None:
            tup = w_self._lookup_where(name)
            return tup
        SHIFT = r_uint.BITS - space.config.objspace.std.methodcachesizeexp
        method_hash = r_uint(intmask(id(version_tag) * hash(name))) >> SHIFT
        cached_version_tag = ec.method_cache_versions[method_hash]
        if cached_version_tag is version_tag:
            cached_name = ec.method_cache_names[method_hash]
            if cached_name is name:
                tup = ec.method_cache_lookup_where[method_hash]
                if space.config.objspace.std.withmethodcachecounter:
                    ec.method_cache_hits[name] = \
                            ec.method_cache_hits.get(name, 0) + 1
#                print "hit", w_self, name
                return tup
        tup = w_self._lookup_where(name)
        ec.method_cache_versions[method_hash] = version_tag
        ec.method_cache_names[method_hash] = name
        ec.method_cache_lookup_where[method_hash] = tup
        if space.config.objspace.std.withmethodcachecounter:
            ec.method_cache_misses[name] = \
                    ec.method_cache_misses.get(name, 0) + 1
#        print "miss", w_self, name
        return tup
Beispiel #38
0
 def _do_call(self, funcsym, ll_args, RESULT):
     # XXX: check len(args)?
     ll_result = lltype.nullptr(rffi.CCHARP.TO)
     if self.restype != types.void:
         ll_result = lltype.malloc(rffi.CCHARP.TO,
                                   intmask(self.restype.c_size),
                                   flavor='raw')
     ffires = c_ffi_call(self.ll_cif,
                         self.funcsym,
                         rffi.cast(rffi.VOIDP, ll_result),
                         rffi.cast(rffi.VOIDPP, ll_args))
     if RESULT is not lltype.Void:
         TP = lltype.Ptr(rffi.CArray(RESULT))
         buf = rffi.cast(TP, ll_result)
         if types.is_struct(self.restype):
             assert RESULT == rffi.SIGNED
             # for structs, we directly return the buffer and transfer the
             # ownership
             res = rffi.cast(RESULT, buf)
         else:
             res = buf[0]
     else:
         res = None
     self._free_buffers(ll_result, ll_args)
     clibffi.check_fficall_result(ffires, self.flags)
     return res
Beispiel #39
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
Beispiel #40
0
def w_match(space, w_state, w_pattern_codes):
    state = space.interp_w(W_State, w_state)
    pattern_codes = [
        intmask(space.uint_w(code))
        for code in space.unpackiterable(w_pattern_codes)
    ]
    return space.newbool(state.match(pattern_codes))
Beispiel #41
0
def _create_tuple_for_X509_NAME(space, xname):
    entry_count = libssl_X509_NAME_entry_count(xname)
    dn_w = []
    rdn_w = []
    rdn_level = -1
    for index in range(entry_count):
        entry = libssl_X509_NAME_get_entry(xname, index)
        # check to see if we've gotten to a new RDN
        entry_level = intmask(entry[0].c_set)
        if rdn_level >= 0:
            if rdn_level != entry_level:
                # yes, new RDN
                # add old RDN to DN
                dn_w.append(space.newtuple(list(rdn_w)))
                rdn_w = []
        rdn_level = entry_level

        # Now add this attribute to the current RDN
        name = libssl_X509_NAME_ENTRY_get_object(entry)
        value = libssl_X509_NAME_ENTRY_get_data(entry)
        attr = _create_tuple_for_attribute(space, name, value)
        rdn_w.append(attr)

    # Now, there is typically a dangling RDN
    if rdn_w:
        dn_w.append(space.newtuple(list(rdn_w)))
    return space.newtuple(list(dn_w))
Beispiel #42
0
    def _pure_lookup_where_with_method_cache(w_self, name, version_tag):
        space = w_self.space
        SHIFT = r_uint.BITS - space.config.objspace.std.methodcachesizeexp
        version_tag_as_int = current_object_addr_as_int(version_tag)
        # ^^^Note: if the version_tag object is moved by a moving GC, the
        # existing method cache entries won't be found any more; new
        # entries will be created based on the new address.  The
        # assumption is that the version_tag object won't keep moving all
        # the time - so using the fast current_object_addr_as_int() instead
        # of a slower solution like hash() is still a good trade-off.
        hash_name = compute_hash(name)
        method_hash = r_uint(intmask(version_tag_as_int * hash_name)) >> SHIFT
        cached_version_tag = space.method_cache_versions[method_hash]
        if cached_version_tag is version_tag:
            cached_name = space.method_cache_names[method_hash]
            if cached_name is name:
                tup = space.method_cache_lookup_where[method_hash]
                if space.config.objspace.std.withmethodcachecounter:
                    space.method_cache_hits[name] = \
                            space.method_cache_hits.get(name, 0) + 1
#                print "hit", w_self, name
                return tup
        tup = w_self._lookup_where_all_typeobjects(name)
        space.method_cache_versions[method_hash] = version_tag
        space.method_cache_names[method_hash] = name
        space.method_cache_lookup_where[method_hash] = tup
        if space.config.objspace.std.withmethodcachecounter:
            space.method_cache_misses[name] = \
                    space.method_cache_misses.get(name, 0) + 1
#        print "miss", w_self, name
        return tup
Beispiel #43
0
    def llimpl_FormatError(code):
        "Return a message corresponding to the given Windows error code."
        buf = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw')

        try:
            msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                   FORMAT_MESSAGE_FROM_SYSTEM,
                                   None,
                                   code,
                                   DEFAULT_LANGUAGE,
                                   rffi.cast(rffi.VOIDP, buf),
                                   0, None)

            if msglen <= 2 or msglen > sys.maxint:
                return fake_FormatError(code)

            # FormatMessage always appends \r\n.
            buflen = intmask(msglen - 2)
            assert buflen > 0

            result = rffi.charpsize2str(buf[0], buflen)
            LocalFree(buf[0])
        finally:
            lltype.free(buf, flavor='raw')

        return result
Beispiel #44
0
    def test_promote_index_in_virtualizable_list(self):
        jitdriver = JitDriver(greens = [], reds = ['frame', 'n'],
                              virtualizables = ['frame'])
        class Frame(object):
            _virtualizable2_ = ['stackpos', 'stack[*]']

        def f(n):
            frame = Frame()
            frame.stack = [42, 0, 0]
            frame.stackpos = 1
            while n > 0:
                jitdriver.can_enter_jit(frame=frame, n=n)
                jitdriver.jit_merge_point(frame=frame, n=n)
                popped = frame.stack[frame.stackpos]
                frame.stackpos -= 1
                to_push = intmask(popped * 3)
                frame.stack[frame.stackpos] = to_push
                frame.stackpos += 1
                n -= 1
            return frame.stack[0]

        res = self.meta_interp(f, [70], listops=True)
        assert res == intmask(42 ** 70)
        self.check_loops(int_add=0,
                         int_sub=1)   # for 'n -= 1' only
Beispiel #45
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
Beispiel #46
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 #47
0
 def read_struct(self, offset, fmt_str, read_null=False):
     if offset == 0 and not read_null:
         return []
     size = calcsize(fmt_str)
     os.lseek(self.os_file, intmask(offset), os.SEEK_SET)
     struct_bin = os.read(self.os_file, size)
     return unpack(fmt_str, struct_bin)
Beispiel #48
0
def default_identity_hash(space, w_obj):
    fn = get_default_hash_function(w_obj.__class__)
    if fn is None:
        typename = space.type(w_obj).getname(space, '?')
        msg = "%s objects have no default hash" % (typename, )
        raise OperationError(space.w_TypeError, space.wrap(msg))
    return space.wrap(intmask(fn(w_obj)))
Beispiel #49
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
Beispiel #50
0
 def __new__(cls, void_p):
     if isinstance(void_p, (int, long)):
         void_p = ctypes.c_void_p(void_p)
     self = long.__new__(cls, void_p.value)
     self.void_p = void_p
     self.intval = intmask(void_p.value)
     return self
Beispiel #51
0
def offset2int(offset):
    intoffset = intmask(offset)
    if intoffset != offset:
        raise StreamError("seek() from a non-seekable source:"
                          " this would read and discard more"
                          " than sys.maxint bytes")
    return intoffset
Beispiel #52
0
 def _push_arg(self, value, ll_args, i):
     # XXX: check the type is not translated?
     argtype = self.argtypes[i]
     c_size = intmask(argtype.c_size)
     ll_buf = lltype.malloc(rffi.CCHARP.TO, c_size, flavor='raw')
     push_arg_as_ffiptr(argtype, value, ll_buf)
     ll_args[i] = ll_buf
Beispiel #53
0
 def bound_reached(cell, *args):
     # bound reached, but we do a last check: if it is the first
     # time we reach the bound, or if another loop or bridge was
     # compiled since the last time we reached it, then decrease
     # the counter by a few percents instead.  It should avoid
     # sudden bursts of JIT-compilation, and also corner cases
     # where we suddenly compile more than one loop because all
     # counters reach the bound at the same time, but where
     # compiling all but the first one is pointless.
     curgen = warmrunnerdesc.memory_manager.current_generation
     curgen = chr(intmask(curgen) & 0xFF)  # only use 8 bits
     if we_are_translated() and curgen != cell.extra_delay:
         cell.counter = int(self.THRESHOLD_LIMIT * 0.98)
         cell.extra_delay = curgen
         return
     #
     if not confirm_enter_jit(*args):
         cell.counter = 0
         return
     # start tracing
     from pypy.jit.metainterp.pyjitpl import MetaInterp
     metainterp = MetaInterp(metainterp_sd, jitdriver_sd)
     # set counter to -2, to mean "tracing in effect"
     cell.counter = -2
     try:
         metainterp.compile_and_run_once(jitdriver_sd, *args)
     finally:
         if cell.counter == -2:
             cell.counter = 0
Beispiel #54
0
 def jumpahead(self, space, w_n):
     if space.is_true(space.isinstance(w_n, space.w_long)):
         num = space.bigint_w(w_n)
         n = intmask(num.uintmask())
     else:
         n = space.int_w(w_n)
     self._rnd.jumpahead(n)
Beispiel #55
0
def int__SmallLong(space, w_value):
    a = w_value.longlong
    b = intmask(a)
    if b == a:
        return space.newint(b)
    else:
        return w_value