Esempio n. 1
0
 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)
Esempio n. 2
0
 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):
         return wrapstr(self, 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)
Esempio n. 3
0
 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):
         return wrapstr(self, 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)