mirror of
https://github.com/python/cpython.git
synced 2026-01-26 21:03:34 +00:00
[3.14] gh-136282: Configparser: create unnamed sections via mapping protocol access (GH-136313) (GH-142978)
(cherry picked from commit 4aef13832521b4e7785c9643f6a995c92b4a691d) Co-authored-by: Rogdham <3994389+Rogdham@users.noreply.github.com>
This commit is contained in:
parent
7d52ea8978
commit
45da8f081d
@ -794,7 +794,8 @@ class RawConfigParser(MutableMapping):
|
||||
"""
|
||||
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):
|
||||
|
||||
@ -2215,6 +2215,16 @@ class SectionlessTestCase(unittest.TestCase):
|
||||
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):
|
||||
@ -2223,6 +2233,9 @@ class SectionlessTestCase(unittest.TestCase):
|
||||
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')
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
Add support for :const:`~configparser.UNNAMED_SECTION` when creating a
|
||||
section via the mapping protocol access
|
||||
Loading…
x
Reference in New Issue
Block a user