Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,11 +971,11 @@ def write(self, fp, space_around_delimiters=True):
d = " {} ".format(self._delimiters[0])
else:
d = self._delimiters[0]
if UNNAMED_SECTION in self._sections and self._sections[UNNAMED_SECTION]:
self._write_section(fp, UNNAMED_SECTION, self._sections[UNNAMED_SECTION].items(), d, unnamed=True)
if self._defaults:
self._write_section(fp, self.default_section,
self._defaults.items(), d)
if UNNAMED_SECTION in self._sections and self._sections[UNNAMED_SECTION]:
self._write_section(fp, UNNAMED_SECTION, self._sections[UNNAMED_SECTION].items(), d, unnamed=True)

for section in self._sections:
if section is UNNAMED_SECTION:
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,17 @@ def test_add_section(self):
cfg.write(output)
self.assertEqual(output.getvalue(), 'a = 1\n\n')

def test_write_roundtrip_with_default(self):
US = configparser.UNNAMED_SECTION
cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg.read_string('key1 = val1\n[DEFAULT]\ndkey = dval\n[sect1]\nkey2 = val2\n')
out = io.StringIO()
cfg.write(out)
cfg2 = configparser.ConfigParser(allow_unnamed_section=True)
cfg2.read_string(out.getvalue())
self.assertIn('key1', cfg2[US])
self.assertNotIn('key1', cfg2.defaults())

def test_disabled_error(self):
with self.assertRaises(configparser.MissingSectionHeaderError):
configparser.ConfigParser().read_string("a = 1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`configparser.ConfigParser.write` outputting unnamed-section
items after the ``[DEFAULT]`` header, causing them to be absorbed into
DEFAULT on re-read. Patch by tonghuaroot.
Loading