summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/string.py6
-rw-r--r--Lib/test/test_pep292.py7
-rw-r--r--Misc/NEWS4
3 files changed, 14 insertions, 3 deletions
diff --git a/Lib/string.py b/Lib/string.py
index ba85a49835..a5837e94b8 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -161,7 +161,7 @@ class Template:
val = mapping[named]
# We use this idiom instead of str() because the latter will
# fail if val is a Unicode containing non-ASCII characters.
- return '%s' % val
+ return '%s' % (val,)
if mo.group('escaped') is not None:
return self.delimiter
if mo.group('invalid') is not None:
@@ -186,13 +186,13 @@ class Template:
try:
# We use this idiom instead of str() because the latter
# will fail if val is a Unicode containing non-ASCII
- return '%s' % mapping[named]
+ return '%s' % (mapping[named],)
except KeyError:
return self.delimiter + named
braced = mo.group('braced')
if braced is not None:
try:
- return '%s' % mapping[braced]
+ return '%s' % (mapping[braced],)
except KeyError:
return self.delimiter + '{' + braced + '}'
if mo.group('escaped') is not None:
diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py
index 2a4353adbd..d1100ea8f6 100644
--- a/Lib/test/test_pep292.py
+++ b/Lib/test/test_pep292.py
@@ -58,6 +58,13 @@ class TestTemplate(unittest.TestCase):
s = Template('tim has eaten ${count} bags of ham today')
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
+ def test_tupleargs(self):
+ eq = self.assertEqual
+ s = Template('$who ate ${meal}')
+ d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))
+ eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
+ eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
+
def test_SafeTemplate(self):
eq = self.assertEqual
s = Template('$who likes ${what} for ${meal}')
diff --git a/Misc/NEWS b/Misc/NEWS
index 34795a438e..e45bd1e19b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,10 @@ Core and builtins
Library
-------
+- string.Template() now correctly handles tuple-values. Previously,
+ multi-value tuples would raise an exception and single-value tuples would
+ be treated as the value they contain, instead.
+
- Bug #822974: Honor timeout in telnetlib.{expect,read_until}
even if some data are received.