]> granicus.if.org Git - python/commitdiff
Merged revisions 80521 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Sun, 5 Sep 2010 22:40:41 +0000 (22:40 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Sun, 5 Sep 2010 22:40:41 +0000 (22:40 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r80521 | r.david.murray | 2010-04-26 22:45:53 -0400 (Mon, 26 Apr 2010) | 13 lines

  Merged revisions 80512 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r80512 | r.david.murray | 2010-04-26 17:17:14 -0400 (Mon, 26 Apr 2010) | 7 lines

    Issue #6656: fix locale.format_string to handle escaped percents and mappings.

    Refactors format_string.  Includes tests for the two problems noted in
    the issue, but as far as I can see there are no other tests that confirm
    that format_string conforms to normal % formatting rules.
  ........
................

Lib/locale.py
Lib/test/test_locale.py
Misc/NEWS

index f74207d4f07e9924fcd8dd603eb5d8bfa9023a19..8c44625e05f6bf461d109bef151f9cc8366413a0 100644 (file)
@@ -224,22 +224,30 @@ def format_string(f, val, grouping=False):
     percents = list(_percent_re.finditer(f))
     new_f = _percent_re.sub('%s', f)
 
-    if isinstance(val, tuple):
-        new_val = list(val)
-        i = 0
-        for perc in percents:
-            starcount = perc.group('modifiers').count('*')
-            new_val[i] = format(perc.group(), new_val[i], grouping, False, *new_val[i+1:i+1+starcount])
-            del new_val[i+1:i+1+starcount]
-            i += (1 + starcount)
-        val = tuple(new_val)
-    elif isinstance(val, collections.Mapping):
+    if isinstance(val, collections.Mapping):
+        new_val = []
         for perc in percents:
-            key = perc.group("key")
-            val[key] = format(perc.group(), val[key], grouping)
+            if perc.group()[-1]=='%':
+                new_val.append('%')
+            else:
+                new_val.append(format(perc.group(), val, grouping))
     else:
-        # val is a single value
-        val = format(percents[0].group(), val, grouping)
+        if not isinstance(val, tuple):
+            val = (val,)
+        new_val = []
+        i = 0
+        for perc in percents:
+            if perc.group()[-1]=='%':
+                new_val.append('%')
+            else:
+                starcount = perc.group('modifiers').count('*')
+                new_val.append(_format(perc.group(),
+                                      val[i],
+                                      grouping,
+                                      False,
+                                      *val[i+1:i+1+starcount]))
+                i += (1 + starcount)
+    val = tuple(new_val)
 
     return new_f % val
 
index 9439522fc90d0056b8ac8dafd91f27db7105d98b..6cdb67ba65d325b07afe8a2f589020d9b993e0c7 100644 (file)
@@ -236,6 +236,25 @@ class TestFormatPatternArg(unittest.TestCase):
         self.assertRaises(ValueError, locale.format, " %f", 'foo')
         self.assertRaises(ValueError, locale.format, "%fg", 'foo')
         self.assertRaises(ValueError, locale.format, "%^g", 'foo')
+        self.assertRaises(ValueError, locale.format, "%f%%", 'foo')
+
+
+class TestLocaleFormatString(unittest.TestCase):
+    """General tests on locale.format_string"""
+
+    def test_percent_escape(self):
+        self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)
+        self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),
+            '%d %f%%d' % (1, 1.0))
+        self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),
+            ('%(foo)s %%d' % {'foo': 'bar'}))
+
+    def test_mapping(self):
+        self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),
+            ('%(foo)s bing.' % {'foo': 'bar'}))
+        self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),
+            ('%(foo)s' % {'foo': 'bar'}))
+
 
 
 class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
@@ -377,6 +396,7 @@ def test_main():
     tests = [
         TestMiscellaneous,
         TestFormatPatternArg,
+        TestLocaleFormatString,
         TestEnUSNumberFormatting,
         TestCNumberFormatting,
         TestFrFRNumberFormatting,
index 2ff4ff44ebd3a311527ba35a6d70857b789741e1..dcec2e96d1239e923526eca22ca0b9c2a7f77066 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -105,6 +105,9 @@ C-API
 Library
 -------
 
+- Issue #6656: fix locale.format_string to handle escaped percents
+  and mappings.
+
 - Issue #1100562: Fix deep-copying of objects derived from the list and
   dict types.  Patch by Michele Orrù and Björn Lindqvist.