Commit 285c3c55 authored by Thomas Wouters's avatar Thomas Wouters

Fix bug in passing tuples to string.Template. All other values (with working

str() or repr()) would work, just not multi-value tuples. Probably not a
backport candidate, since it changes the behaviour of passing a
single-element tuple:

>>> string.Template("$foo").substitute(dict(foo=(1,)))

'(1,)'

versus

'1'
parent d562cca1
...@@ -161,7 +161,7 @@ class Template: ...@@ -161,7 +161,7 @@ class Template:
val = mapping[named] val = mapping[named]
# We use this idiom instead of str() because the latter will # We use this idiom instead of str() because the latter will
# fail if val is a Unicode containing non-ASCII characters. # fail if val is a Unicode containing non-ASCII characters.
return '%s' % val return '%s' % (val,)
if mo.group('escaped') is not None: if mo.group('escaped') is not None:
return self.delimiter return self.delimiter
if mo.group('invalid') is not None: if mo.group('invalid') is not None:
...@@ -186,13 +186,13 @@ class Template: ...@@ -186,13 +186,13 @@ class Template:
try: try:
# We use this idiom instead of str() because the latter # We use this idiom instead of str() because the latter
# will fail if val is a Unicode containing non-ASCII # will fail if val is a Unicode containing non-ASCII
return '%s' % mapping[named] return '%s' % (mapping[named],)
except KeyError: except KeyError:
return self.delimiter + named return self.delimiter + named
braced = mo.group('braced') braced = mo.group('braced')
if braced is not None: if braced is not None:
try: try:
return '%s' % mapping[braced] return '%s' % (mapping[braced],)
except KeyError: except KeyError:
return self.delimiter + '{' + braced + '}' return self.delimiter + '{' + braced + '}'
if mo.group('escaped') is not None: if mo.group('escaped') is not None:
......
...@@ -58,6 +58,13 @@ class TestTemplate(unittest.TestCase): ...@@ -58,6 +58,13 @@ class TestTemplate(unittest.TestCase):
s = Template('tim has eaten ${count} bags of ham today') s = Template('tim has eaten ${count} bags of ham today')
eq(s.substitute(d), 'tim has eaten 7 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): def test_SafeTemplate(self):
eq = self.assertEqual eq = self.assertEqual
s = Template('$who likes ${what} for ${meal}') s = Template('$who likes ${what} for ${meal}')
......
...@@ -25,6 +25,10 @@ Core and builtins ...@@ -25,6 +25,10 @@ Core and builtins
Library 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} - Bug #822974: Honor timeout in telnetlib.{expect,read_until}
even if some data are received. even if some data are received.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment