mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
gh-66449: configparser: Add support for unnamed sections (#117273)
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
parent
d9cfe7e565
commit
54f7e14500
5 changed files with 172 additions and 31 deletions
|
@ -2115,6 +2115,54 @@ class BlatantOverrideConvertersTestCase(unittest.TestCase):
|
|||
self.assertEqual(cfg['two'].getlen('one'), 5)
|
||||
|
||||
|
||||
class SectionlessTestCase(unittest.TestCase):
|
||||
|
||||
def fromstring(self, string):
|
||||
cfg = configparser.ConfigParser(allow_unnamed_section=True)
|
||||
cfg.read_string(string)
|
||||
return cfg
|
||||
|
||||
def test_no_first_section(self):
|
||||
cfg1 = self.fromstring("""
|
||||
a = 1
|
||||
b = 2
|
||||
[sect1]
|
||||
c = 3
|
||||
""")
|
||||
|
||||
self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg1.sections()))
|
||||
self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a'])
|
||||
self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b'])
|
||||
self.assertEqual('3', cfg1['sect1']['c'])
|
||||
|
||||
output = io.StringIO()
|
||||
cfg1.write(output)
|
||||
cfg2 = self.fromstring(output.getvalue())
|
||||
|
||||
#self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg2.sections()))
|
||||
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
|
||||
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
|
||||
self.assertEqual('3', cfg2['sect1']['c'])
|
||||
|
||||
def test_no_section(self):
|
||||
cfg1 = self.fromstring("""
|
||||
a = 1
|
||||
b = 2
|
||||
""")
|
||||
|
||||
self.assertEqual([configparser.UNNAMED_SECTION], cfg1.sections())
|
||||
self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a'])
|
||||
self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b'])
|
||||
|
||||
output = io.StringIO()
|
||||
cfg1.write(output)
|
||||
cfg2 = self.fromstring(output.getvalue())
|
||||
|
||||
self.assertEqual([configparser.UNNAMED_SECTION], cfg2.sections())
|
||||
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
|
||||
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
|
||||
|
||||
|
||||
class MiscTestCase(unittest.TestCase):
|
||||
def test__all__(self):
|
||||
support.check__all__(self, configparser, not_exported={"Error"})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue