[Python-checkins] python/dist/src/Lib string.py,1.79,1.80

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Tue Sep 14 04:34:11 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18610

Modified Files:
	string.py 
Log Message:
Fix small bugs in Template code.

* The parameterization of "delimiter" was incomplete.
* safe_substitute's code for braced delimiters should only be executed
  when braced is not None.
* Invalid pattern group names now raise a ValueError.  Formerly, the
  convert code would fall off the end and improperly return None.

Beefed-up tests.

* Test delimiter override for all paths in substitute and safe_substitute.
* Alter unittest invocation to match other modules (now it itemizes the
  tests as they are run).



Index: string.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/string.py,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- string.py	13 Sep 2004 20:52:50 -0000	1.79
+++ string.py	14 Sep 2004 02:34:08 -0000	1.80
@@ -152,6 +152,7 @@
             mapping = _multimap(kws, args[0])
         else:
             mapping = args[0]
+        delimiter = self.delimiter[-1]
         # Helper function for .sub()
         def convert(mo):
             # Check the most common path first.
@@ -162,9 +163,10 @@
                 # fail if val is a Unicode containing non-ASCII characters.
                 return '%s' % val
             if mo.group('escaped') is not None:
-                return '$'
+                return delimiter
             if mo.group('invalid') is not None:
                 self._invalid(mo)
+            raise ValueError('Unrecognized named group in pattern', pattern)
         return self.pattern.sub(convert, self.template)
 
     def safe_substitute(self, *args, **kws):
@@ -176,6 +178,7 @@
             mapping = _multimap(kws, args[0])
         else:
             mapping = args[0]
+        delimiter = self.delimiter[-1]            
         # Helper function for .sub()
         def convert(mo):
             named = mo.group('named')
@@ -185,16 +188,18 @@
                     # will fail if val is a Unicode containing non-ASCII
                     return '%s' % mapping[named]
                 except KeyError:
-                    return '$' + named
+                    return delimiter + named
             braced = mo.group('braced')
-            try:
-                return '%s' % mapping[braced]
-            except KeyError:
-                return '${' + braced + '}'
+            if braced is not None:
+                try:
+                    return '%s' % mapping[braced]
+                except KeyError:
+                    return delimiter + '{' + braced + '}'
             if mo.group('escaped') is not None:
-                return '$'
+                return delimiter
             if mo.group('invalid') is not None:
                 self._invalid(mo)
+            raise ValueError('Unrecognized named group in pattern', pattern)
         return self.pattern.sub(convert, self.template)
 
 



More information about the Python-checkins mailing list