]> granicus.if.org Git - python/commitdiff
Fix issue 2522. locale.format now checks that it is passed
authorR. David Murray <rdmurray@bitdance.com>
Wed, 1 Apr 2009 03:21:43 +0000 (03:21 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Wed, 1 Apr 2009 03:21:43 +0000 (03:21 +0000)
exactly one pattern, which avoids mysterious errors where it
had seemed to fail to do localization.

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

index 163b0440e65f7431ea9aa720fb2dad5c98399f87..777bb03f4fba0203cd710aa87c4cde89743eea84 100644 (file)
 
 """
 
-import sys, encodings, encodings.aliases
+import sys
+import encodings
+import encodings.aliases
+import re
+import operator
 import functools
 
 # Try importing the _locale module.
@@ -166,6 +170,9 @@ def _strip_padding(s, amount):
         amount -= 1
     return s[lpos:rpos+1]
 
+_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
+                         r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
+
 def format(percent, value, grouping=False, monetary=False, *additional):
     """Returns the locale-aware substitution of a %? specifier
     (percent).
@@ -173,9 +180,13 @@ def format(percent, value, grouping=False, monetary=False, *additional):
     additional is for format strings which contain one or more
     '*' modifiers."""
     # this is only for one-percent-specifier strings and this should be checked
-    if percent[0] != '%':
-        raise ValueError("format() must be given exactly one %char "
-                         "format specifier")
+    match = _percent_re.match(percent)
+    if not match or len(match.group())!= len(percent):
+        raise ValueError(("format() must be given exactly one %%char "
+                         "format specifier, %s not valid") % repr(percent))
+    return _format(percent, value, grouping, monetary, *additional)
+
+def _format(percent, value, grouping=False, monetary=False, *additional):
     if additional:
         formatted = percent % ((value,) + additional)
     else:
@@ -199,10 +210,6 @@ def format(percent, value, grouping=False, monetary=False, *additional):
             formatted = _strip_padding(formatted, seps)
     return formatted
 
-import re, operator
-_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
-                         r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
-
 def format_string(f, val, grouping=False):
     """Formats a string in the same way that the % formatting would use,
     but takes the current locale into account.
index 180f4030dc27fc81ed480013e7d4354c5b154f96..d9d3cd95dc89efc18ff1e1fc586ecda9deb6fb3c 100644 (file)
@@ -221,6 +221,19 @@ class EnUSNumberFormatting(BaseFormattingTest):
                 (self.sep, self.sep))
 
 
+class TestFormatPatternArg(unittest.TestCase):
+    # Test handling of pattern argument of format
+
+    def test_onlyOnePattern(self):
+        # Issue 2522: accept exactly one % pattern, and no extra chars.
+        self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
+        self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
+        self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
+        self.assertRaises(ValueError, locale.format, " %f", 'foo')
+        self.assertRaises(ValueError, locale.format, "%fg", 'foo')
+        self.assertRaises(ValueError, locale.format, "%^g", 'foo')
+
+
 class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
     # Test number formatting with a real English locale.
 
@@ -351,6 +364,7 @@ class TestMiscellaneous(unittest.TestCase):
 def test_main():
     tests = [
         TestMiscellaneous,
+        TestFormatPatternArg,
         TestEnUSNumberFormatting,
         TestCNumberFormatting,
         TestFrFRNumberFormatting,
index b7cd25a75b139586b9245e2cb99bdee6c2e3b834..b5f656e6f4ce150a03ed0ff3f99833a68ff3f2be 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -480,6 +480,7 @@ Damien Miller
 Chad Miller
 Jay T. Miller
 Roman Milner
+Andrii V. Mishkovskyi 
 Dustin J. Mitchell
 Dom Mitchell
 Doug Moen
@@ -490,6 +491,7 @@ James A Morrison
 Sjoerd Mullender
 Sape Mullender
 Michael Muller
+R. David Murray
 Piotr Meyer
 John Nagle
 Takahiro Nakayama
index 6907c97853d310bfae2fa0ed53c9623f079960f1..d51a3601d2b8e92438433a4e868ee8713f7ae94f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -200,6 +200,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #2522: locale.format now checks its first argument to ensure it has
+  been passed only one pattern, avoiding mysterious errors where it appeared
+  that it was failing to do localization.
+
 - Issue #5583: Added optional Extensions in Distutils. Initial patch by Georg
   Brandl.