]> granicus.if.org Git - python/commitdiff
Issue #1672332: Fix unpickling of subnormal floats, which was raising
authorMark Dickinson <dickinsm@gmail.com>
Sat, 24 Jan 2009 16:40:29 +0000 (16:40 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sat, 24 Jan 2009 16:40:29 +0000 (16:40 +0000)
ValueError on some platforms as a result of the platform strtod setting
errno on underflow.

Lib/test/pickletester.py
Misc/NEWS
Modules/cPickle.c

index bd0a3a4813a5f7b525f6d16c3a8cfa87e3e6e148..e34d55b85d11272cfdc7b12c2dafea4fab982be8 100644 (file)
@@ -535,6 +535,16 @@ class AbstractPickleTests(unittest.TestCase):
             got = self.loads(p)
             self.assertEqual(n, got)
 
+    def test_float(self):
+        test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5,
+                       3.14, 263.44582062374053, 6.022e23, 1e30]
+        test_values = test_values + [-x for x in test_values]
+        for proto in protocols:
+            for value in test_values:
+                pickle = self.dumps(value, proto)
+                got = self.loads(pickle)
+                self.assertEqual(value, got)
+
     @run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
     def test_float_format(self):
         # make sure that floats are formatted locale independent
index 0a65732c1956b90daf440579295316c703771ef1..0b5f801ab4f90f80cd4651c1c0806dd0aecc15da 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -145,6 +145,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #1672332: fix unpickling of subnormal floats, which was
+  producing a ValueError on some platforms.
+
 - Issue #3881: Help Tcl to load even when started through the
   unreadable local symlink to "Program Files" on Vista.
 
index 7f836c31269b3d04ecf8a8e435db2ec070d9cffd..fb13ba18bc459ecc56a9df125a9d42624015ae47 100644 (file)
@@ -3465,7 +3465,8 @@ load_float(Unpicklerobject *self)
        errno = 0;
        d = PyOS_ascii_strtod(s, &endptr);
 
-       if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
+       if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
+           (endptr[0] != '\n') || (endptr[1] != '\0')) {
                PyErr_SetString(PyExc_ValueError,
                                "could not convert string to float");
                goto finally;