[pypy-commit] pypy refactor-buffer-api: fix float.__new__ bufferstr behavior

bdkearns noreply at buildbot.pypy.org
Thu Apr 24 22:52:16 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: refactor-buffer-api
Changeset: r70941:47fd7571d5f0
Date: 2014-04-24 16:49 -0400
http://bitbucket.org/pypy/pypy/changeset/47fd7571d5f0/

Log:	fix float.__new__ bufferstr behavior

diff --git a/pypy/objspace/std/floattype.py b/pypy/objspace/std/floattype.py
--- a/pypy/objspace/std/floattype.py
+++ b/pypy/objspace/std/floattype.py
@@ -23,7 +23,7 @@
 register_all(vars(), globals())
 
 
- at unwrap_spec(w_x = WrappedDefault(0.0))
+ at unwrap_spec(w_x=WrappedDefault(0.0))
 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
@@ -32,15 +32,19 @@
         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_str) or
-          space.isinstance_w(w_value, space.w_bytearray)):
-        value = _string_to_float(space, w_value, space.bufferstr_w(w_value))
     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:
-        value = space.float_w(w_x)
+        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
diff --git a/pypy/objspace/std/test/test_floatobject.py b/pypy/objspace/std/test/test_floatobject.py
--- a/pypy/objspace/std/test/test_floatobject.py
+++ b/pypy/objspace/std/test/test_floatobject.py
@@ -61,7 +61,7 @@
 
 class AppTestAppFloatTest:
     spaceconfig = dict(usemodules=['binascii', 'rctime'])
-    
+
     def setup_class(cls):
         cls.w_py26 = cls.space.wrap(sys.version_info >= (2, 6))
 
@@ -138,6 +138,11 @@
         assert repr(float("+nan")) == "nan"
         assert repr(float("-nAn")) == "nan"
 
+        assert float(buffer("inf")) == inf
+        assert float(bytearray("inf")) == inf
+        exc = raises(TypeError, float, memoryview("inf"))
+        assert str(exc.value) == "float() argument must be a string or a number"
+
     def test_float_unicode(self):
         # u00A0 and u2000 are some kind of spaces
         assert 42.75 == float(unichr(0x00A0)+unicode("42.75")+unichr(0x2000))
@@ -812,7 +817,7 @@
 
         def check(a, b):
             assert (a, math.copysign(1.0, a)) == (b, math.copysign(1.0, b))
-            
+
         check(mod(-1.0, 1.0), 0.0)
         check(mod(-1e-100, 1.0), 1.0)
         check(mod(-0.0, 1.0), 0.0)


More information about the pypy-commit mailing list