Skip to content

Commit 6aa09a5

Browse files
jaracoPalmtopTiger
andauthored
[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 914c232) Co-authored-by: Andrey Efremov <[email protected]>
1 parent 65f3432 commit 6aa09a5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

Lib/configparser.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,11 +1093,7 @@ def _handle_continuation_line(self, st, line, fpname):
10931093
def _handle_rest(self, st, line, fpname):
10941094
# a section header or option header?
10951095
if self._allow_unnamed_section and st.cursect is None:
1096-
st.sectname = UNNAMED_SECTION
1097-
st.cursect = self._dict()
1098-
self._sections[st.sectname] = st.cursect
1099-
self._proxies[st.sectname] = SectionProxy(self, st.sectname)
1100-
st.elements_added.add(st.sectname)
1096+
self._handle_header(st, UNNAMED_SECTION, fpname)
11011097

11021098
st.indent_level = st.cur_indent_level
11031099
# is it a section header?
@@ -1106,10 +1102,10 @@ def _handle_rest(self, st, line, fpname):
11061102
if not mo and st.cursect is None:
11071103
raise MissingSectionHeaderError(fpname, st.lineno, line)
11081104

1109-
self._handle_header(st, mo, fpname) if mo else self._handle_option(st, line, fpname)
1105+
self._handle_header(st, mo.group('header'), fpname) if mo else self._handle_option(st, line, fpname)
11101106

1111-
def _handle_header(self, st, mo, fpname):
1112-
st.sectname = mo.group('header')
1107+
def _handle_header(self, st, sectname, fpname):
1108+
st.sectname = sectname
11131109
if st.sectname in self._sections:
11141110
if self._strict and st.sectname in st.elements_added:
11151111
raise DuplicateSectionError(st.sectname, fpname,

Lib/test/test_configparser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,15 @@ 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_multiple_configs(self):
2165+
cfg = configparser.ConfigParser(allow_unnamed_section=True)
2166+
cfg.read_string('a = 1')
2167+
cfg.read_string('b = 2')
2168+
2169+
self.assertEqual([configparser.UNNAMED_SECTION], cfg.sections())
2170+
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a'])
2171+
self.assertEqual('2', cfg[configparser.UNNAMED_SECTION]['b'])
2172+
21642173

21652174
class MiscTestCase(unittest.TestCase):
21662175
def test__all__(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Do not recreate unnamed section on every read in
2+
:class:`configparser.ConfigParser`. Patch by Andrey Efremov.

0 commit comments

Comments
 (0)