def immutable_unique_id(self, space): if self.user_overridden_class: return None from rpython.rlib.longlong2float import float2longlong from pypy.objspace.std.util import IDTAG_COMPLEX as tag real = space.float_w(space.getattr(self, space.wrap("real"))) imag = space.float_w(space.getattr(self, space.wrap("imag"))) real_b = rbigint.fromrarith_int(float2longlong(real)) imag_b = rbigint.fromrarith_int(r_ulonglong(float2longlong(imag))) val = real_b.lshift(64).or_(imag_b).lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(val)
def test_args_from_int(self): BASE = 1 << 31 # Can't can't shift here. Shift might be from longlonglong MAX = int(BASE-1) assert rbigint.fromrarith_int(0).eq(bigint([0], 0)) assert rbigint.fromrarith_int(17).eq(bigint([17], 1)) assert rbigint.fromrarith_int(MAX).eq(bigint([MAX], 1)) # No longer true. """assert rbigint.fromrarith_int(r_longlong(BASE)).eq(bigint([0, 1], 1)) assert rbigint.fromrarith_int(r_longlong(BASE**2)).eq( bigint([0, 0, 1], 1))""" assert rbigint.fromrarith_int(-17).eq(bigint([17], -1)) assert rbigint.fromrarith_int(-MAX).eq(bigint([MAX], -1)) """assert rbigint.fromrarith_int(-MAX-1).eq(bigint([0, 1], -1))
def get_stat_ns_as_bigint(st, name): """'name' is one of the strings "atime", "mtime" or "ctime". Returns a bigint that represents the number of nanoseconds stored inside the RPython-level os.stat_result 'st'. Note that when running untranslated, the os.stat_result type is from Python 2.7, which doesn't store more precision than a float anyway. You will only get more after translation. """ from rpython.rlib.rbigint import rbigint if not we_are_translated(): as_float = getattr(st, "st_" + name) return rbigint.fromfloat(as_float * 1e9) if name == "atime": i, j = 7, -3 elif name == "mtime": i, j = 8, -2 elif name == "ctime": i, j = 9, -1 else: raise AssertionError(name) sec = st[i] nsec = st[j] result = rbigint.fromrarith_int(sec).int_mul(1000000000) result = result.int_add(nsec) return result
def file_position(args): if len(args) == 1: w_port = args[0] assert isinstance(w_port, values.W_Port) told = w_port.tell() assert told >= 0 return values.W_Integer.frombigint( rbigint.fromrarith_int(told)) elif len(args) == 2: w_port = args[0] assert isinstance(w_port, values.W_Port) w_offset = args[1] if isinstance(w_offset, values.W_Fixnum): assert w_offset.value >= 0 w_port.seek(w_offset.value) elif isinstance(w_offset, values.W_Bignum): v = w_offset.value.tolonglong() w_port.seek(v) elif w_offset is values.eof_object: w_port.seek(0, end=True) else: assert 0 return values.w_void raise SchemeException( "printf expected one or two arguments, got %s" % len(args))
def newint_or_bigint(self, someinteger): if -sys.maxint <= someinteger <= sys.maxint: # The smallest int -sys.maxint - 1 has to be a Bignum, # because parsing gives a Bignum in that case return self.newint(intmask(someinteger)) else: return self.newbigint_fromrbigint(rbigint.fromrarith_int(someinteger))
def newint_or_bigint_fromunsigned(self, someunsigned): #XXX somehow combine with above if 0 <= someunsigned <= sys.maxint: return self.newint(intmask(someunsigned)) else: return self.newbigint_fromrbigint( rbigint.fromrarith_int(someunsigned))
def newint_or_bigint_fromunsigned(self, someunsigned): # XXX somehow combine with above if 0 <= someunsigned <= sys.maxint: return self.newint(intmask(someunsigned)) else: return self.newbigint_fromrbigint( rbigint.fromrarith_int(someunsigned))
def immutable_unique_id(self, space): if self.user_overridden_class: return None from rpython.rlib.longlong2float import float2longlong from pypy.objspace.std.util import IDTAG_FLOAT as tag val = float2longlong(space.float_w(self)) b = rbigint.fromrarith_int(val) b = b.lshift(3).int_or_(tag) return space.newlong_from_rbigint(b)
def unmarshal_int64(space, u, tc): from rpython.rlib.rbigint import rbigint # no longer generated, but we still support unmarshalling lo = u.get_int() # get the first 32 bits hi = u.get_int() # get the next 32 bits if LONG_BIT >= 64: x = (hi << 32) | (lo & (2**32 - 1)) # result fits in an int return space.newint(x) else: x = (r_longlong(hi) << 32) | r_longlong(r_uint(lo)) # get a r_longlong result = rbigint.fromrarith_int(x) return space.newlong_from_rbigint(result)
def file_size(obj): if not is_path_string(obj): raise SchemeException("file-size: expected path string") path = extract_path(obj) try: size = os.path.getsize(path) except OSError: raise SchemeException("file-size: file %s does not exists" % path) intsize = intmask(size) if intsize == size: return values.W_Fixnum(intsize) return values.W_Bignum(rbigint.fromrarith_int(size))
def test_args_from_uint(self): BASE = 1 << SHIFT assert rbigint.fromrarith_int(r_uint(0)).eq(bigint([0], 0)) assert rbigint.fromrarith_int(r_uint(17)).eq(bigint([17], 1)) assert rbigint.fromrarith_int(r_uint(BASE-1)).eq(bigint([intmask(BASE-1)], 1)) assert rbigint.fromrarith_int(r_uint(BASE)).eq(bigint([0, 1], 1)) #assert rbigint.fromrarith_int(r_uint(BASE**2)).eq(bigint([0], 0)) assert rbigint.fromrarith_int(r_uint(sys.maxint)).eq( rbigint.fromint(sys.maxint)) assert rbigint.fromrarith_int(r_uint(sys.maxint+1)).eq( rbigint.fromlong(sys.maxint+1)) assert rbigint.fromrarith_int(r_uint(2*sys.maxint+1)).eq( rbigint.fromlong(2*sys.maxint+1))
def fn(i): n = rbigint.fromrarith_int(values[i]) return n.str()
def fromrarith_int(i): return W_LongObject(rbigint.fromrarith_int(i))
def asbigint(self): return rbigint.fromrarith_int(self.longlong)