Skip to content

Commit 8844441

Browse files
committed
fix output from RawConfigParser.write and ConfigParser.write for None
values (http://bugs.python.org/issue7005) (merged r84443 from the release27-mmaint branch, with changes to reflect changes in Python 3)
1 parent af1e140 commit 8844441

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎Lib/configparser.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def _write_section(self, fp, section_name, section_items, delimiter):
637637
for key, value in section_items:
638638
if key == "__name__":
639639
continue
640-
if value is not None:
640+
if (value is not None) or (self._optcre == self.OPTCRE):
641641
value = delimiter + str(value).replace('\n', '\n\t')
642642
else:
643643
value = ""

‎Lib/test/test_cfgparser.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,34 @@ def test_unicode_failure(self):
755755
with self.assertRaises(UnicodeDecodeError):
756756
cf.read(tricky, encoding='ascii')
757757

758+
759+
class Issue7005TestCase(unittest.TestCase):
760+
"""Test output when None is set() as a value and allow_no_value == False.
761+
762+
http://bugs.python.org/issue7005
763+
764+
"""
765+
766+
expected_output = "[section]\noption = None\n\n"
767+
768+
def prepare(self, config_class):
769+
# This is the default, but that's the point.
770+
cp = config_class(allow_no_value=False)
771+
cp.add_section("section")
772+
cp.set("section", "option", None)
773+
sio = io.StringIO()
774+
cp.write(sio)
775+
return sio.getvalue()
776+
777+
def test_none_as_value_stringified(self):
778+
output = self.prepare(configparser.ConfigParser)
779+
self.assertEqual(output, self.expected_output)
780+
781+
def test_none_as_value_stringified_raw(self):
782+
output = self.prepare(configparser.RawConfigParser)
783+
self.assertEqual(output, self.expected_output)
784+
785+
758786
class SortedTestCase(RawConfigParserTestCase):
759787
dict_type = SortedDict
760788

@@ -811,6 +839,7 @@ def test_main():
811839
SafeConfigParserTestCaseNoValue,
812840
SafeConfigParserTestCaseTrickyFile,
813841
SortedTestCase,
842+
Issue7005TestCase,
814843
StrictTestCase,
815844
CompatibleTestCase,
816845
)

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ Extensions
155155
Library
156156
-------
157157

158+
- Issue #7005: Fixed output of None values for RawConfigParser.write and
159+
ConfigParser.write.
160+
158161
- Issue #8990: array.fromstring() and array.tostring() get renamed to
159162
frombytes() and tobytes(), respectively, to avoid confusion. Furthermore,
160163
array.frombytes(), array.extend() as well as the array.array()

0 commit comments

Comments
 (0)