[pypy-svn] r75999 - in pypy/branch/fast-forward/pypy: module/struct/test rlib/rstruct

benjamin at codespeak.net benjamin at codespeak.net
Wed Jul 7 20:34:00 CEST 2010


Author: benjamin
Date: Wed Jul  7 20:33:57 2010
New Revision: 75999

Modified:
   pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
   pypy/branch/fast-forward/pypy/rlib/rstruct/ieee.py
   pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py
Log:
complain when float is too large for 'f'

Modified: pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py	(original)
+++ pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py	Wed Jul  7 20:33:57 2010
@@ -260,7 +260,7 @@
         assert pack("<f", 12.5) == '\x00\x00HA'
         assert unpack("!f", 'AH\x00\x00') == (12.5,)
         assert unpack("<f", '\x00\x00H\xc1') == (-12.5,)
-
+        raises(self.struct.error, pack, "<f", 10e100)
 
     def test_struct_error(self):
         """

Modified: pypy/branch/fast-forward/pypy/rlib/rstruct/ieee.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rstruct/ieee.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rstruct/ieee.py	Wed Jul  7 20:33:57 2010
@@ -114,7 +114,7 @@
         # Raise on overflow (in some circumstances, may want to return
         # infinity instead).
         if exp >= MAX_EXP - MIN_EXP + 2:
-             raise OverflowError("float too large to pack in this format")
+             raise ValueError("float too large to pack in this format")
 
     # check constraints
     assert 0 <= mant < 1 << MANT_DIG - 1

Modified: pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py	Wed Jul  7 20:33:57 2010
@@ -57,10 +57,14 @@
         fmtiter.result.append('\x00')
 
 def make_float_packer(size):
-    return lambda fmtiter: ieee.pack_float(fmtiter.result,
-                                           fmtiter.accept_float_arg(),
-                                           size,
-                                           fmtiter.bigendian)
+    def packer(fmtiter):
+        fl = fmtiter.accept_float_arg()
+        try:
+            return ieee.pack_float(fmtiter.result, fl, size, fmtiter.bigendian)
+        except ValueError:
+            assert size == 4
+            raise StructError("float too large to pack with format f")
+    return packer
 
 # ____________________________________________________________
 
@@ -155,10 +159,11 @@
     fmtiter.appendobj(data[1:end])
 
 def make_float_unpacker(size):
-    return specialize.argtype(0)(
-        lambda fmtiter: fmtiter.appendobj(ieee.unpack_float(
-        fmtiter.read(size),
-        fmtiter.bigendian)))
+    @specialize.argtype(0)
+    def unpacker(fmtiter):
+        data = fmtiter.read(size)
+        fmtiter.appendobj(ieee.unpack_float(data, fmtiter.bigendian))
+    return unpacker
 
 # ____________________________________________________________
 



More information about the Pypy-commit mailing list