]> granicus.if.org Git - python/commitdiff
Issue #9551: Do not raise TypeError when setting the value to None for
authorFred Drake <fdrake@acm.org>
Tue, 10 Aug 2010 13:09:54 +0000 (13:09 +0000)
committerFred Drake <fdrake@acm.org>
Tue, 10 Aug 2010 13:09:54 +0000 (13:09 +0000)
SafeConfigParser instances constructed with allow_no_value == True.

Lib/ConfigParser.py
Lib/test/test_cfgparser.py
Misc/NEWS

index 3e2175f04950c4582c739d35cce16cf2f9830e42..d356a138a1396a739c3a8312a1f0651832fcbaab 100644 (file)
@@ -699,13 +699,13 @@ class SafeConfigParser(ConfigParser):
         if self._optcre is self.OPTCRE or value:
             if not isinstance(value, basestring):
                 raise TypeError("option values must be strings")
-        # check for bad percent signs:
-        # first, replace all "good" interpolations
-        tmp_value = value.replace('%%', '')
-        tmp_value = self._interpvar_re.sub('', tmp_value)
-        # then, check if there's a lone percent sign left
-        percent_index = tmp_value.find('%')
-        if percent_index != -1:
-            raise ValueError("invalid interpolation syntax in %r at "
-                             "position %d" % (value, percent_index))
+        if value is not None:
+            # check for bad percent signs:
+            # first, replace all "good" interpolations
+            tmp_value = value.replace('%%', '')
+            tmp_value = self._interpvar_re.sub('', tmp_value)
+            # then, check if there's a lone percent sign left
+            if '%' in tmp_value:
+                raise ValueError("invalid interpolation syntax in %r at "
+                                "position %d" % (value, tmp_value.find('%')))
         ConfigParser.set(self, section, option, value)
index 22a314d779bf82d9580d4db13461b6a59a3664a9..5417d247fd4b210536563f6533e063fc2b0aaf51 100644 (file)
@@ -357,6 +357,7 @@ class TestCaseBase(unittest.TestCase):
 
 class ConfigParserTestCase(TestCaseBase):
     config_class = ConfigParser.ConfigParser
+    allow_no_value = True
 
     def test_interpolation(self):
         rawval = {
@@ -397,6 +398,7 @@ class ConfigParserTestCase(TestCaseBase):
         cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
                                       '%(list)': '%(list)'})
         cf.set('non-string', 'string_with_interpolation', '%(list)s')
+        cf.set('non-string', 'no-value')
         self.assertEqual(cf.get('non-string', 'int', raw=True), 1)
         self.assertRaises(TypeError, cf.get, 'non-string', 'int')
         self.assertEqual(cf.get('non-string', 'list', raw=True),
@@ -409,6 +411,7 @@ class ConfigParserTestCase(TestCaseBase):
                                 raw=True), '%(list)s')
         self.assertRaises(ValueError, cf.get, 'non-string',
                           'string_with_interpolation', raw=False)
+        self.assertEqual(cf.get('non-string', 'no-value'), None)
 
 class MultilineValuesTestCase(TestCaseBase):
     config_class = ConfigParser.ConfigParser
index 6bf21ac20210dc480b7bcae1e00a8bdd9a50b948..b4909faa35087fb8ab417e13aba9054639dc1f41 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #9551: Don't raise TypeError when setting the value to None for
+  SafeConfigParser instances constructed with allow_no_value == True.
+
 - Issue #6915: Under Windows, os.listdir() didn't release the Global
   Interpreter Lock around all system calls.  Original patch by Ryan Kelly.