def lshift_bound(self, other): if self.has_upper and self.has_lower and \ other.has_upper and other.has_lower and \ other.known_ge(IntBound(0, 0)) and \ other.known_lt(IntBound(LONG_BIT, LONG_BIT)): try: vals = (ovfcheck_lshift(self.upper, other.upper), ovfcheck_lshift(self.upper, other.lower), ovfcheck_lshift(self.lower, other.upper), ovfcheck_lshift(self.lower, other.lower)) return IntBound(min4(vals), max4(vals)) except (OverflowError, ValueError): return IntUnbounded() else: return IntUnbounded()
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 op_int_lshift_ovf_val(self, x, y): assert isinstance(x, int) assert isinstance(y, int) try: return ovfcheck_lshift(x, y) except (OverflowError, ValueError): self.make_llexception()
def op_int_lshift_ovf_val(self, x, y): assert isinstance(x, int) assert isinstance(y, int) try: return ovfcheck_lshift(x, y) except (OverflowError, ValueError): self.make_llexception()
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 fn(maxofs): lastofs = 0 ofs = 1 while ofs < maxofs: lastofs = ofs try: ofs = ovfcheck_lshift(ofs, 1) except OverflowError: ofs = maxofs else: ofs = ofs + 1 return lastofs
def fn(maxofs): lastofs = 0 ofs = 1 while ofs < maxofs: lastofs = ofs try: ofs = ovfcheck_lshift(ofs, 1) except OverflowError: ofs = maxofs else: ofs = ofs + 1 return lastofs
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")) try: c = ovfcheck_lshift(a, b) except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer left shift")) return wrapint(space, c)
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")) try: c = ovfcheck_lshift(a, b) except OverflowError: raise FailedToImplement(space.w_OverflowError, space.wrap("integer left shift")) return wrapint(space, c)
def lshift__Int_Int(space, w_int1, w_int2): a = w_int1.intval b = w_int2.intval if r_uint(b) < LONG_BIT: # 0 <= b < LONG_BIT try: c = ovfcheck_lshift(a, b) except OverflowError: raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer left shift")) return wrapint(space, c) if b < 0: raise OperationError(space.w_ValueError, space.wrap("negative shift count")) else: #b >= LONG_BIT if a == 0: return get_integer(space, w_int1) raise FailedToImplementArgs(space.w_OverflowError, space.wrap("integer left shift"))
def lshift_ovf(x, y): return ovfcheck_lshift(x, y)
def lshift_func(i=numtype): try: hugo(2, 3, 5) return ovfcheck_lshift((-maxint-1), i) except (hugelmugel, OverflowError, StandardError, ValueError): raise
def lshift_ovf(x, y): return ovfcheck_lshift(x, y)
def gallop(self, key, a, hint, rightmost): assert 0 <= hint < a.len if rightmost: lower = self.le # search for the largest k for which a[k] <= key else: lower = self.lt # search for the largest k for which a[k] < key p = a.base + hint lastofs = 0 ofs = 1 if lower(a.list[p], key): # a[hint] < key -- gallop right, until # a[hint + lastofs] < key <= a[hint + ofs] maxofs = a.len - hint # a[a.len-1] is highest while ofs < maxofs: if lower(a.list[p + ofs], key): lastofs = ofs try: ofs = ovfcheck_lshift(ofs, 1) except OverflowError: ofs = maxofs else: ofs = ofs + 1 else: # key <= a[hint + ofs] break if ofs > maxofs: ofs = maxofs # Translate back to offsets relative to a. lastofs += hint ofs += hint else: # key <= a[hint] -- gallop left, until # a[hint - ofs] < key <= a[hint - lastofs] maxofs = hint + 1 # a[0] is lowest while ofs < maxofs: if lower(a.list[p - ofs], key): break else: # key <= a[hint - ofs] lastofs = ofs try: ofs = ovfcheck_lshift(ofs, 1) except OverflowError: ofs = maxofs else: ofs = ofs + 1 if ofs > maxofs: ofs = maxofs # Translate back to positive offsets relative to a. lastofs, ofs = hint-ofs, hint-lastofs assert -1 <= lastofs < ofs <= a.len # Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the # right of lastofs but no farther right than ofs. Do a binary # search, with invariant a[lastofs-1] < key <= a[ofs]. lastofs += 1 while lastofs < ofs: m = lastofs + ((ofs - lastofs) >> 1) if lower(a.list[a.base + m], key): lastofs = m+1 # a[m] < key else: ofs = m # key <= a[m] assert lastofs == ofs # so a[ofs-1] < key <= a[ofs] return ofs
def gallop(self, key, a, hint, rightmost): assert 0 <= hint < a.len if rightmost: lower = self.le # search for the largest k for which a[k] <= key else: lower = self.lt # search for the largest k for which a[k] < key p = a.base + hint lastofs = 0 ofs = 1 if lower(a.list[p], key): # a[hint] < key -- gallop right, until # a[hint + lastofs] < key <= a[hint + ofs] maxofs = a.len - hint # a[a.len-1] is highest while ofs < maxofs: if lower(a.list[p + ofs], key): lastofs = ofs try: ofs = ovfcheck_lshift(ofs, 1) except OverflowError: ofs = maxofs else: ofs = ofs + 1 else: # key <= a[hint + ofs] break if ofs > maxofs: ofs = maxofs # Translate back to offsets relative to a. lastofs += hint ofs += hint else: # key <= a[hint] -- gallop left, until # a[hint - ofs] < key <= a[hint - lastofs] maxofs = hint + 1 # a[0] is lowest while ofs < maxofs: if lower(a.list[p - ofs], key): break else: # key <= a[hint - ofs] lastofs = ofs try: ofs = ovfcheck_lshift(ofs, 1) except OverflowError: ofs = maxofs else: ofs = ofs + 1 if ofs > maxofs: ofs = maxofs # Translate back to positive offsets relative to a. lastofs, ofs = hint - ofs, hint - lastofs assert -1 <= lastofs < ofs <= a.len # Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the # right of lastofs but no farther right than ofs. Do a binary # search, with invariant a[lastofs-1] < key <= a[ofs]. lastofs += 1 while lastofs < ofs: m = lastofs + ((ofs - lastofs) >> 1) if lower(a.list[a.base + m], key): lastofs = m + 1 # a[m] < key else: ofs = m # key <= a[m] assert lastofs == ofs # so a[ofs-1] < key <= a[ofs] return ofs
def lshift_func(i=numtype): try: hugo(2, 3, 5) return ovfcheck_lshift((-maxint - 1), i) except (hugelmugel, OverflowError, StandardError, ValueError): raise
def f(x): try: return ovfcheck_lshift(x, 2) except OverflowError: return -42