From: Jason R. Coombs Date: Sun, 2 Feb 2025 18:53:23 +0000 (-0500) Subject: [3.13] gh-127096: Do not recreate unnamed section on every read in ConfigParser ... X-Git-Tag: v3.13.2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6aa09a5898960b7a489503f42c2071e4c0736992;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-127096: Do not recreate unnamed section on every read in ConfigParser (GH-127228) (#129593) * Do not recreate unnamed section on every read in ConfigParser * Remove duplicate section creation code (cherry picked from commit 914c232e9391e8e5014b089ba12c75d4a3b0cc7f) Co-authored-by: Andrey Efremov --- diff --git a/Lib/configparser.py b/Lib/configparser.py index ff7d712bed4a..42d0ae1c0b52 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -1093,11 +1093,7 @@ class RawConfigParser(MutableMapping): def _handle_rest(self, st, line, fpname): # a section header or option header? if self._allow_unnamed_section and st.cursect is None: - st.sectname = UNNAMED_SECTION - st.cursect = self._dict() - self._sections[st.sectname] = st.cursect - self._proxies[st.sectname] = SectionProxy(self, st.sectname) - st.elements_added.add(st.sectname) + self._handle_header(st, UNNAMED_SECTION, fpname) st.indent_level = st.cur_indent_level # is it a section header? @@ -1106,10 +1102,10 @@ class RawConfigParser(MutableMapping): if not mo and st.cursect is None: raise MissingSectionHeaderError(fpname, st.lineno, line) - self._handle_header(st, mo, fpname) if mo else self._handle_option(st, line, fpname) + self._handle_header(st, mo.group('header'), fpname) if mo else self._handle_option(st, line, fpname) - def _handle_header(self, st, mo, fpname): - st.sectname = mo.group('header') + def _handle_header(self, st, sectname, fpname): + st.sectname = sectname if st.sectname in self._sections: if self._strict and st.sectname in st.elements_added: raise DuplicateSectionError(st.sectname, fpname, diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index a934e493a763..ab86d7f1af39 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -2161,6 +2161,15 @@ class SectionlessTestCase(unittest.TestCase): self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a']) self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b']) + def test_multiple_configs(self): + cfg = configparser.ConfigParser(allow_unnamed_section=True) + cfg.read_string('a = 1') + cfg.read_string('b = 2') + + self.assertEqual([configparser.UNNAMED_SECTION], cfg.sections()) + self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a']) + self.assertEqual('2', cfg[configparser.UNNAMED_SECTION]['b']) + class MiscTestCase(unittest.TestCase): def test__all__(self): diff --git a/Misc/NEWS.d/next/Library/2024-11-24-22-06-42.gh-issue-127096.R7LLpQ.rst b/Misc/NEWS.d/next/Library/2024-11-24-22-06-42.gh-issue-127096.R7LLpQ.rst new file mode 100644 index 000000000000..8619296143b7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-24-22-06-42.gh-issue-127096.R7LLpQ.rst @@ -0,0 +1,2 @@ +Do not recreate unnamed section on every read in +:class:`configparser.ConfigParser`. Patch by Andrey Efremov.