8000 gh-129678: ConfigParser: do not write an empty unnamed section (GH-12… · python/cpython@ef8eeca · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit ef8eeca

Browse files
gh-129678: ConfigParser: do not write an empty unnamed section (GH-129679)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent b9d2ee6 commit ef8eeca

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/configparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ def write(self, fp, space_around_delimiters=True):
959959
if self._defaults:
960960
self._write_section(fp, self.default_section,
961961
self._defaults.items(), d)
962-
if UNNAMED_SECTION in self._sections:
962+
if UNNAMED_SECTION in self._sections and self._sections[UNNAMED_SECTION]:
963963
self._write_section(fp, UNNAMED_SECTION, self._sections[UNNAMED_SECTION].items(), d, unnamed=True)
964964

965965
for section in self._sections:

Lib/test/test_configparser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,14 @@ def test_no_section(self):
21612161
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
21622162
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
21632163

2164+
def test_empty_unnamed_section(self):
2165+
cfg = configparser.ConfigParser(allow_unnamed_section=True)
2166+
cfg.add_section(configparser.UNNAMED_SECTION)
2167+
cfg.add_section('section')
2168+
output = io.StringIO()
2169+
cfg.write(output)
2170+
self.assertEqual(output.getvalue(), '[section]\n\n')
2171+
21642172
def test_add_section(self):
21652173
cfg = configparser.ConfigParser(allow_unnamed_section=True)
21662174
cfg.add_section(configparser.UNNAMED_SECTION)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`configparser.ConfigParser`: do not write an empty unnamed section

0 commit comments

Comments
 (0)
0