[pypy-svn] pypy default: Fix format('n') to correctly use the thousands grouping indicated by the locale

amauryfa commits-noreply at bitbucket.org
Mon Feb 7 22:37:56 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41688:a32d795de509
Date: 2011-02-07 22:37 +0100
http://bitbucket.org/pypy/pypy/changeset/a32d795de509/

Log:	Fix format('n') to correctly use the thousands grouping indicated by
	the locale

diff --git a/pypy/objspace/std/newformat.py b/pypy/objspace/std/newformat.py
--- a/pypy/objspace/std/newformat.py
+++ b/pypy/objspace/std/newformat.py
@@ -863,6 +863,7 @@
             msg = "alternate form not allowed in float formats"
             raise OperationError(space.w_ValueError, space.wrap(msg))
         tp = self._type
+        self._get_locale(tp)
         if tp == "\0":
             tp = "g"
             default_precision = 12
@@ -892,7 +893,6 @@
             to_number = 0
         have_dec_point, to_remainder = self._parse_number(result, to_number)
         n_remainder = len(result) - to_remainder
-        self._get_locale(tp)
         if self.is_unicode:
             digits = result.decode("ascii")
         else:

diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py
--- a/pypy/objspace/std/test/test_newformat.py
+++ b/pypy/objspace/std/test/test_newformat.py
@@ -1,5 +1,6 @@
 """Test unicode/str's format method"""
 from __future__ import with_statement
+from pypy.conftest import gettestobjspace
 
 
 class BaseStringFormatTests:
@@ -291,6 +292,8 @@
 
 
 class AppTestFloatFormatting:
+    def setup_class(cls):
+        cls.space = gettestobjspace(usemodules=('_locale',))
 
     def test_alternate(self):
         raises(ValueError, format, 1.0, "#")
@@ -313,6 +316,16 @@
     def test_digit_separator(self):
         assert format(-1234., "012,f") == "-1,234.000000"
 
+    def test_locale(self):
+        import locale
+        locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF8')
+        x = 1234.567890
+        try:
+            assert locale.format('%g', x, grouping=True) == '1,234.57'
+            assert format(x, 'n') == '1,234.57'
+        finally:
+            locale.setlocale(locale.LC_NUMERIC, 'C')
+
     def test_dont_switch_to_g(self):
         skip("must fix when float formatting is figured out")
         assert len(format(1.1234e90, "f")) == 98


More information about the Pypy-commit mailing list