[pypy-svn] r79703 - in pypy/branch/fast-forward/pypy/objspace/std: . test

afa at codespeak.net afa at codespeak.net
Wed Dec 1 11:09:50 CET 2010


Author: afa
Date: Wed Dec  1 11:09:47 2010
New Revision: 79703

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/floattype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py
Log:
Don't cheat and assume that float.__getformat__ may be missing from the host python.


Modified: pypy/branch/fast-forward/pypy/objspace/std/floattype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/floattype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/floattype.py	Wed Dec  1 11:09:47 2010
@@ -59,8 +59,34 @@
     return w_obj
 
 
-_float_format = float.__getformat__("float")
-_double_format = float.__getformat__("double")
+def detect_floatformat():
+    from pypy.rpython.lltypesystem import rffi, lltype
+    buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw')
+    rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0
+    packed = rffi.charpsize2str(buf, 8)
+    if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05":
+        double_format = 'IEEE, big-endian'
+    elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43":
+        double_format = 'IEEE, little-endian'
+    else:
+        double_format = 'unknown'
+    lltype.free(buf, flavor='raw')
+    #
+    buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw')
+    rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0)
+    packed = rffi.charpsize2str(buf, 4)
+    if packed == "\x4b\x7f\x01\x02":
+        float_format = 'IEEE, big-endian'
+    elif packed == "\x02\x01\x7f\x4b":
+        float_format = 'IEEE, little-endian'
+    else:
+        float_format = 'unknown'
+    lltype.free(buf, flavor='raw')
+
+    return double_format, float_format
+
+_double_format, _float_format = detect_floatformat()
+
 def descr___getformat__(space, w_cls, kind):
     if kind == "float":
         return space.wrap(_float_format)

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py	Wed Dec  1 11:09:47 2010
@@ -361,8 +361,8 @@
                 assert not (nan >  x)
 
     def test___getformat__(self):
-        float.__getformat__("float")
-        float.__getformat__("double")
+        assert float.__getformat__("float") != "unknown"
+        assert float.__getformat__("double") != "unknown"
         raises(ValueError, float.__getformat__, "random")
 
     def test_trunc(self):



More information about the Pypy-commit mailing list