[Python-checkins] r70936 - in python/trunk: Lib/locale.py Lib/test/test_locale.py Misc/ACKS Misc/NEWS

r.david.murray python-checkins at python.org
Wed Apr 1 05:21:43 CEST 2009


Author: r.david.murray
Date: Wed Apr  1 05:21:43 2009
New Revision: 70936

Log:
Fix issue 2522.  locale.format now checks that it is passed
exactly one pattern, which avoids mysterious errors where it
had seemed to fail to do localization.


Modified:
   python/trunk/Lib/locale.py
   python/trunk/Lib/test/test_locale.py
   python/trunk/Misc/ACKS
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/locale.py
==============================================================================
--- python/trunk/Lib/locale.py	(original)
+++ python/trunk/Lib/locale.py	Wed Apr  1 05:21:43 2009
@@ -11,7 +11,11 @@
 
 """
 
-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 @@
         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 @@
     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 @@
             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.

Modified: python/trunk/Lib/test/test_locale.py
==============================================================================
--- python/trunk/Lib/test/test_locale.py	(original)
+++ python/trunk/Lib/test/test_locale.py	Wed Apr  1 05:21:43 2009
@@ -221,6 +221,19 @@
                 (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 @@
 def test_main():
     tests = [
         TestMiscellaneous,
+        TestFormatPatternArg,
         TestEnUSNumberFormatting,
         TestCNumberFormatting,
         TestFrFRNumberFormatting,

Modified: python/trunk/Misc/ACKS
==============================================================================
--- python/trunk/Misc/ACKS	(original)
+++ python/trunk/Misc/ACKS	Wed Apr  1 05:21:43 2009
@@ -480,6 +480,7 @@
 Chad Miller
 Jay T. Miller
 Roman Milner
+Andrii V. Mishkovskyi 
 Dustin J. Mitchell
 Dom Mitchell
 Doug Moen
@@ -490,6 +491,7 @@
 Sjoerd Mullender
 Sape Mullender
 Michael Muller
+R. David Murray
 Piotr Meyer
 John Nagle
 Takahiro Nakayama

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Apr  1 05:21:43 2009
@@ -200,6 +200,10 @@
 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.
 


More information about the Python-checkins mailing list