Skip to content

Commit 450c6c1

Browse files
committed
Add support to unnamed section on RawConfigParser.add_section
May fix #123049.
1 parent e2f2dc7 commit 450c6c1

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

Lib/configparser.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,10 @@ def add_section(self, section):
692692
if section == self.default_section:
693693
raise ValueError('Invalid section name: %r' % section)
694694

695+
if section is UNNAMED_SECTION:
696+
if not self._allow_unnamed_section:
697+
raise ValueError("Unnamed section not enabled")
698+
695699
if section in self._sections:
696700
raise DuplicateSectionError(section)
697701
self._sections[section] = self._dict()
@@ -1203,20 +1207,21 @@ def _convert_to_boolean(self, value):
12031207
return self.BOOLEAN_STATES[value.lower()]
12041208

12051209
def _validate_value_types(self, *, section="", option="", value=""):
1206-
"""Raises a TypeError for non-string values.
1210+
"""Raises a TypeError for incorrect non-string values.
12071211
1208-
The only legal non-string value if we allow valueless
1209-
options is None, so we need to check if the value is a
1210-
string if:
1211-
- we do not allow valueless options, or
1212-
- we allow valueless options but the value is not None
1212+
Legal non-string values are UNNAMED_SECTION and valueless values if
1213+
they are are allowed.
12131214
12141215
For compatibility reasons this method is not used in classic set()
12151216
for RawConfigParsers. It is invoked in every case for mapping protocol
12161217
access and in ConfigParser.set().
12171218
"""
1218-
if not isinstance(section, str):
1219-
raise TypeError("section names must be strings")
1219+
if section is UNNAMED_SECTION:
1220+
if not self._allow_unnamed_section:
1221+
raise ValueError("UNNAMED_SECTION is not allowed")
1222+
else:
1223+
if not isinstance(section, str):
1224+
raise TypeError("section names must be strings or UNNAMED_SECTION")
12201225
if not isinstance(option, str):
12211226
raise TypeError("option keys must be strings")
12221227
if not self._allow_no_value or value:

Lib/test/test_configparser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,11 @@ def test_no_section(self):
21612161
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
21622162
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
21632163

2164+
def test_add_section(self):
2165+
cfg = configparser.ConfigParser(allow_unnamed_section=True)
2166+
cfg.add_section(configparser.UNNAMED_SECTION)
2167+
cfg.set(configparser.UNNAMED_SECTION, 'a', '1')
2168+
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a'])
21642169

21652170
class MiscTestCase(unittest.TestCase):
21662171
def test__all__(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for the unnamed section in :meth:`RawConfigParser.add_section`.

0 commit comments

Comments
 (0)