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))
def test_parse_digit_string(self): from pypy.rlib.rbigint import parse_digit_string class Parser: def __init__(self, base, sign, digits): self.base = base self.sign = sign self.next_digit = iter(digits + [-1]).next x = parse_digit_string(Parser(10, 1, [6])) assert x.eq(rbigint.fromint(6)) x = parse_digit_string(Parser(10, 1, [6, 2, 3])) assert x.eq(rbigint.fromint(623)) x = parse_digit_string(Parser(10, -1, [6, 2, 3])) assert x.eq(rbigint.fromint(-623)) x = parse_digit_string(Parser(16, 1, [0xA, 0x4, 0xF])) assert x.eq(rbigint.fromint(0xA4F)) num = 0 for i in range(36): x = parse_digit_string(Parser(36, 1, range(i))) assert x.eq(rbigint.fromlong(num)) num = num * 36 + i x = parse_digit_string(Parser(16, -1, range(15,-1,-1)*99)) assert x.eq(rbigint.fromlong(long('-0x' + 'FEDCBA9876543210'*99, 16))) assert x.tobool() is True x = parse_digit_string(Parser(7, 1, [0, 0, 0])) assert x.tobool() is False x = parse_digit_string(Parser(7, -1, [0, 0, 0])) assert x.tobool() is False
def test_order(self): f6 = rbigint.fromint(6) f7 = rbigint.fromint(7) assert (f6.lt(f6), f6.lt(f7), f7.lt(f6)) == (0,1,0) assert (f6.le(f6), f6.le(f7), f7.le(f6)) == (1,1,0) assert (f6.gt(f6), f6.gt(f7), f7.gt(f6)) == (0,0,1) assert (f6.ge(f6), f6.ge(f7), f7.ge(f6)) == (1,0,1)
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))
def test_truediv(self): for op1 in [-12, -2, -1, 1, 2, 50]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.truediv(rl_op2) r2 = op1 / op2 assert r1 == r2
def test_mod(self): for op1 in [-50, -12, -2, -1, 1, 2, 50, 52]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.mod(rl_op2) r2 = op1 % op2 assert r1.tolong() == r2
def test_pow(self): for op1 in [-50, -12, -2, -1, 1, 2, 50, 52]: for op2 in [0, 1, 2, 8, 9, 10, 11]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.pow(rl_op2) r2 = op1 ** op2 assert r1.tolong() == r2
def test_truediv(self): for op1 in [-12, -2, -1, 1, 2, 50]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.truediv(rl_op2) r2 = op1 / op2 assert r1 == r2
def test_floordiv(self): for op1 in [-12, -2, -1, 1, 2, 50]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.floordiv(rl_op2) r2 = op1 // op2 assert r1.tolong() == r2
def test_mod(self): for op1 in [-50, -12, -2, -1, 1, 2, 50, 52]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.mod(rl_op2) r2 = op1 % op2 assert r1.tolong() == r2
def test_floordiv(self): for op1 in [-12, -2, -1, 1, 2, 50]: for op2 in [-4, -2, -1, 1, 2, 8]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.floordiv(rl_op2) r2 = op1 // op2 assert r1.tolong() == r2
def test_pow(self): for op1 in [-50, -12, -2, -1, 1, 2, 50, 52]: for op2 in [0, 1, 2, 8, 9, 10, 11]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) r1 = rl_op1.pow(rl_op2) r2 = op1 ** op2 assert r1.tolong() == r2
def test_simple(self): for op1 in [-2, -1, 0, 1, 2, 50]: for op2 in [-2, -1, 0, 1, 2, 50]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) for op in "add sub mul".split(): r1 = getattr(rl_op1, op)(rl_op2) r2 = getattr(operator, op)(op1, op2) assert r1.tolong() == r2
def test_add(self): x = rbigint.fromint(-2147483647) y = rbigint.fromint(-1) z = rbigint.fromint(-2147483648) def test(): return x.add(y).eq(z) assert test() res = interpret(test, []) assert res
def test_simple(self): for op1 in [-2, -1, 0, 1, 2, 50]: for op2 in [-2, -1, 0, 1, 2, 50]: rl_op1 = rbigint.fromint(op1) rl_op2 = rbigint.fromint(op2) for op in "add sub mul".split(): r1 = getattr(rl_op1, op)(rl_op2) r2 = getattr(operator, op)(op1, op2) assert r1.tolong() == r2
def test_bigint_w(self): space = self.space fromlong = lobj.W_LongObject.fromlong assert isinstance(space.bigint_w(fromlong(42)), rbigint) assert space.bigint_w(fromlong(42)).eq(rbigint.fromint(42)) assert space.bigint_w(fromlong(-1)).eq(rbigint.fromint(-1)) w_obj = space.wrap("hello world") space.raises_w(space.w_TypeError, space.bigint_w, w_obj) w_obj = space.wrap(123.456) space.raises_w(space.w_TypeError, space.bigint_w, w_obj)
def test_bigint_w(self): space = self.space fromlong = lobj.W_LongObject.fromlong assert isinstance(space.bigint_w(fromlong(42)), rbigint) assert space.bigint_w(fromlong(42)).eq(rbigint.fromint(42)) assert space.bigint_w(fromlong(-1)).eq(rbigint.fromint(-1)) w_obj = space.wrap("hello world") space.raises_w(space.w_TypeError, space.bigint_w, w_obj) w_obj = space.wrap(123.456) space.raises_w(space.w_TypeError, space.bigint_w, w_obj)
def test_bigint_w(): space = CPyObjSpace() r1 = space.bigint_w(space.newlong(42)) assert isinstance(r1, rbigint) assert r1.eq(rbigint.fromint(42)) # cpython digit size assert space.bigint_w(space.newlong(2**8)).eq(rbigint.fromint(2**8)) # rpython digit size assert space.bigint_w(space.newlong(2**15)).eq(rbigint.fromint(2**15)) # and negative numbers assert space.bigint_w(space.newlong(-1)).eq(rbigint.fromint(-1)) assert space.bigint_w(space.newlong(-2**8)).eq(rbigint.fromint(-2**8)) assert space.bigint_w(space.newlong(-2**15)).eq(rbigint.fromlong(-2**15))
def pow__Long_Long_None(space, w_long1, w_long2, w_long3): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise FailedToImplement( space.w_ValueError, space.wrap("long pow() too negative")) return W_LongObject(w_long1.num.pow(w_long2.num, None))
def unmarshal_Long(space, u, tc): # XXX access internals from pypy.rlib.rbigint import rbigint lng = u.get_int() if lng < 0: sign = -1 lng = -lng elif lng > 0: sign = 1 else: sign = 0 if long_bits != 15: SHIFT = 15 result = rbigint([0], 0) for i in range(lng): shift = i * SHIFT result = result.add(rbigint.fromint(u.get_short()).lshift(shift)) if lng and not result.tobool(): raise_exception(space, 'bad marshal data') if sign == -1: result = result.neg() else: digits = [0] * lng for i in range(lng): digit = u.get_int() if digit < 0: raise_exception(space, 'bad marshal data') digits[i] = digit if digits[-1] == 0: raise_exception(space, 'bad marshal data') result = rbigint(digits, sign) w_long = W_LongObject(result) return w_long
def test_hash(self): for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, sys.maxint-3, sys.maxint-2, sys.maxint-1, sys.maxint, ] + [randint(0, sys.maxint) for _ in range(100)]: # hash of machine-sized integers assert rbigint.fromint(i).hash() == i # hash of negative machine-sized integers assert rbigint.fromint(-i-1).hash() == -i-1 # for i in range(200): # hash of large integers: should be equal to the hash of the # integer reduced modulo 2**64-1, to make decimal.py happy x = randint(0, sys.maxint**5) y = x % (2**64-1) assert rbigint.fromlong(x).hash() == rbigint.fromlong(y).hash() assert rbigint.fromlong(-x).hash() == rbigint.fromlong(-y).hash()
def imod( self, other ): if self.bvalue: if other.bvalue: return HybridCell( 0, self.bvalue.mod( other.bvalue ) ) else: o = rbigint.fromint(other.ivalue) v = self.bvalue.mod( o ) return HybridCell( 0, v ) else: if other.bvalue: s = rbigint.fromint(self.ivalue) v = s.mod( other.bvalue ) return HybridCell( 0, v ) else: v = self.ivalue % other.ivalue return HybridCell( v )
def test_conversions(self): for v in (0, 1, -1, sys.maxint, -sys.maxint-1): assert rbigint.fromlong(long(v)).tolong() == long(v) l = rbigint.fromint(v) assert l.toint() == v if v >= 0: u = l.touint() assert u == v assert type(u) is r_uint else: py.test.raises(ValueError, l.touint) toobig_lv1 = rbigint.fromlong(sys.maxint+1) assert toobig_lv1.tolong() == sys.maxint+1 toobig_lv2 = rbigint.fromlong(sys.maxint+2) assert toobig_lv2.tolong() == sys.maxint+2 toobig_lv3 = rbigint.fromlong(-sys.maxint-2) assert toobig_lv3.tolong() == -sys.maxint-2 for lv in (toobig_lv1, toobig_lv2, toobig_lv3): py.test.raises(OverflowError, lv.toint) lmaxuint = rbigint.fromlong(2*sys.maxint+1) toobig_lv4 = rbigint.fromlong(2*sys.maxint+2) u = lmaxuint.touint() assert u == 2*sys.maxint+1 py.test.raises(ValueError, toobig_lv3.touint) py.test.raises(OverflowError, toobig_lv4.touint)
def pow__Long_Long_None(space, w_long1, w_long2, w_long3): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise FailedToImplementArgs( space.w_ValueError, space.wrap("long pow() too negative")) return W_LongObject(w_long1.num.pow(w_long2.num, None))
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)
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)
def marshal_w__Long(space, w_long, m): from pypy.rlib.rbigint import rbigint m.start(TYPE_LONG) # XXX access internals if long_bits != 15: SHIFT = 15 MASK = (1 << SHIFT) - 1 BIGMASK = rbigint.fromint(MASK) num = w_long.num sign = num.sign num = num.abs() ints = [] while num.tobool(): next = num.and_(BIGMASK).toint() ints.append(next) num = num.rshift(SHIFT) m.put_int(len(ints) * sign) for i in ints: m.put_short(i) return lng = len(w_long.num.digits) if w_long.num.sign < 0: m.put_int(-lng) else: m.put_int(lng) for digit in w_long.num.digits: m.put_short(digit)
def test_conversions(self): for v in (0, 1, -1, sys.maxint, -sys.maxint-1): assert rbigint.fromlong(long(v)).tolong() == long(v) l = rbigint.fromint(v) assert l.toint() == v if v >= 0: u = l.touint() assert u == v assert type(u) is r_uint else: py.test.raises(ValueError, l.touint) toobig_lv1 = rbigint.fromlong(sys.maxint+1) assert toobig_lv1.tolong() == sys.maxint+1 toobig_lv2 = rbigint.fromlong(sys.maxint+2) assert toobig_lv2.tolong() == sys.maxint+2 toobig_lv3 = rbigint.fromlong(-sys.maxint-2) assert toobig_lv3.tolong() == -sys.maxint-2 for lv in (toobig_lv1, toobig_lv2, toobig_lv3): py.test.raises(OverflowError, lv.toint) lmaxuint = rbigint.fromlong(2*sys.maxint+1) toobig_lv4 = rbigint.fromlong(2*sys.maxint+2) u = lmaxuint.touint() assert u == 2*sys.maxint+1 py.test.raises(ValueError, toobig_lv3.touint) py.test.raises(OverflowError, toobig_lv4.touint)
def immutable_unique_id(self, space): if self.user_overridden_class: return None from pypy.objspace.std.model import IDTAG_LONG as tag b = space.bigint_w(self) b = b.lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(b)
def marshal_w__Long(space, w_long, m): from pypy.rlib.rbigint import rbigint m.start(TYPE_LONG) # XXX access internals if long_bits != 15: SHIFT = 15 MASK = (1 << SHIFT) - 1 BIGMASK = rbigint.fromint(MASK) num = w_long.num sign = num.sign num = num.abs() ints = [] while num.tobool(): next = num.and_(BIGMASK).toint() ints.append(next) num = num.rshift(SHIFT) m.put_int(len(ints) * sign) for i in ints: m.put_short(i) return lng = len(w_long.num.digits) if w_long.num.sign < 0: m.put_int(-lng) else: m.put_int(lng) for digit in w_long.num.digits: m.put_short(digit)
def unique_id(self, space): if self.user_overridden_class: return W_Object.unique_id(self, space) from pypy.objspace.std.model import IDTAG_INT as tag b = space.bigint_w(self) b = b.lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(b)
def immutable_unique_id(self, space): if self.user_overridden_class: return None from pypy.objspace.std.model import IDTAG_LONG as tag b = space.bigint_w(self) b = b.lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(b)
def unmarshal_Long(space, u, tc): # XXX access internals from pypy.rlib.rbigint import rbigint lng = u.get_int() if lng < 0: sign = -1 lng = -lng elif lng > 0: sign = 1 else: sign = 0 if long_bits != 15: SHIFT = 15 result = rbigint([0], 0) for i in range(lng): shift = i * SHIFT result = result.add(rbigint.fromint(u.get_short()).lshift(shift)) if lng and not result.tobool(): raise_exception(space, "bad marshal data") if sign == -1: result = result.neg() else: digits = [0] * lng for i in range(lng): digit = u.get_int() if digit < 0: raise_exception(space, "bad marshal data") digits[i] = digit if digits[-1] == 0: raise_exception(space, "bad marshal data") result = rbigint(digits, sign) w_long = W_LongObject(result) return w_long
def unique_id(self, space): if self.user_overridden_class: return W_Object.unique_id(self, space) from pypy.rlib.longlong2float import float2longlong from pypy.objspace.std.model import IDTAG_FLOAT as tag val = float2longlong(space.float_w(self)) b = rbigint.fromrarith_int(val) b = b.lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(b)
def f(space, w_int1, w_float2): f2 = w_float2.floatval i1 = w_int1.intval f1 = float(i1) if LONG_BIT > 32 and int(f1) != i1: res = revcompare(f2, rbigint.fromint(i1)) else: res = op(f1, f2) return space.newbool(res)
def test_pow_lll_bug(self): two = rbigint.fromint(2) t = rbigint.fromlong(2655689964083835493447941032762343136647965588635159615997220691002017799304) for n, expected in [(37, 9), (1291, 931), (67889, 39464)]: v = two.pow(t, rbigint.fromint(n)) assert v.toint() == expected # # more tests, comparing against CPython's answer enabled = sample(range(5*32), 10) for i in range(5*32): t = t.mul(two) # add one random bit if random() >= 0.5: t = t.add(rbigint.fromint(1)) if i not in enabled: continue # don't take forever n = randint(1, sys.maxint) v = two.pow(t, rbigint.fromint(n)) assert v.toint() == pow(2, t.tolong(), n)
def f(space, w_float1, w_int2): f1 = w_float1.floatval i2 = w_int2.intval f2 = float(i2) if LONG_BIT > 32 and int(f2) != i2: res = compare(f1, rbigint.fromint(i2)) else: res = op(f1, f2) return space.newbool(res)
def f(space, w_int1, w_float2): f2 = w_float2.floatval i1 = w_int1.intval f1 = float(i1) if LONG_BIT > 32 and int(f1) != i1: res = revcompare(f2, rbigint.fromint(i1)) else: res = op(f1, f2) return space.newbool(res)
def immutable_unique_id(self, space): if self.user_overridden_class: return None from pypy.rlib.longlong2float import float2longlong from pypy.objspace.std.model import IDTAG_FLOAT as tag val = float2longlong(space.float_w(self)) b = rbigint.fromrarith_int(val) b = b.lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(b)
def cmp_( self, other ): if self.bvalue: if other.bvalue: o = other.bvalue else: o = rbigint.fromint( other.ivalue ) diff = self.bvalue.sub( o ) if diff.lt( ZERO ): return -1 elif diff.eq(ZERO): return 0 else: return 1 return self.ivalue - other.ivalue
def f(space, w_float1, w_int2): f1 = w_float1.floatval i2 = w_int2.intval f2 = float(i2) if LONG_BIT > 32 and int(f2) != i2: res = compare(f1, rbigint.fromint(i2)) else: res = op(f1, f2) return space.newbool(res)
def rshift__Long_Long(space, w_long1, w_long2): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise OperationError(space.w_ValueError, space.wrap("negative shift count")) try: shift = w_long2.num.toint() except OverflowError: # b too big # XXX maybe just return 0L instead? raise OperationError(space.w_OverflowError, space.wrap("shift count too large")) return newlong(space, w_long1.num.rshift(shift))
def lshift__Long_Long(space, w_long1, w_long2): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise OperationError(space.w_ValueError, space.wrap("negative shift count")) try: return W_LongObject(w_long1.num.lshift(w_long2.num)) except OverflowError: # b too big raise OperationError(space.w_OverflowError, space.wrap("shift count too large"))
def unmarshal_Long(space, u, tc): from pypy.rlib.rbigint import rbigint lng = u.get_int() if lng < 0: negative = True lng = -lng else: negative = False SHIFT = 15 result = rbigint.fromint(0) for i in range(lng): shift = i * SHIFT result = result.or_(rbigint.fromint(u.get_short()).lshift(shift)) if lng and not result.tobool(): raise_exception(space, 'bad marshal data') if negative: result = result.neg() w_long = newlong(space, result) return w_long
def rshift__Long_Long(space, w_long1, w_long2): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise OperationError(space.w_ValueError, space.wrap("negative shift count")) try: return W_LongObject(w_long1.num.rshift(w_long2.num)) except OverflowError: # b too big # XXX maybe just return 0L instead? raise OperationError(space.w_OverflowError, space.wrap("shift count too large"))
def pow__Long_Long_Long(space, w_long1, w_long2, w_long3): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise OperationError( space.w_TypeError, space.wrap("pow() 2nd argument " "cannot be negative when 3rd argument specified") ) try: return W_LongObject(w_long1.num.pow(w_long2.num, w_long3.num)) except ValueError: raise OperationError(space.w_ValueError, space.wrap("pow 3rd argument cannot be 0"))
def unmarshal_Long(space, u, tc): from pypy.rlib.rbigint import rbigint lng = u.get_int() if lng < 0: negative = True lng = -lng else: negative = False SHIFT = 15 result = rbigint.fromint(0) for i in range(lng): shift = i * SHIFT result = result.or_(rbigint.fromint(u.get_short()).lshift(shift)) if lng and not result.tobool(): raise_exception(space, 'bad marshal data') if negative: result = result.neg() w_long = newlong(space, result) return w_long
def lshift__Long_Long(space, w_long1, w_long2): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise OperationError(space.w_ValueError, space.wrap("negative shift count")) try: shift = w_long2.num.toint() except OverflowError: # b too big raise OperationError(space.w_OverflowError, space.wrap("shift count too large")) return W_LongObject(w_long1.num.lshift(shift))
def immutable_unique_id(self, space): if self.user_overridden_class: return None from pypy.rlib.longlong2float import float2longlong from pypy.objspace.std.model 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(float2longlong(imag)) val = real_b.lshift(64).or_(imag_b).lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(val)
def unique_id(self, space): if self.user_overridden_class: return W_Object.unique_id(self, space) from pypy.rlib.longlong2float import float2longlong from pypy.objspace.std.model 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(float2longlong(imag)) val = real_b.lshift(64).or_(imag_b).lshift(3).or_(rbigint.fromint(tag)) return space.newlong_from_rbigint(val)
def test_int_conversion(self): f1 = rbigint.fromlong(12332) f2 = rbigint.fromint(12332) assert f2.tolong() == f1.tolong() assert f2.toint() assert rbigint.fromlong(42).tolong() == 42 assert rbigint.fromlong(-42).tolong() == -42 u = f2.touint() assert u == 12332 assert type(u) is r_uint
def pow__Long_Long_Long(space, w_long1, w_long2, w_long3): # XXX need to replicate some of the logic, to get the errors right if w_long2.num.lt(rbigint.fromint(0)): raise OperationError( space.w_TypeError, space.wrap("pow() 2nd argument " "cannot be negative when 3rd argument specified")) try: return W_LongObject(w_long1.num.pow(w_long2.num, w_long3.num)) except ValueError: raise OperationError(space.w_ValueError, space.wrap("pow 3rd argument cannot be 0"))
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 test_rint_variants(self): py.test.skip("XXX broken!") from pypy.rpython.tool.rfficache import platform space = self.space for r in platform.numbertype_to_rclass.values(): if r is int: continue print r values = [0, -1, r.MASK>>1, -(r.MASK>>1)-1] for x in values: if not r.SIGNED: x &= r.MASK w_obj = space.wrap(r(x)) assert space.bigint_w(w_obj).eq(rbigint.fromint(x))
def fromint(space, intval): return W_LongObject(rbigint.fromint(intval))