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?
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,
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):