[Python-checkins] cpython: Issue #13918: Provide a locale.delocalize() function which can remove

antoine.pitrou python-checkins at python.org
Thu Oct 23 22:52:54 CEST 2014


https://hg.python.org/cpython/rev/aee097e5a2b2
changeset:   93164:aee097e5a2b2
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Thu Oct 23 22:52:31 2014 +0200
summary:
  Issue #13918: Provide a locale.delocalize() function which can remove
locale-specific number formatting from a string representing a number,
without then converting it to a specific type.  Patch by Cédric Krier.

files:
  Doc/library/locale.rst  |   8 ++++
  Lib/locale.py           |  15 ++++---
  Lib/test/test_locale.py |  54 +++++++++++++++++++++++++++++
  Misc/NEWS               |   4 ++
  4 files changed, 75 insertions(+), 6 deletions(-)


diff --git a/Doc/library/locale.rst b/Doc/library/locale.rst
--- a/Doc/library/locale.rst
+++ b/Doc/library/locale.rst
@@ -387,6 +387,14 @@
    ``str(float)``, but takes the decimal point into account.
 
 
+.. function:: delocalize(string)
+
+    Converts a string into a normalized number string, following the
+    :const:'LC_NUMERIC`settings.
+
+    .. versionadded:: 3.5
+
+
 .. function:: atof(string)
 
    Converts a string to a floating point number, following the :const:`LC_NUMERIC`
diff --git a/Lib/locale.py b/Lib/locale.py
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -301,8 +301,8 @@
     """Convert float to integer, taking the locale into account."""
     return format("%.12g", val)
 
-def atof(string, func=float):
-    "Parses a string as a float according to the locale settings."
+def delocalize(string):
+    "Parses a string as a normalized number according to the locale settings."
     #First, get rid of the grouping
     ts = localeconv()['thousands_sep']
     if ts:
@@ -311,12 +311,15 @@
     dd = localeconv()['decimal_point']
     if dd:
         string = string.replace(dd, '.')
-    #finally, parse the string
-    return func(string)
+    return string
 
-def atoi(str):
+def atof(string, func=float):
+    "Parses a string as a float according to the locale settings."
+    return func(delocalize(string))
+
+def atoi(string):
     "Converts a string to an integer according to the locale settings."
-    return atof(str, int)
+    return int(delocalize(string))
 
 def _test():
     setlocale(LC_ALL, "")
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -524,5 +524,59 @@
             locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
 
 
+class BaseDelocalizeTest(BaseLocalizedTest):
+
+    def _test_delocalize(self, value, out):
+        self.assertEqual(locale.delocalize(value), out)
+
+    def _test_atof(self, value, out):
+        self.assertEqual(locale.atof(value), out)
+
+    def _test_atoi(self, value, out):
+        self.assertEqual(locale.atoi(value), out)
+
+
+class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
+
+    def test_delocalize(self):
+        self._test_delocalize('50000.00', '50000.00')
+        self._test_delocalize('50,000.00', '50000.00')
+
+    def test_atof(self):
+        self._test_atof('50000.00', 50000.)
+        self._test_atof('50,000.00', 50000.)
+
+    def test_atoi(self):
+        self._test_atoi('50000', 50000)
+        self._test_atoi('50,000', 50000)
+
+
+class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
+
+    def test_delocalize(self):
+        self._test_delocalize('50000.00', '50000.00')
+
+    def test_atof(self):
+        self._test_atof('50000.00', 50000.)
+
+    def test_atoi(self):
+        self._test_atoi('50000', 50000)
+
+
+class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
+
+    def test_delocalize(self):
+        self._test_delocalize('50000,00', '50000.00')
+        self._test_delocalize('50 000,00', '50000.00')
+
+    def test_atof(self):
+        self._test_atof('50000,00', 50000.)
+        self._test_atof('50 000,00', 50000.)
+
+    def test_atoi(self):
+        self._test_atoi('50000', 50000)
+        self._test_atoi('50 000', 50000)
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -181,6 +181,10 @@
 Library
 -------
 
+- Issue #13918: Provide a locale.delocalize() function which can remove
+  locale-specific number formatting from a string representing a number,
+  without then converting it to a specific type.  Patch by Cédric Krier.
+
 - Issue #22676: Make the pickling of global objects which don't have a
   __module__ attribute less slow.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list