summaryrefslogtreecommitdiff
path: root/Lib/locale.py
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-04-27 02:45:53 +0000
committerR. David Murray <rdmurray@bitdance.com>2010-04-27 02:45:53 +0000
commitad78d15acc3db9918a115b7b7ed547ba815e3097 (patch)
tree1cf512e845342884e0dc187974d9be6e68b70a5d /Lib/locale.py
parent469b1f0d502756f049af8c26fca96bb85275a324 (diff)
downloadcpython-git-ad78d15acc3db9918a115b7b7ed547ba815e3097.tar.gz
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. ........
Diffstat (limited to 'Lib/locale.py')
-rw-r--r--Lib/locale.py36
1 files changed, 22 insertions, 14 deletions
diff --git a/Lib/locale.py b/Lib/locale.py
index 58e625f8ce..7b987bed06 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -227,22 +227,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