[pypy-svn] r46944 - in pypy/dist/pypy/module/struct: . test

arigo at codespeak.net arigo at codespeak.net
Thu Sep 27 13:10:37 CEST 2007


Author: arigo
Date: Thu Sep 27 13:10:37 2007
New Revision: 46944

Modified:
   pypy/dist/pypy/module/struct/standardfmttable.py
   pypy/dist/pypy/module/struct/test/test_struct.py
Log:
Use ieee.py to implement the 'f' and 'd' format characters
in standard sizes.


Modified: pypy/dist/pypy/module/struct/standardfmttable.py
==============================================================================
--- pypy/dist/pypy/module/struct/standardfmttable.py	(original)
+++ pypy/dist/pypy/module/struct/standardfmttable.py	Thu Sep 27 13:10:37 2007
@@ -7,6 +7,7 @@
 
 import struct
 from pypy.module.struct.error import StructError
+from pypy.module.struct import ieee
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
 
@@ -48,8 +49,11 @@
     for i in range(1 + prefix, count):
         fmtiter.result.append('\x00')
 
-def pack_float(fmtiter):
-    xxx
+def make_float_packer(size):
+    return lambda fmtiter: ieee.pack_float(fmtiter.result,
+                                           fmtiter.accept_float_arg(),
+                                           size,
+                                           fmtiter.bigendian)
 
 # ____________________________________________________________
 
@@ -127,8 +131,10 @@
         end = count
     fmtiter.appendobj(data[1:end])
 
-def unpack_float(fmtiter):
-    xxx
+def make_float_unpacker(size):
+    return lambda fmtiter: fmtiter.appendobj(ieee.unpack_float(
+        fmtiter.read(size),
+        fmtiter.bigendian))
 
 # ____________________________________________________________
 
@@ -185,8 +191,10 @@
           'needcount' : True },
     'p':{ 'size' : 1, 'pack' : pack_pascal, 'unpack' : unpack_pascal,
           'needcount' : True },
-    'f':{ 'size' : 4, 'pack' : pack_float, 'unpack' : unpack_float},
-    'd':{ 'size' : 8, 'pack' : pack_float, 'unpack' : unpack_float},
+    'f':{ 'size' : 4, 'pack' : make_float_packer(4),
+                    'unpack' : make_float_unpacker(4)},
+    'd':{ 'size' : 8, 'pack' : make_float_packer(8),
+                    'unpack' : make_float_unpacker(8)},
     }    
 
 for c, size in [('b', 1), ('h', 2), ('i', 4), ('l', 4), ('q', 8)]:

Modified: pypy/dist/pypy/module/struct/test/test_struct.py
==============================================================================
--- pypy/dist/pypy/module/struct/test/test_struct.py	(original)
+++ pypy/dist/pypy/module/struct/test/test_struct.py	Thu Sep 27 13:10:37 2007
@@ -242,14 +242,16 @@
         """
         Check the 'd' and 'f' format characters on standard packing.
         """
-        skip("in-progress")
         pack = self.struct.pack
         unpack = self.struct.unpack
         assert pack("!d", 12.5) == '@)\x00\x00\x00\x00\x00\x00'
-        assert pack("<d", 12.5) == '\x00\x00\x00\x00\x00\x00)@'
-        assert unpack("!d", '@)\x00\x00\x00\x00\x00\x00') == (12.5,)
+        assert pack("<d", -12.5) == '\x00\x00\x00\x00\x00\x00)\xc0'
+        assert unpack("!d", '\xc0)\x00\x00\x00\x00\x00\x00') == (-12.5,)
         assert unpack("<d", '\x00\x00\x00\x00\x00\x00)@') == (12.5,)
-        XXX # "f"
+        assert pack("!f", -12.5) == '\xc1H\x00\x00'
+        assert pack("<f", 12.5) == '\x00\x00HA'
+        assert unpack("!f", 'AH\x00\x00') == (12.5,)
+        assert unpack("<f", '\x00\x00H\xc1') == (-12.5,)
 
 
     def test_struct_error(self):



More information about the Pypy-commit mailing list