]> granicus.if.org Git - python/commitdiff
#5741: dont disallow double percent signs in SafeConfigParser.set() keys.
authorGeorg Brandl <georg@python.org>
Sun, 12 Apr 2009 17:24:11 +0000 (17:24 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 12 Apr 2009 17:24:11 +0000 (17:24 +0000)
Lib/ConfigParser.py
Lib/test/test_cfgparser.py
Misc/NEWS

index 1861b5aff91247e17023a576a8050dcba5027d61..2fa323b5206fec3cdc5f8452b487821e5be61a81 100644 (file)
@@ -620,7 +620,6 @@ class SafeConfigParser(ConfigParser):
         return ''.join(L)
 
     _interpvar_re = re.compile(r"%\(([^)]+)\)s")
-    _badpercent_re = re.compile(r"%[^%]|%$")
 
     def _interpolate_some(self, option, accum, rest, section, map, depth):
         if depth > MAX_INTERPOLATION_DEPTH:
@@ -667,9 +666,10 @@ class SafeConfigParser(ConfigParser):
         # check for bad percent signs:
         # first, replace all "good" interpolations
         tmp_value = self._interpvar_re.sub('', value)
+        tmp_value = tmp_value.replace('%%', '')
         # then, check if there's a lone percent sign left
-        m = self._badpercent_re.search(tmp_value)
-        if m:
+        percent_index = tmp_value.find('%')
+        if percent_index != -1:
             raise ValueError("invalid interpolation syntax in %r at "
-                             "position %d" % (value, m.start()))
+                             "position %d" % (value, percent_index))
         ConfigParser.set(self, section, option, value)
index 600d2dc3c78cecef2bef910f3810211a12a7a08e..a0d80513d5190dcac8859d178ca3166d622f3bd0 100644 (file)
@@ -434,6 +434,10 @@ class SafeConfigParserTestCase(ConfigParserTestCase):
 
         self.assertEqual(cf.get('sect', "option1"), "foo")
 
+        # bug #5741: double percents are *not* malformed
+        cf.set("sect", "option2", "foo%%bar")
+        self.assertEqual(cf.get("sect", "option2"), "foo%bar")
+
     def test_set_nonstring_types(self):
         cf = self.fromstring("[sect]\n"
                              "option1=foo\n")
index 2112a807335a8e49c446f782f5f13d83db24f319..35f3d3f2ccb68dbd282b7eac63e9e902772ee76f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -221,6 +221,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5741: don't disallow "%%" (which is an escape for "%") when setting
+  a value in SafeConfigParser.
+
 - Issue #5732: added a new command in Distutils: check.
 
 - Issue #5731: Distutils bdist_wininst no longer worked on non-Windows