diff options
author | R. David Murray <rdmurray@bitdance.com> | 2009-04-01 03:21:43 (GMT) |
---|---|---|
committer | R. David Murray <rdmurray@bitdance.com> | 2009-04-01 03:21:43 (GMT) |
commit | a83da3507f6f6075cce143cb118d3ddb23df981c (patch) | |
tree | 75e643486900e3c7547e16c70b92dd98fbf346e9 /Lib/locale.py | |
parent | bb94d43dcdbfddff40f23944f0e4079f41a30421 (diff) | |
download | cpython-a83da3507f6f6075cce143cb118d3ddb23df981c.zip cpython-a83da3507f6f6075cce143cb118d3ddb23df981c.tar.gz cpython-a83da3507f6f6075cce143cb118d3ddb23df981c.tar.bz2 |
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.
Diffstat (limited to 'Lib/locale.py')
-rw-r--r-- | Lib/locale.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/Lib/locale.py b/Lib/locale.py index 163b044..777bb03 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -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 @@ 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. |