]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Bug #1417699: Reject locale-specific decimal point in float()
authorMartin v. Löwis <martin@v.loewis.de>
Mon, 3 Jul 2006 12:19:50 +0000 (12:19 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Mon, 3 Jul 2006 12:19:50 +0000 (12:19 +0000)
and atof().

Lib/test/test__locale.py
Lib/test/test_builtin.py
Misc/NEWS
Python/pystrtod.c

index 9799f89ff8cce095dc338fde3c672c2de8c6a6c8..ec59d7121b1d3c01169e4073d7f4443c589989d5 100644 (file)
@@ -113,6 +113,9 @@ class _LocaleTests(unittest.TestCase):
                                 "using eval('3.14') failed for %s" % loc)
             self.assertEquals(int(float('3.14') * 100), 314,
                                 "using float('3.14') failed for %s" % loc)
+            if localeconv()['decimal_point'] != '.':
+                self.assertRaises(ValueError, float,
+                                  localeconv()['decimal_point'].join(['1', '23']))
 
 def test_main():
     run_unittest(_LocaleTests)
index dcc565ac5586ce35a9f94be0a5e375715a35b785..bcb4424d0ecdbfcafd53bdfde07d4081a0914d2b 100644 (file)
@@ -558,13 +558,24 @@ class BuiltinTest(unittest.TestCase):
     @run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
     def test_float_with_comma(self):
         # set locale to something that doesn't use '.' for the decimal point
+        # float must not accept the locale specific decimal point but
+        # it still has to accept the normal python syntac
         import locale
         if not locale.localeconv()['decimal_point'] == ',':
             return
 
-        self.assertEqual(float("  3,14  "), 3.14)
-        self.assertEqual(float("  +3,14  "), 3.14)
-        self.assertEqual(float("  -3,14  "), -3.14)
+        self.assertEqual(float("  3.14  "), 3.14)
+        self.assertEqual(float("+3.14  "), 3.14)
+        self.assertEqual(float("-3.14  "), -3.14)
+        self.assertEqual(float(".14  "), .14)
+        self.assertEqual(float("3.  "), 3.0)
+        self.assertEqual(float("3.e3  "), 3000.0)
+        self.assertEqual(float("3.2e3  "), 3200.0)
+        self.assertEqual(float("2.5e-1  "), 0.25)
+        self.assertEqual(float("5e-1"), 0.5)
+        self.assertRaises(ValueError, float, "  3,14  ")
+        self.assertRaises(ValueError, float, "  +3,14  ")
+        self.assertRaises(ValueError, float, "  -3,14  ")
         self.assertRaises(ValueError, float, "  0x3.1  ")
         self.assertRaises(ValueError, float, "  -0x3.p-1  ")
         self.assertEqual(float("  25.e-1  "), 2.5)
index a5faa9b88d5e499dbdc9ad7ee8ee9b2e9b5f8de3..474010454a1a6ebaf5ad0eec024e1b6bb6d2a52a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.5 beta 2?
 Core and builtins
 -----------------
 
+- Bug #1417699: Reject locale-specific decimal point in float()
+  and atof().
+
 - Bug #1511381: codec_getstreamcodec() in codec.c is corrected to
   omit a default "error" argument for NULL pointer.  This allows
   the parser to take a codec from cjkcodecs again.
index e1c84ea03e0eb735841b3493a85b6e6d4690a2b1..6c19b45fd260070591307da151e8ab0639b7b8f8 100644 (file)
@@ -90,6 +90,13 @@ PyOS_ascii_strtod(const char *nptr, char **endptr)
                                p++;
                        end = p;
                }
+               else if (strncmp(p, decimal_point, decimal_point_len) == 0)
+               {
+                       /* Python bug #1417699 */
+                       *endptr = (char*)nptr;
+                       errno = EINVAL;
+                       return val;
+               }
                /* For the other cases, we need not convert the decimal point */
        }