From 024d343642cee6dd91834b5c48c027b2026eff2b Mon Sep 17 00:00:00 2001 From: Andrey Efremov Date: Wed, 5 Feb 2025 15:22:18 +0700 Subject: [PATCH 1/2] ConfigParser: do not write an empty unnamed section --- Lib/configparser.py | 2 +- Lib/test/test_configparser.py | 7 +++++++ .../Library/2025-02-05-15-17-31.gh-issue-129678.GIUrmV.rst | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-05-15-17-31.gh-issue-129678.GIUrmV.rst diff --git a/Lib/configparser.py b/Lib/configparser.py index 9dc4fa515cfcbe..9ff52e03c2c458 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -959,7 +959,7 @@ def write(self, fp, space_around_delimiters=True): if self._defaults: self._write_section(fp, self.default_section, self._defaults.items(), d) - if UNNAMED_SECTION in self._sections: + 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: diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index bde805eb741c33..40413de22efaed 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -2161,6 +2161,13 @@ def test_no_section(self): self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a']) self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b']) + def test_empty_unnamed_section(self): + expected_output = '[sect]\n\n' + cfg = self.fromstring(expected_output) + output = io.StringIO() + cfg.write(output) + self.assertEqual(output.getvalue(), expected_output) + def test_add_section(self): cfg = configparser.ConfigParser(allow_unnamed_section=True) cfg.add_section(configparser.UNNAMED_SECTION) diff --git a/Misc/NEWS.d/next/Library/2025-02-05-15-17-31.gh-issue-129678.GIUrmV.rst b/Misc/NEWS.d/next/Library/2025-02-05-15-17-31.gh-issue-129678.GIUrmV.rst new file mode 100644 index 00000000000000..5c91a0f99e88bd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-05-15-17-31.gh-issue-129678.GIUrmV.rst @@ -0,0 +1 @@ +:class:`configparser.ConfigParser`: do not write an empty unnamed section From c3a47b3b2ebc6a2583754a5a6858732e139439e1 Mon Sep 17 00:00:00 2001 From: Andrey Efremov Date: Mon, 10 Feb 2025 12:29:06 +0700 Subject: [PATCH 2/2] A more reliable test of an empty unnamed section --- Lib/test/test_configparser.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 40413de22efaed..c2c82ebe6a87aa 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -2162,11 +2162,12 @@ def test_no_section(self): self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b']) def test_empty_unnamed_section(self): - expected_output = '[sect]\n\n' - cfg = self.fromstring(expected_output) + cfg = configparser.ConfigParser(allow_unnamed_section=True) + cfg.add_section(configparser.UNNAMED_SECTION) + cfg.add_section('section') output = io.StringIO() cfg.write(output) - self.assertEqual(output.getvalue(), expected_output) + self.assertEqual(output.getvalue(), '[section]\n\n') def test_add_section(self): cfg = configparser.ConfigParser(allow_unnamed_section=True)