@@ -692,6 +692,10 @@ def add_section(self, section):
692
692
if section == self .default_section :
693
693
raise ValueError ('Invalid section name: %r' % section )
694
694
695
+ if section is UNNAMED_SECTION :
696
+ if not self ._allow_unnamed_section :
697
+ raise ValueError ("Unnamed section not enabled" )
698
+
695
699
if section in self ._sections :
696
700
raise DuplicateSectionError (section )
697
701
self ._sections [section ] = self ._dict ()
@@ -1203,20 +1207,21 @@ def _convert_to_boolean(self, value):
1203
1207
return self .BOOLEAN_STATES [value .lower ()]
1204
1208
1205
1209
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.
1207
1211
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.
1213
1214
1214
1215
For compatibility reasons this method is not used in classic set()
1215
1216
for RawConfigParsers. It is invoked in every case for mapping protocol
1216
1217
access and in ConfigParser.set().
1217
1218
"""
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" )
1220
1225
if not isinstance (option , str ):
1221
1226
raise TypeError ("option keys must be strings" )
1222
1227
if not self ._allow_no_value or value :
0 commit comments