[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 914c232e93)

Co-authored-by: Andrey Efremov <duxus@yandex.ru>
This commit is contained in:
Jason R. Coombs 2025-02-02 13:53:23 -05:00 committed by GitHub
parent 65f3432ac3
commit 6aa09a5898
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 8 deletions

View file

@ -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,