Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,12 @@ 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)
if st.sectname in self._sections:
st.cursect = self._sections[st.sectname]
else:
st.cursect = self._dict()
self._sections[st.sectname] = st.cursect
self._proxies[st.sectname] = SectionProxy(self, st.sectname)

st.indent_level = st.cur_indent_level
# is it a section header?
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,15 @@ def test_disabled_error(self):
with self.assertRaises(configparser.UnnamedSectionDisabledError):
configparser.ConfigParser().add_section(configparser.UNNAMED_SECTION)

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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Do not recreate unnamed section on every read in
:class:`configparser.ConfigParser`. Patch by Andrey Efremov.
Loading