summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-04-26 21:17:14 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-04-26 21:17:14 (GMT)
commit3939dcdb72e29aeac43c0c6bcba69e098ed516ee (patch)
tree07c54da7d72f86513018e84f082ffe4231897d19
parent278d665c6a407ca1c1b26009b5a51f341f64a027 (diff)
downloadcpython-3939dcdb72e29aeac43c0c6bcba69e098ed516ee.zip
cpython-3939dcdb72e29aeac43c0c6bcba69e098ed516ee.tar.gz
cpython-3939dcdb72e29aeac43c0c6bcba69e098ed516ee.tar.bz2
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.
-rw-r--r--Lib/locale.py36
-rw-r--r--Lib/test/test_locale.py20
-rw-r--r--Misc/NEWS3
3 files changed, 45 insertions, 14 deletions
diff --git a/Lib/locale.py b/Lib/locale.py
index b156066..921f4f6 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -220,22 +220,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 operator.isMappingType(val):
+ if operator.isMappingType(val):
+ 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
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index 7f0f61d..8bb99cb 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -238,6 +238,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):
@@ -382,6 +401,7 @@ def test_main():
tests = [
TestMiscellaneous,
TestFormatPatternArg,
+ TestLocaleFormatString,
TestEnUSNumberFormatting,
TestCNumberFormatting,
TestFrFRNumberFormatting,
diff --git a/Misc/NEWS b/Misc/NEWS
index db6ca01..77fc78f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
Library
-------
+- Issue #6656: fix locale.format_string to handle escaped percents
+ and mappings.
+
- Issue #2302: Fix a race condition in SocketServer.BaseServer.shutdown,
where the method could block indefinitely if called just before the
event loop started running. This also fixes the occasional freezes