def pow_ovr(space, w_int1, w_int2): try: return _impl_pow(space, r_longlong(w_int1.intval), w_int2) except FailedToImplementArgs: from pypy.objspace.std import longobject w_a = W_LongObject.fromint(space, w_int1.intval) w_b = W_LongObject.fromint(space, w_int2.intval) return longobject.pow__Long_Long_None(space, w_a, w_b, space.w_None)
def lshift_ovr(space, w_int1, w_int2): a = r_longlong(w_int1.intval) try: return lshift__SmallLong_Int(space, W_SmallLongObject(a), w_int2) except FailedToImplementArgs: from pypy.objspace.std import longobject w_a = W_LongObject.fromint(space, w_int1.intval) w_b = W_LongObject.fromint(space, w_int2.intval) return longobject.lshift__Long_Long(space, w_a, w_b)
def _pow_ovf2long(space, iv, iw, w_modulus): if space.is_none(w_modulus) and _recover_with_smalllong(space): from pypy.objspace.std.smalllongobject import _pow as _pow_small try: # XXX: shouldn't have to pass r_longlong(0) here (see # 4fa4c6b93a84) return _pow_small(space, r_longlong(iv), iw, r_longlong(0)) except (OverflowError, ValueError): pass from pypy.objspace.std.longobject import W_LongObject w_iv = W_LongObject.fromint(space, iv) w_iw = W_LongObject.fromint(space, iw) return w_iv.descr_pow(space, w_iw, w_modulus)
def ovf2long(space, x, y): """Handle overflowing to smalllong or long""" if _recover_with_smalllong(space): if ovf2small: return ovf2small(space, x, y) # Assume a generic operation without an explicit ovf2small # handler from pypy.objspace.std.smalllongobject import W_SmallLongObject a = r_longlong(x) b = r_longlong(y) return W_SmallLongObject(op(a, b)) from pypy.objspace.std.longobject import W_LongObject w_x = W_LongObject.fromint(space, x) w_y = W_LongObject.fromint(space, y) return getattr(w_x, 'descr_' + opname)(space, w_y)
def _ovf2long_lshift(space, x, w_x, y, w_y): if _recover_with_smalllong(space): return _lshift_ovf2small(space, x, y) from pypy.objspace.std.longobject import W_LongObject, W_AbstractLongObject if w_x is None or not isinstance(w_x, W_AbstractLongObject): w_x = W_LongObject.fromint(space, x) # crucially, *don't* convert w_y to W_LongObject, it will just be # converted back (huge lshifts always overflow) return w_x._int_lshift(space, y)
except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) else: # otherwise, use the __long__() method w_obj = space.long(w_value) # 'long(x)' should return whatever x.__long__() returned if space.is_w(w_longtype, space.w_long): return w_obj if space.is_true(space.isinstance(w_obj, space.w_long)): assert isinstance(w_obj, W_LongObject) # XXX this could fail! # XXX find a way to do that even if w_obj is not a W_LongObject w_value = w_obj elif space.is_true(space.isinstance(w_obj, space.w_int)): intval = space.int_w(w_obj) w_value = W_LongObject.fromint(space, intval) else: raise OperationError(space.w_ValueError, space.wrap("value can't be converted to long")) else: base = space.int_w(w_base) if space.is_true(space.isinstance(w_value, space.w_unicode)): from pypy.objspace.std.unicodeobject import unicode_to_decimal_w s = unicode_to_decimal_w(space, w_value) else: try: s = space.str_w(w_value) except OperationError, e: raise OperationError(space.w_TypeError, space.wrap("long() can't convert non-string "
def newlong(self, val): # val is an int if self.config.objspace.std.withsmalllong: from pypy.objspace.std.smalllongobject import W_SmallLongObject return W_SmallLongObject.fromint(val) return W_LongObject.fromint(self, val)
def as_w_long(self, space): # XXX: should try smalllong from pypy.objspace.std.longobject import W_LongObject return W_LongObject.fromint(space, self.intval)
def descr_long(self, space): # XXX: should try smalllong from pypy.objspace.std.longobject import W_LongObject return W_LongObject.fromint(space, self.intval)