[Python-checkins] r47237 - in python/trunk: Lib/string.py Lib/test/test_pep292.py Misc/NEWS

thomas.wouters python-checkins at python.org
Wed Jul 5 13:03:50 CEST 2006


Author: thomas.wouters
Date: Wed Jul  5 13:03:49 2006
New Revision: 47237

Modified:
   python/trunk/Lib/string.py
   python/trunk/Lib/test/test_pep292.py
   python/trunk/Misc/NEWS
Log:

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'



Modified: python/trunk/Lib/string.py
==============================================================================
--- python/trunk/Lib/string.py	(original)
+++ python/trunk/Lib/string.py	Wed Jul  5 13:03:49 2006
@@ -161,7 +161,7 @@
                 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 @@
                 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:

Modified: python/trunk/Lib/test/test_pep292.py
==============================================================================
--- python/trunk/Lib/test/test_pep292.py	(original)
+++ python/trunk/Lib/test/test_pep292.py	Wed Jul  5 13:03:49 2006
@@ -58,6 +58,13 @@
         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}')

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Jul  5 13:03:49 2006
@@ -25,6 +25,10 @@
 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.
 


More information about the Python-checkins mailing list