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 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 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, 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): from pypy.objspace.std.stringtype import wrapstr return wrapstr(self, x) if isinstance(x, unicode): from pypy.objspace.std.unicodetype import wrapunicode 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, r_int) or isinstance(x, r_uint) or \ isinstance(x, r_longlong) or isinstance(x, r_ulonglong): return W_LongObject.fromrarith_int(x) # _____ below here is where the annotator should not get _____ # 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 W_TupleObject(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 isinstance(x, long): 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): wrappeditems = [self.wrap(item) for item in x] return W_SetObject(self, wrappeditems) 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: # annotation should actually not get here. If it does, you get # an error during rtyping because '%r' is not supported. It tells # you that there was a space.wrap() on a strange object. 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 #print "fake-wrapping", x from fake import fake_object return fake_object(self, x)