File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -694,6 +694,7 @@ def add_section(self, section):
694
694
695
695
if section in self ._sections :
696
696
raise DuplicateSectionError (section )
697
+
697
698
self ._sections [section ] = self ._dict ()
698
699
self ._proxies [section ] = SectionProxy (self , section )
699
700
@@ -1243,7 +1244,11 @@ def add_section(self, section):
1243
1244
"""Create a new section in the configuration. Extends
1244
1245
RawConfigParser.add_section by validating if the section name is
1245
1246
a string."""
1246
- self ._validate_value_types (section = section )
1247
+ if section is UNNAMED_SECTION :
1248
+ if not self ._allow_unnamed_section :
1249
+ raise ValueError ("Unnamed section not enabled" )
1250
+ else :
1251
+ self ._validate_value_types (section = section )
1247
1252
super ().add_section (section )
1248
1253
1249
1254
def _read_defaults (self , defaults ):
Original file line number Diff line number Diff line change @@ -2161,6 +2161,11 @@ def test_no_section(self):
2161
2161
self .assertEqual ('1' , cfg2 [configparser .UNNAMED_SECTION ]['a' ])
2162
2162
self .assertEqual ('2' , cfg2 [configparser .UNNAMED_SECTION ]['b' ])
2163
2163
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' ])
2164
2169
2165
2170
class MiscTestCase (unittest .TestCase ):
2166
2171
def test__all__ (self ):
You can’t perform that action at this time.
0 commit comments