def test_pow_iin(self): x = 10 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) v = self.space.pow(f1, f2, self.space.w_None) assert v.intval == x ** y
def test_or(self): x = 12345678 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) v = self.space.or_(f1, f2) assert v.intval == x | y
def list_pop__RangeList_ANY(space, w_rangelist, w_idx=-1): if w_rangelist.w_list is not None: raise FailedToImplement length = w_rangelist.length if length == 0: raise OperationError(space.w_IndexError, space.wrap("pop from empty list")) if space.isinstance_w(w_idx, space.w_float): raise OperationError(space.w_TypeError, space.wrap("integer argument expected, got float") ) idx = space.int_w(space.int(w_idx)) if idx == 0: result = w_rangelist.start w_rangelist.start += w_rangelist.step w_rangelist.length -= 1 return wrapint(space, result) if idx == -1 or idx == length - 1: w_rangelist.length -= 1 return wrapint( space, w_rangelist.start + (length - 1) * w_rangelist.step) if idx >= w_rangelist.length: raise OperationError(space.w_IndexError, space.wrap("pop index out of range")) raise FailedToImplement
def test_rshift(self): x = 12345678 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) v = self.space.rshift(f1, f2) assert v.intval == x >> y
def test_and(self): x = 12345678 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) v = self.space.and_(f1, f2) assert v.intval == x & y
def test_mul(self): for x in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]: for y in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]: f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) result = self.space.unwrap(self.space.mul(f1, f2)) assert result == x*y and type(result) == type(x*y)
def test_div(self): for i in range(10): res = i//3 f1 = wrapint(self.space, i) f2 = wrapint(self.space, 3) result = self.space.div(f1, f2) assert result.intval == res
def test_mod(self): x = 1 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) v = self.space.mod(f1, f2) assert v.intval == x % y
def test_mul(self): for x in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]: for y in [0, 1, 100, sys.maxint // 2 - 50, sys.maxint - 1000]: f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) result = self.space.unwrap(self.space.mul(f1, f2)) assert result == x * y
def test_pow_iin(self): x = 10 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) v = self.space.pow(f1, f2, self.space.w_None) assert v.intval == x**y
def test_div(self): for i in range(10): res = i // 3 f1 = wrapint(self.space, i) f2 = wrapint(self.space, 3) result = self.space.div(f1, f2) assert result.intval == res
def test_divmod(self): x = 1 y = 2 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) ret = self.space.divmod(f1, f2) v, w = self.space.unwrap(ret) assert (v, w) == divmod(x, y)
def test_abs(self): x = 42 f1 = wrapint(self.space, x) v = self.space.abs(f1) assert v.intval == abs(x) x = -42 f1 = wrapint(self.space, x) v = self.space.abs(f1) assert v.intval == abs(x)
def test_pos(self): x = 42 f1 = wrapint(self.space, x) v = self.space.pos(f1) assert v.intval == +x x = -42 f1 = wrapint(self.space, x) v = self.space.pos(f1) assert v.intval == +x
def len__RopeIter(space, w_ropeiter): if w_ropeiter.node is None: return wrapint(space, 0) index = w_ropeiter.index length = w_ropeiter.node.length() result = length - index if result < 0: return wrapint(space, 0) return wrapint(space, result)
def test_add(self): for x in [1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint]: for y in [1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint]: f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) result = self.space.unwrap(self.space.add(f1, f2)) assert result == x+y
def len__RangeIter(space, w_rangeiter): if w_rangeiter.w_rangelist is None: return wrapint(space, 0) index = w_rangeiter.index w_length = space.len(w_rangeiter.w_rangelist) w_len = space.sub(w_length, wrapint(space, index)) if space.is_true(space.lt(w_len, wrapint(space, 0))): w_len = wrapint(space, 0) return w_len
def test_sub(self): for x in [1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint]: for y in [1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint]: f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) result = self.space.unwrap(self.space.sub(f1, f2)) assert result == x-y and type(result) == type(x-y)
def test_compare(self): import operator optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge'] for x in (-10, -1, 0, 1, 2, 1000, sys.maxint): for y in (-sys.maxint - 1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint): for op in optab: wx = wrapint(self.space, x) wy = wrapint(self.space, y) res = getattr(operator, op)(x, y) method = getattr(self.space, op) myres = method(wx, wy) assert self.space.unwrap(myres) == res
def test_compare(self): import operator optab = ['lt', 'le', 'eq', 'ne', 'gt', 'ge'] for x in (-10, -1, 0, 1, 2, 1000, sys.maxint): for y in (-sys.maxint-1, -11, -9, -2, 0, 1, 3, 1111, sys.maxint): for op in optab: wx = wrapint(self.space, x) wy = wrapint(self.space, y) res = getattr(operator, op)(x, y) method = getattr(self.space, op) myres = method(wx, wy) assert self.space.unwrap(myres) == res
def test_add(self): for x in [ 1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint ]: for y in [ 1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint ]: f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) result = self.space.unwrap(self.space.add(f1, f2)) assert result == x + y and type(result) == type(x + y)
def test_sub(self): for x in [ 1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint ]: for y in [ 1, 100, sys.maxint // 2 - 50, sys.maxint // 2, sys.maxint - 1000, sys.maxint ]: f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) result = self.space.unwrap(self.space.sub(f1, f2)) assert result == x - y
def test_pow_iii(self): x = 10 y = 2 z = 13 f1 = wrapint(self.space, x) f2 = wrapint(self.space, y) f3 = wrapint(self.space, z) v = self.space.pow(f1, f2, f3) assert v.intval == pow(x, y, z) f1, f2, f3 = [wrapint(self.space, i) for i in (10, -1, 42)] self.space.raises_w(self.space.w_TypeError, self.space.pow, f1, f2, f3) f1, f2, f3 = [wrapint(self.space, i) for i in (10, 5, 0)] self.space.raises_w(self.space.w_ValueError, self.space.pow, f1, f2, f3)
def lshift__Int_Int(space, w_int1, w_int2): a = w_int1.intval b = w_int2.intval if b < 0: raise OperationError(space.w_ValueError, space.wrap("negative shift count")) if a == 0 or b == 0: return int__Int(space, w_int1) if b >= LONG_BIT: raise FailedToImplement(space.w_OverflowError, space.wrap("integer left shift")) ## ## XXX please! have a look into pyport.h and see how to implement ## the overflow checking, using macro Py_ARITHMETIC_RIGHT_SHIFT ## we *assume* that the overflow checking is done correctly ## in the code generator, which is not trivial! ## XXX also note that Python 2.3 returns a long and never raises ## OverflowError. try: c = ovfcheck_lshift(a, b) ## the test in C code is ## if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { ## if (PyErr_Warn(PyExc_FutureWarning, # and so on except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer left shift")) return wrapint(space, c)
def newint(self, intval): # this time-critical and circular-imports-funny method was stored # on 'self' by initialize() # not sure how bad this is: from pypy.objspace.std.inttype import wrapint return wrapint(self, intval)
def _impl_int_int_pow(space, iv, iw, iz=0): if iw < 0: if iz != 0: raise OperationError( space.w_TypeError, space.wrap("pow() 2nd argument " "cannot be negative when 3rd argument specified")) ## bounce it, since it always returns float raise FailedToImplement(space.w_ValueError, space.wrap("integer exponentiation")) temp = iv ix = 1 try: while iw > 0: if iw & 1: ix = ovfcheck(ix * temp) iw >>= 1 #/* Shift exponent down by 1 bit */ if iw == 0: break temp = ovfcheck(temp * temp) #/* Square the value of temp */ if iz: #/* If we did a multiplication, perform a modulo */ ix = ix % iz temp = temp % iz if iz: ix = ix % iz except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer exponentiation")) return wrapint(space, ix)
def lshift__SmallInt_SmallInt(space, w_int1, w_int2): a = w_int1.intval b = w_int2.intval if b < 0: raise OperationError(space.w_ValueError, space.wrap("negative shift count")) if a == 0 or b == 0: return int__SmallInt(space, w_int1) if b >= LONG_BIT: raise FailedToImplement(space.w_OverflowError, space.wrap("integer left shift")) ## ## XXX please! have a look into pyport.h and see how to implement ## the overflow checking, using macro Py_ARITHMETIC_RIGHT_SHIFT ## we *assume* that the overflow checking is done correctly ## in the code generator, which is not trivial! ## XXX also note that Python 2.3 returns a long and never raises ## OverflowError. try: c = ovfcheck_lshift(a, b) ## the test in C code is ## if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { ## if (PyErr_Warn(PyExc_FutureWarning, # and so on except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer left shift")) return wrapint(space, c)
def neg__Int(space, w_int1): a = w_int1.intval try: x = ovfcheck(-a) except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer negation")) return wrapint(space, x)
def _impl_int_int_pow(space, iv, iw, iz=0): if iw < 0: if iz != 0: raise OperationError( space.w_TypeError, space.wrap("pow() 2nd argument " "cannot be negative when 3rd argument specified") ) ## bounce it, since it always returns float raise FailedToImplement(space.w_ValueError, space.wrap("integer exponentiation")) temp = iv ix = 1 try: while iw > 0: if iw & 1: ix = ovfcheck(ix * temp) iw >>= 1 # /* Shift exponent down by 1 bit */ if iw == 0: break temp = ovfcheck(temp * temp) # /* Square the value of temp */ if iz: # /* If we did a multiplication, perform a modulo */ ix = ix % iz temp = temp % iz if iz: ix = ix % iz except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer exponentiation")) return wrapint(space, ix)
def str_rfind__Rope_Rope_ANY_ANY(space, w_self, w_sub, w_start, w_end): # XXX works but flattens (self, sub, start, end) = _convert_idx_params(space, w_self, w_sub, w_start, w_end) self = self.flatten_string() sub = sub.flatten_string() res = self.rfind(sub, start, end) return wrapint(space, res)
def add__SmallInt_SmallInt(space, w_int1, w_int2): x = w_int1.intval y = w_int2.intval # note that no overflow checking is necessary here: x and y fit into 31 # bits (or 63 bits respectively), so their sum fits into 32 (or 64) bits. # wrapint then makes sure that either a tagged int or a normal int is # created return wrapint(space, x + y)
def div__SmallInt_SmallInt(space, w_int1, w_int2): x = w_int1.intval y = w_int2.intval if y == 0: raise OperationError(space.w_ZeroDivisionError, space.wrap("integer division by zero")) # no overflow possible return wrapint(space, x // y)
def mul__Int_Int(space, w_int1, w_int2): x = w_int1.intval y = w_int2.intval try: z = ovfcheck(x * y) except OverflowError: raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer multiplication")) return wrapint(space, z)
def int(self, space): if (type(self) is not W_IntObject and space.is_overloaded(self, space.w_int, '__int__')): return W_Object.int(self, space) if space.is_w(space.type(self), space.w_int): return self a = self.intval return wrapint(space, a)
def str_index__Rope_Rope_ANY_ANY(space, w_self, w_sub, w_start, w_end): (self, sub, start, end) = _convert_idx_params(space, w_self, w_sub, w_start, w_end) res = rope.find(self, sub, start, end) if res < 0: raise OperationError(space.w_ValueError, space.wrap("substring not found in string.index")) return wrapint(space, res)
def sub__Int_Int(space, w_int1, w_int2): x = w_int1.intval y = w_int2.intval try: z = ovfcheck(x - y) except OverflowError: raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer substraction")) return wrapint(space, z)
def add__Int_Int(space, w_int1, w_int2): x = w_int1.intval y = w_int2.intval try: z = ovfcheck(x + y) except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer addition")) return wrapint(space, z)
def neg__Int(space, w_int1): a = w_int1.intval try: x = ovfcheck(-a) except OverflowError: raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer negation")) return wrapint(space, x)
def getitem__RangeList_ANY(space, w_rangelist, w_index): if w_rangelist.w_list is not None: return space.getitem(w_rangelist.w_list, w_index) idx = space.getindex_w(w_index, space.w_IndexError, "list index") try: return wrapint(space, w_rangelist.getitem(idx)) except IndexError: raise OperationError(space.w_IndexError, space.wrap("list index out of range"))
def hash__String(space, w_str): s = w_str._value if we_are_translated(): x = hash(s) # to use the hash cache in rpython strings else: x = _hash_string(s) # to make sure we get the same hash as rpython # (otherwise translation will freeze W_DictObjects where we can't find # the keys any more!) return wrapint(space, x)
def str_rindex__Rope_Rope_ANY_ANY(space, w_self, w_sub, w_start, w_end): (self, sub, start, end) = _convert_idx_params(space, w_self, w_sub, w_start, w_end) # XXX works but flattens self = self.flatten_string() sub = sub.flatten_string() res = self.rfind(sub, start, end) if res < 0: raise OperationError(space.w_ValueError, space.wrap("substring not found in string.rindex")) return wrapint(space, res)
def mod__Int_Int(space, w_int1, w_int2): x = w_int1.intval y = w_int2.intval try: z = ovfcheck(x % y) except ZeroDivisionError: raise OperationError(space.w_ZeroDivisionError, space.wrap("integer modulo by zero")) except OverflowError: raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer modulo")) return wrapint(space, z)