]> granicus.if.org Git - python/commitdiff
fix output from RawConfigParser.write and ConfigParser.write for None
authorFred Drake <fdrake@acm.org>
Fri, 3 Sep 2010 03:55:50 +0000 (03:55 +0000)
committerFred Drake <fdrake@acm.org>
Fri, 3 Sep 2010 03:55:50 +0000 (03:55 +0000)
values (http://bugs.python.org/issue7005)

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

index d356a138a1396a739c3a8312a1f0651832fcbaab..dc7398841bc34e67d918d2c83f6f2da52c1e3de2 100644 (file)
@@ -400,7 +400,7 @@ class RawConfigParser:
             for (key, value) in self._sections[section].items():
                 if key == "__name__":
                     continue
-                if value is not None:
+                if (value is not None) or (self._optcre == self.OPTCRE):
                     key = " = ".join((key, str(value).replace('\n', '\n\t')))
                 fp.write("%s\n" % (key))
             fp.write("\n")
index 5417d247fd4b210536563f6533e063fc2b0aaf51..20c10dce88f079aba1ddd90d81a8d59e14b298dc 100644 (file)
@@ -530,6 +530,33 @@ class SafeConfigParserTestCaseNoValue(SafeConfigParserTestCase):
     allow_no_value = True
 
 
+class Issue7005TestCase(unittest.TestCase):
+    """Test output when None is set() as a value and allow_no_value == False.
+
+    http://bugs.python.org/issue7005
+
+    """
+
+    expected_output = "[section]\noption = None\n\n"
+
+    def prepare(self, config_class):
+        # This is the default, but that's the point.
+        cp = config_class(allow_no_value=False)
+        cp.add_section("section")
+        cp.set("section", "option", None)
+        sio = StringIO.StringIO()
+        cp.write(sio)
+        return sio.getvalue()
+
+    def test_none_as_value_stringified(self):
+        output = self.prepare(ConfigParser.ConfigParser)
+        self.assertEqual(output, self.expected_output)
+
+    def test_none_as_value_stringified_raw(self):
+        output = self.prepare(ConfigParser.RawConfigParser)
+        self.assertEqual(output, self.expected_output)
+
+
 class SortedTestCase(RawConfigParserTestCase):
     def newconfig(self, defaults=None):
         self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
@@ -563,6 +590,7 @@ def test_main():
         SafeConfigParserTestCase,
         SafeConfigParserTestCaseNoValue,
         SortedTestCase,
+        Issue7005TestCase,
         )
 
 
index e1ff17a293f190d16f5fae69e31f0eb6bc0927a3..3172e4983b50c97a7281993472ddf007c654117c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7005: Fixed output of None values for RawConfigParser.write and
+  ConfigParser.write.
+
 - Issue #808164: Fixed socket.close to avoid references to globals, to
   avoid issues when socket.close is called from a __del__ method.