]> granicus.if.org Git - python/commitdiff
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
authorMark Dickinson <dickinsm@gmail.com>
Fri, 14 Mar 2008 14:23:37 +0000 (14:23 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Fri, 14 Mar 2008 14:23:37 +0000 (14:23 +0000)
across platforms:  it should now raise OverflowError on all
platforms.  (Previously it raised OverflowError only on
non IEEE 754 platforms.)

Also fix the (already existing) test for this behaviour
so that it actually raises TestFailed instead of just
referencing it.

Lib/test/test_struct.py
Misc/NEWS
Objects/floatobject.c

index d22611552772e291aa9db61a1fee6a2e2e658e83..3ede005c7acdcf269af658044a39f468df5ec7ad 100644 (file)
@@ -482,7 +482,7 @@ def test_705836():
     except OverflowError:
         pass
     else:
-        TestFailed("expected OverflowError")
+        raise TestFailed("expected OverflowError")
 
 test_705836()
 
index 65ce4c0b93c04be5203ac4e3413156d095e35172..3397f0c4439f4feccb81bdc3f4a9360606e9ce31 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,10 @@ Core and builtins
 Library
 -------
 
+- Issue #705836: struct.pack(">f", x) now raises OverflowError on all
+  platforms when x is too large to fit into an IEEE 754 float; previously
+  it only raised OverflowError on non IEEE 754 platforms.
+
 - Issue #1106316: pdb.post_mortem()'s parameter, "traceback", is now
   optional: it defaults to the traceback of the exception that is currently
   being handled (is mandatory to be in the middle of an exception, otherwise
index 392a03760931d528eb13ee0028d7077b44234465..0eaca0f7bea769784b033be9b7e1402cca1f6484 100644 (file)
@@ -1751,9 +1751,6 @@ PyFloat_Fini(void)
 
 /*----------------------------------------------------------------------------
  * _PyFloat_{Pack,Unpack}{4,8}.  See floatobject.h.
- *
- * TODO:  On platforms that use the standard IEEE-754 single and double
- * formats natively, these routines could simply copy the bytes.
  */
 int
 _PyFloat_Pack4(double x, unsigned char *p, int le)
@@ -1833,28 +1830,31 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
                /* Done */
                return 0;
 
-         Overflow:
-               PyErr_SetString(PyExc_OverflowError,
-                               "float too large to pack with f format");
-               return -1;
        }
        else {
                float y = (float)x;
                const char *s = (char*)&y;
                int i, incr = 1;
 
+               if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
+                       goto Overflow;
+
                if ((float_format == ieee_little_endian_format && !le)
                    || (float_format == ieee_big_endian_format && le)) {
                        p += 3;
                        incr = -1;
                }
-               
+
                for (i = 0; i < 4; i++) {
                        *p = *s++;
                        p += incr;
                }
                return 0;
        }
+  Overflow:
+       PyErr_SetString(PyExc_OverflowError,
+                       "float too large to pack with f format");
+       return -1;
 }
 
 int