def _divmod_ovf2small(space, x, y): from pypy.objspace.std.smalllongobject import W_SmallLongObject a = r_longlong(x) b = r_longlong(y) return space.newtuple( [W_SmallLongObject(a // b), W_SmallLongObject(a % b)])
def test_direct(space): w5 = space.wrap(r_longlong(5)) assert isinstance(w5, W_SmallLongObject) wlarge = space.wrap(r_longlong(0x123456789ABCDEFL)) # assert space.int_w(w5) == 5 if sys.maxint < 0x123456789ABCDEFL: with pytest.raises(OperationError): space.int_w(wlarge) else: assert space.int_w(wlarge) == 0x123456789ABCDEF # assert space.pos(w5) is w5 assert space.abs(w5) is w5 wm5 = space.wrap(r_longlong(-5)) assert space.int_w(space.abs(wm5)) == 5 assert space.int_w(space.neg(w5)) == -5 assert space.is_true(w5) is True assert space.is_true(wm5) is True w0 = space.wrap(r_longlong(0)) assert space.is_true(w0) is False # w14000000000000 = space.wrap(r_longlong(0x14000000000000L)) assert space.is_true(space.eq( space.lshift(w5, space.wrap(49)), w14000000000000)) is False assert space.is_true(space.eq( space.lshift(w5, space.wrap(50)), w14000000000000)) is True # w_huge = space.sub(space.lshift(w5, space.wrap(150)), space.wrap(1)) wx = space.and_(w14000000000000, w_huge) assert space.is_true(space.eq(wx, w14000000000000)) w_obj = W_SmallLongObject.fromint(42) assert space.unwrap(w_obj) == 42
def test_direct(): space = gettestobjspace(**{"objspace.std.withsmalllong": True}) w5 = space.wrap(r_longlong(5)) assert isinstance(w5, W_SmallLongObject) wlarge = space.wrap(r_longlong(0x123456789ABCDEFL)) # assert space.int_w(w5) == 5 if sys.maxint < 0x123456789ABCDEFL: py.test.raises(OperationError, space.int_w, wlarge) else: assert space.int_w(wlarge) == 0x123456789ABCDEF # assert space.pos(w5) is w5 assert space.abs(w5) is w5 wm5 = space.wrap(r_longlong(-5)) assert space.int_w(space.abs(wm5)) == 5 assert space.int_w(space.neg(w5)) == -5 assert space.is_true(w5) is True assert space.is_true(wm5) is True w0 = space.wrap(r_longlong(0)) assert space.is_true(w0) is False # w14000000000000 = space.wrap(r_longlong(0x14000000000000L)) assert space.is_true(space.eq( space.lshift(w5, space.wrap(49)), w14000000000000)) is False assert space.is_true(space.eq( space.lshift(w5, space.wrap(50)), w14000000000000)) is True # w_huge = space.sub(space.lshift(w5, space.wrap(150)), space.wrap(1)) wx = space.and_(w14000000000000, w_huge) assert space.is_true(space.eq(wx, w14000000000000)) w_obj = W_SmallLongObject.fromint(42) assert space.unwrap(w_obj) == 42
def wrap(self, x): "Wraps the Python value 'x' into one of the wrapper classes." # You might notice that this function is rather conspicuously # not RPython. We can get away with this because the function # is specialized (see after the function body). Also worth # noting is that the isinstance's involving integer types # behave rather differently to how you might expect during # annotation (see pypy/annotation/builtin.py) if x is None: return self.w_None if isinstance(x, OperationError): raise TypeError, ("attempt to wrap already wrapped exception: %s"% (x,)) if isinstance(x, int): if isinstance(x, bool): return self.newbool(x) else: return self.newint(x) if isinstance(x, str): # this hack is temporary: look at the comment in # test_stdstdobjspace.test_wrap_string try: unicode_x = x.decode('ascii') except UnicodeDecodeError: # poor man's x.decode('ascii', 'replace'), since it's not # supported by RPython if not we_are_translated(): print 'WARNING: space.wrap() called on a non-ascii byte string: %r' % x lst = [] for ch in x: ch = ord(ch) if ch > 127: lst.append(u'\ufffd') else: lst.append(unichr(ch)) unicode_x = u''.join(lst) return wrapunicode(self, unicode_x) if isinstance(x, unicode): return wrapunicode(self, x) if isinstance(x, float): return W_FloatObject(x) if isinstance(x, W_Root): w_result = x.__spacebind__(self) #print 'wrapping', x, '->', w_result return w_result if isinstance(x, base_int): if self.config.objspace.std.withsmalllong: from pypy.objspace.std.smalllongobject import W_SmallLongObject from rpython.rlib.rarithmetic import r_longlong, r_ulonglong from rpython.rlib.rarithmetic import longlongmax if (not isinstance(x, r_ulonglong) or x <= r_ulonglong(longlongmax)): return W_SmallLongObject(r_longlong(x)) x = widen(x) if isinstance(x, int): return self.newint(x) else: return W_LongObject.fromrarith_int(x) return self._wrap_not_rpython(x)
def descr_neg(self, space): a = self.intval try: b = ovfcheck(-a) except OverflowError: if _recover_with_smalllong(space): from pypy.objspace.std.smalllongobject import W_SmallLongObject x = r_longlong(a) return W_SmallLongObject(-x) return self.as_w_long(space).descr_neg(space) return wrapint(space, b)
def wraplong(self, x): if self.config.objspace.std.withsmalllong: from rpython.rlib.rarithmetic import r_longlong try: rx = r_longlong(x) except OverflowError: pass else: from pypy.objspace.std.smalllongobject import \ W_SmallLongObject return W_SmallLongObject(rx) return W_LongObject.fromlong(x)
def newint(self, intval): if self.config.objspace.std.withsmalllong and isinstance(intval, base_int): from pypy.objspace.std.smalllongobject import W_SmallLongObject from rpython.rlib.rarithmetic import r_longlong, r_ulonglong from rpython.rlib.rarithmetic import longlongmax if (not isinstance(intval, r_ulonglong) or intval <= r_ulonglong(longlongmax)): return W_SmallLongObject(r_longlong(intval)) intval = widen(intval) if not isinstance(intval, int): return W_LongObject.fromrarith_int(intval) return wrapint(self, intval)
def newlong(space, bigint, w_symbolic=False): """Turn the bigint into a W_LongObject. If withsmalllong is enabled, check if the bigint would fit in a smalllong, and return a W_SmallLongObject instead if it does. """ if space.config.objspace.std.withsmalllong: try: z = bigint.tolonglong() except OverflowError: pass else: from pypy.objspace.std.smalllongobject import W_SmallLongObject return W_SmallLongObject(z, w_symbolic) return W_LongObject(bigint, w_symbolic)
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 wrap(self, x): "Wraps the Python value 'x' into one of the wrapper classes." # You might notice that this function is rather conspicuously # not RPython. We can get away with this because the function # is specialized (see after the function body). Also worth # noting is that the isinstance's involving integer types # behave rather differently to how you might expect during # annotation (see pypy/annotation/builtin.py) if x is None: return self.w_None if isinstance(x, model.W_Object): raise TypeError, "attempt to wrap already wrapped object: %s" % ( x, ) if isinstance(x, OperationError): raise TypeError, ("attempt to wrap already wrapped exception: %s" % (x, )) if isinstance(x, int): if isinstance(x, bool): return self.newbool(x) else: return self.newint(x) if isinstance(x, str): return wrapstr(self, x) if isinstance(x, unicode): return wrapunicode(self, x) if isinstance(x, float): return W_FloatObject(x) if isinstance(x, Wrappable): w_result = x.__spacebind__(self) #print 'wrapping', x, '->', w_result return w_result if isinstance(x, base_int): if self.config.objspace.std.withsmalllong: from pypy.objspace.std.smalllongobject import W_SmallLongObject from pypy.rlib.rarithmetic import r_longlong, r_ulonglong from pypy.rlib.rarithmetic import longlongmax if (not isinstance(x, r_ulonglong) or x <= r_ulonglong(longlongmax)): return W_SmallLongObject(r_longlong(x)) x = widen(x) if isinstance(x, int): return self.newint(x) else: return W_LongObject.fromrarith_int(x) return self._wrap_not_rpython(x)
def newbigint(space, w_longtype, bigint): """Turn the bigint into a W_LongObject. If withsmalllong is enabled, check if the bigint would fit in a smalllong, and return a W_SmallLongObject instead if it does. Similar to newlong() in longobject.py, but takes an explicit w_longtype argument. """ if (space.config.objspace.std.withsmalllong and space.is_w(w_longtype, space.w_long)): try: z = bigint.tolonglong() except OverflowError: pass else: from pypy.objspace.std.smalllongobject import W_SmallLongObject return W_SmallLongObject(z) w_obj = space.allocate_instance(W_LongObject, w_longtype) W_LongObject.__init__(w_obj, bigint) return w_obj
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 w__long(space, w_obj): return W_SmallLongObject.frombigint(space.bigint_w(w_obj))
def _lshift_ovf2small(space, a, b): from pypy.objspace.std.smalllongobject import W_SmallLongObject w_a = W_SmallLongObject.fromint(a) w_b = W_SmallLongObject.fromint(b) return w_a.descr_lshift(space, w_b)
def w__small(space, w_obj): return W_SmallLongObject.frombigint(space.bigint_w(w_obj))
def _lshift_ovf2small(space, a, b): from pypy.objspace.std.smalllongobject import W_SmallLongObject w_a = W_SmallLongObject.fromint(a) w_b = W_SmallLongObject.fromint(b) return w_a.descr_lshift(space, w_b)
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 _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: if self.config.objspace.std.withsmalllong: from pypy.rlib.rarithmetic import r_longlong try: rx = r_longlong(x) except OverflowError: pass else: from pypy.objspace.std.smalllongobject import \ W_SmallLongObject return W_SmallLongObject(rx) return W_LongObject.fromlong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): rdict_w = r_dict(self.eq_w, self.hash_w) for item in x: rdict_w[self.wrap(item)] = None res = W_SetObject(self, rdict_w) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis if self.config.objspace.nofaking: raise OperationError(self.w_RuntimeError, self.wrap("nofaking enabled: refusing " "to wrap cpython value %r" %(x,))) if isinstance(x, type(Exception)) and issubclass(x, Exception): w_result = self.wrap_exception_cls(x) if w_result is not None: return w_result from pypy.objspace.std.fake import fake_object return fake_object(self, x)