Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ def read_dict(self, dictionary, source='<dict>'):
"""
elements_added = set()
for section, keys in dictionary.items():
section = str(section)
if section is not UNNAMED_SECTION:
section = str(section)
try:
self.add_section(section)
except (DuplicateSectionError, ValueError):
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,16 @@ def test_add_section(self):
cfg.add_section(configparser.UNNAMED_SECTION)
cfg.set(configparser.UNNAMED_SECTION, 'a', '1')
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a'])
output = io.StringIO()
cfg.write(output)
self.assertEqual(output.getvalue(), 'a = 1\n\n')

cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg[configparser.UNNAMED_SECTION] = {'a': '1'}
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a'])
output = io.StringIO()
cfg.write(output)
self.assertEqual(output.getvalue(), 'a = 1\n\n')

def test_disabled_error(self):
with self.assertRaises(configparser.MissingSectionHeaderError):
Expand All @@ -2223,6 +2233,9 @@ def test_disabled_error(self):
with self.assertRaises(configparser.UnnamedSectionDisabledError):
configparser.ConfigParser().add_section(configparser.UNNAMED_SECTION)

with self.assertRaises(configparser.UnnamedSectionDisabledError):
configparser.ConfigParser()[configparser.UNNAMED_SECTION] = {'a': '1'}

def test_multiple_configs(self):
cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg.read_string('a = 1')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add support for :const:`~configparser.UNNAMED_SECTION` when creating a
section via the mapping protocol access
Loading