]> 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 04:22:36 +0000 (04:22 +0000)
committerFred Drake <fdrake@acm.org>
Fri, 3 Sep 2010 04:22:36 +0000 (04:22 +0000)
values (http://bugs.python.org/issue7005)
(merged r84443 from the release27-mmaint branch, with changes to reflect
changes in Python 3)

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

index eb29b02b19721a5d90c64c3e191c39ecf950c89c..fb39ac3343b07f600b545dae663eb793de4243eb 100644 (file)
@@ -637,7 +637,7 @@ class RawConfigParser:
         for key, value in section_items:
             if key == "__name__":
                 continue
-            if value is not None:
+            if (value is not None) or (self._optcre == self.OPTCRE):
                 value = delimiter + str(value).replace('\n', '\n\t')
             else:
                 value = ""
index f43d1d7faf340ee702a1c3fe067f5a53106b90a1..a20678dc2cf007237917be79cb935b3106608ee0 100644 (file)
@@ -755,6 +755,34 @@ class SafeConfigParserTestCaseTrickyFile(CfgParserTestCaseClass):
         with self.assertRaises(UnicodeDecodeError):
             cf.read(tricky, encoding='ascii')
 
+
+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 = io.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):
     dict_type = SortedDict
 
@@ -811,6 +839,7 @@ def test_main():
         SafeConfigParserTestCaseNoValue,
         SafeConfigParserTestCaseTrickyFile,
         SortedTestCase,
+        Issue7005TestCase,
         StrictTestCase,
         CompatibleTestCase,
         )
index ad6090ea643e6af4f5eee57bb0765d79a7af0ca4..4053dd76d33e5943d1c6015f0fc6a700b9425481 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -155,6 +155,9 @@ Extensions
 Library
 -------
 
+- Issue #7005: Fixed output of None values for RawConfigParser.write and
+  ConfigParser.write.
+
 - Issue #8990: array.fromstring() and array.tostring() get renamed to
   frombytes() and tobytes(), respectively, to avoid confusion.  Furthermore,
   array.frombytes(), array.extend() as well as the array.array()