def descr__new__(space, w_floattype, w_x): from pypy.objspace.std.floatobject import W_FloatObject w_value = w_x # 'x' is the keyword argument name in CPython if space.lookup(w_value, "__float__") is not None: w_obj = space.float(w_value) if space.is_w(w_floattype, space.w_float): return w_obj value = space.float_w(w_obj) elif space.isinstance_w(w_value, space.w_unicode): from unicodeobject import unicode_to_decimal_w value = _string_to_float(space, w_value, unicode_to_decimal_w(space, w_value)) else: try: value = space.charbuf_w(w_value) except OperationError as e: if e.match(space, space.w_TypeError): raise OperationError( space.w_TypeError, space.wrap( "float() argument must be a string or a number")) raise value = _string_to_float(space, w_value, value) w_obj = space.allocate_instance(W_FloatObject, w_floattype) W_FloatObject.__init__(w_obj, value) return w_obj
def test_pow_fff(self): x = 10.0 y = 2.0 z = 13.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) f3 = W_FloatObject(z) self.space.raises_w(self.space.w_TypeError, f1.descr_pow, self.space, f2, f3)
def wrap(self, x): """ Wraps the Python value 'x' into one of the wrapper classes. This should only be used for tests, in real code you need to use the explicit new* methods.""" 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 self.newtext(x) if isinstance(x, unicode): return self.newunicode(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): return self.newint(x) # we might get there in non-translated versions if 'x' is # a long that fits the correct range. if is_valid_int(x): return self.newint(x) return self._wrap_not_rpython(x)
def wrap(self, x): """ Wraps the Python value 'x' into one of the wrapper classes. This should only be used for tests, in real code you need to use the explicit new* methods.""" 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: return self._wrap_string_old(x) return self.newunicode(unicode_x) if isinstance(x, unicode): return self.newunicode(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): return self.newint(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, 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 test_pow_ffn(self): x = 10.0 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x ** y f1 = W_FloatObject(-1.23) f2 = W_FloatObject(-4.56) self.space.raises_w(self.space.w_ValueError, f1.descr_pow, self.space, f2, self.space.w_None) x = -10 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x**y
def descr__new__(space, w_floattype, w_x): from pypy.objspace.std.floatobject import W_FloatObject w_value = w_x # 'x' is the keyword argument name in CPython if space.lookup(w_value, "__float__") is not None: w_obj = space.float(w_value) if space.is_w(w_floattype, space.w_float): return w_obj value = space.float_w(w_obj) elif space.isinstance_w(w_value, space.w_unicode): from unicodeobject import unicode_to_decimal_w value = _string_to_float(space, w_value, unicode_to_decimal_w(space, w_value)) else: try: value = space.charbuf_w(w_value) except OperationError as e: if e.match(space, space.w_TypeError): raise OperationError(space.w_TypeError, space.wrap( "float() argument must be a string or a number")) raise value = _string_to_float(space, w_value, value) w_obj = space.allocate_instance(W_FloatObject, w_floattype) W_FloatObject.__init__(w_obj, value) return w_obj
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 test_pow_ffn(self): x = 10.0 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x ** y f1 = W_FloatObject(-1.23) f2 = W_FloatObject(-4.56) v = f1.descr_pow(self.space, f2, self.space.w_None) assert self.space.isinstance_w(v, self.space.w_complex) x = -10 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x**y
def newfloat(self, floatval): return W_FloatObject(floatval)
from pypy.objspace.std.ropeunicodeobject import unicode_to_decimal_w else: from unicodeobject import unicode_to_decimal_w strvalue = unicode_to_decimal_w(space, w_value) try: if USE_NEW_S2F: value = interp_string_to_float(space, strvalue) else: value = string_to_float(strvalue) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) else: w_obj = space.float(w_value) if space.is_w(w_floattype, space.w_float): return w_obj # 'float(x)' should return # whatever x.__float__() returned value = space.float_w(w_obj) w_obj = space.allocate_instance(W_FloatObject, w_floattype) W_FloatObject.__init__(w_obj, value) return w_obj # ____________________________________________________________ float_typedef = StdTypeDef("float", __doc__ = '''float(x) -> floating point number Convert a string or number to a floating point number, if possible.''', __new__ = newmethod(descr__new__), )
def test_pow_ffn(self): x = 10.0 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x**y f1 = W_FloatObject(-1.23) f2 = W_FloatObject(-4.56) self.space.raises_w(self.space.w_ValueError, f1.descr_pow, self.space, f2, self.space.w_None) x = -10 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x**y
def unmarshal_Float_bin(space, u, tc): return W_FloatObject(unpack_float(u.get(8)))
def test_pow_ffn(self): x = 10.0 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x**y f1 = W_FloatObject(-1.23) f2 = W_FloatObject(-4.56) v = f1.descr_pow(self.space, f2, self.space.w_None) assert self.space.isinstance_w(v, self.space.w_complex) x = -10 y = 2.0 f1 = W_FloatObject(x) f2 = W_FloatObject(y) v = f1.descr_pow(self.space, f2, self.space.w_None) assert v.floatval == x**y