@@ -279,9 +279,9 @@ class InterpolationMissingOptionError(InterpolationError):
279
279
280
280
def __init__ (self , option , section , rawval , reference ):
281
281
msg = (
282
- "Bad value substitution: option {0 !r} in section {1 !r} contains "
283
- "an interpolation key {2 !r} which is not a valid option name. "
284
- "Raw value: {3 !r}" .format (option , section , reference , rawval )
282
+ "Bad value substitution: option {!r} in section {!r} contains "
283
+ "an interpolation key {!r} which is not a valid option name. "
284
+ "Raw value: {!r}" .format (option , section , reference , rawval )
285
285
)
286
286
InterpolationError .__init__ (self , option , section , msg )
287
287
self .reference = reference
@@ -301,9 +301,9 @@ class InterpolationDepthError(InterpolationError):
301
301
302
302
def __init__ (self , option , section , rawval ):
303
303
msg = (
304
- "Recursion limit exceeded in value substitution: option {0 !r} "
305
- "in section {1 !r} contains an interpolation key which "
306
- "cannot be substituted in {2 } steps. Raw value: {3 !r}"
304
+ "Recursion limit exceeded in value substitution: option {!r} "
305
+ "in section {!r} contains an interpolation key which "
306
+ "cannot be substituted in {} steps. Raw value: {!r}"
307
307
"" .format (option , section , MAX_INTERPOLATION_DEPTH , rawval )
308
308
)
309
309
InterpolationError .__init__ (self , option , section , msg )
@@ -377,7 +377,7 @@ def __init__(self, filename, lineno, line):
377
377
_UNSET = object ()
378
378
379
379
380
- class Interpolation ( object ) :
380
+ class Interpolation :
381
381
"""Dummy interpolation that passes the value through with no changes."""
382
382
383
383
def before_get (self , parser , section , option , value , defaults ):
@@ -650,19 +650,21 @@ class RawConfigParser(MutableMapping):
650
650
}
651
651
652
652
def __init__ (
653
- self , defaults = None , dict_type = _default_dict , allow_no_value = False , ** kwargs
653
+ self ,
654
+ defaults = None ,
655
+ dict_type = _default_dict ,
656
+ allow_no_value = False ,
657
+ * ,
658
+ delimiters = ('=' , ':' ),
659
+ comment_prefixes = ('#' , ';' ),
660
+ inline_comment_prefixes = None ,
661
+ strict = True ,
662
+ empty_lines_in_values = True ,
663
+ default_section = DEFAULTSECT ,
664
+ interpolation = _UNSET ,
665
+ converters = _UNSET ,
654
666
):
655
667
656
- # keyword-only arguments
657
- delimiters = kwargs .get ('delimiters' , ('=' , ':' ))
658
- comment_prefixes = kwargs .get ('comment_prefixes' , ('#' , ';' ))
659
- inline_comment_prefixes = kwargs .get ('inline_comment_prefixes' , None )
660
- strict = kwargs .get ('strict' , True )
661
- empty_lines_in_values = kwargs .get ('empty_lines_in_values' , True )
662
- default_section = kwargs .get ('default_section' , DEFAULTSECT )
663
- interpolation = kwargs .get ('interpolation' , _UNSET )
664
- converters = kwargs .get ('converters' , _UNSET )
665
-
666
668
self ._dict = dict_type
667
669
self ._sections = self ._dict ()
668
670
self ._defaults = self ._dict ()
@@ -749,13 +751,13 @@ def read(self, filenames, encoding=None):
749
751
encoding = io .text_encoding (encoding )
750
752
read_ok = []
751
753
for filename in filenames :
752
- if isinstance (filename , os .PathLike ):
753
- filename = os .fspath (filename )
754
754
try :
755
755
with open (filename , encoding = encoding ) as fp :
756
756
self ._read (fp , filename )
757
- except IOError :
757
+ except OSError :
758
758
continue
759
+ if isinstance (filename , os .PathLike ):
760
+ filename = os .fspath (filename )
759
761
read_ok .append (filename )
760
762
return read_ok
761
763
@@ -820,7 +822,7 @@ def readfp(self, fp, filename=None):
820
822
)
821
823
self .read_file (fp , source = filename )
822
824
823
- def get (self , section , option , ** kwargs ):
825
+ def get (self , section , option , * , raw = False , vars = None , fallback = _UNSET ):
824
826
"""Get an option value for a given section.
825
827
826
828
If `vars' is provided, it must be a dictionary. The option is looked up
@@ -835,11 +837,6 @@ def get(self, section, option, **kwargs):
835
837
836
838
The section DEFAULT is special.
837
839
"""
838
- # keyword-only arguments
839
- raw = kwargs .get ('raw' , False )
840
- vars = kwargs .get ('vars' , None )
841
- fallback = kwargs .get ('fallback' , _UNSET )
842
-
843
840
try :
844
841
d = self ._unify_values (section , vars )
845
842
except NoSectionError :
@@ -864,39 +861,43 @@ def get(self, section, option, **kwargs):
864
861
def _get (self , section , conv , option , ** kwargs ):
865
862
return conv (self .get (section , option , ** kwargs ))
866
863
867
- def _get_conv (self , section , option , conv , ** kwargs ):
868
- # keyword-only arguments
869
- kwargs .setdefault ('raw' , False )
870
- kwargs .setdefault ('vars' , None )
871
- fallback = kwargs .pop ('fallback' , _UNSET )
864
+ def _get_conv (
865
+ self , section , option , conv , * , raw = False , vars = None , fallback = _UNSET , ** kwargs
866
+ ):
872
867
try :
873
- return self ._get (section , conv , option , ** kwargs )
868
+ return self ._get (section , conv , option , raw = raw , vars = vars , ** kwargs )
874
869
except (NoSectionError , NoOptionError ):
875
870
if fallback is _UNSET :
876
871
raise
877
872
return fallback
878
873
879
874
# getint, getfloat and getboolean provided directly for backwards compat
880
- def getint (self , section , option , ** kwargs ):
881
- # keyword-only arguments
882
- kwargs .setdefault ('raw' , False )
883
- kwargs .setdefault ('vars' , None )
884
- kwargs .setdefault ('fallback' , _UNSET )
885
- return self ._get_conv (section , option , int , ** kwargs )
886
-
887
- def getfloat (self , section , option , ** kwargs ):
888
- # keyword-only arguments
889
- kwargs .setdefault ('raw' , False )
890
- kwargs .setdefault ('vars' , None )
891
- kwargs .setdefault ('fallback' , _UNSET )
892
- return self ._get_conv (section , option , float , ** kwargs )
893
-
894
- def getboolean (self , section , option , ** kwargs ):
895
- # keyword-only arguments
896
- kwargs .setdefault ('raw' , False )
897
- kwargs .setdefault ('vars' , None )
898
- kwargs .setdefault ('fallback' , _UNSET )
899
- return self ._get_conv (section , option , self ._convert_to_boolean , ** kwargs )
875
+ def getint (
876
+ self , section , option , * , raw = False , vars = None , fallback = _UNSET , ** kwargs
877
+ ):
878
+ return self ._get_conv (
879
+ section , option , int , raw = raw , vars = vars , fallback = fallback , ** kwargs
880
+ )
881
+
882
+ def getfloat (
883
+ self , section , option , * , raw = False , vars = None , fallback = _UNSET , ** kwargs
884
+ ):
885
+ return self ._get_conv (
886
+ section , option , float , raw = raw , vars = vars , fallback = fallback , ** kwargs
887
+ )
888
+
889
+ def getboolean (
890
+ self , section , option , * , raw = False , vars = None , fallback = _UNSET , ** kwargs
891
+ ):
892
+ return self ._get_conv (
893
+ section ,
894
+ option ,
895
+ self ._convert_to_boolean ,
896
+ raw = raw ,
897
+ vars = vars ,
898
+ fallback = fallback ,
899
+ ** kwargs ,
900
+ )
900
901
901
902
def items (self , section = _UNSET , raw = False , vars = None ):
902
903
"""Return a list of (name, value) tuples for each option in a section.
@@ -985,7 +986,7 @@ def write(self, fp, space_around_delimiters=True):
985
986
preserved when writing the configuration back.
986
987
"""
987
988
if space_around_delimiters :
988
- d = " {0 } " .format (self ._delimiters [0 ])
989
+ d = " {} " .format (self ._delimiters [0 ])
989
990
else :
990
991
d = self ._delimiters [0 ]
991
992
if self ._defaults :
@@ -995,14 +996,14 @@ def write(self, fp, space_around_delimiters=True):
995
996
996
997
def _write_section (self , fp , section_name , section_items , delimiter ):
997
998
"""Write a single section to the specified `fp'."""
998
- fp .write ("[{0 }]\n " .format (section_name ))
999
+ fp .write ("[{}]\n " .format (section_name ))
999
1000
for key , value in section_items :
1000
1001
value = self ._interpolation .before_write (self , section_name , key , value )
1001
1002
if value is not None or not self ._allow_no_value :
1002
1003
value = delimiter + str (value ).replace ('\n ' , '\n \t ' )
1003
1004
else :
1004
1005
value = ""
1005
- fp .write ("{0}{1 }\n " .format (key , value ))
1006
+ fp .write ("{}{ }\n " .format (key , value ))
1006
1007
fp .write ("\n " )
1007
1008
1008
1009
def remove_option (self , section , option ):
@@ -1091,7 +1092,7 @@ def _read(self, fp, fpname): # noqa: C901
1091
1092
for lineno , line in enumerate (fp , start = 1 ):
1092
1093
comment_start = sys .maxsize
1093
1094
# strip inline comments
1094
- inline_prefixes = dict (( p , - 1 ) for p in self ._inline_comment_prefixes )
1095
+ inline_prefixes = { p : - 1 for p in self ._inline_comment_prefixes }
1095
1096
while comment_start == sys .maxsize and inline_prefixes :
1096
1097
next_prefixes = {}
1097
1098
for prefix , index in inline_prefixes .items ():
@@ -1235,7 +1236,7 @@ def _convert_to_boolean(self, value):
1235
1236
raise ValueError ('Not a boolean: %s' % value )
1236
1237
return self .BOOLEAN_STATES [value .lower ()]
1237
1238
1238
- def _validate_value_types (self , ** kwargs ):
1239
+ def _validate_value_types (self , * , section = "" , option = "" , value = "" ):
1239
1240
"""Raises a TypeError for non-string values.
1240
1241
1241
1242
The only legal non-string value if we allow valueless
@@ -1248,11 +1249,6 @@ def _validate_value_types(self, **kwargs):
1248
1249
for RawConfigParsers. It is invoked in every case for mapping protocol
1249
1250
access and in ConfigParser.set().
1250
1251
"""
1251
- # keyword-only arguments
1252
- section = kwargs .get ('section' , "" )
1253
- option = kwargs .get ('option' , "" )
1254
- value = kwargs .get ('value' , "" )
1255
-
1256
1252
if not isinstance (section , str ):
1257
1253
raise TypeError ("section names must be strings" )
1258
1254
if not isinstance (option , str ):
@@ -1261,8 +1257,6 @@ def _validate_value_types(self, **kwargs):
1261
1257
if not isinstance (value , str ):
1262
1258
raise TypeError ("option values must be strings" )
1263
1259
1264
- return section , option , value
1265
-
1266
1260
@property
1267
1261
def converters (self ):
1268
1262
return self ._converters
@@ -1276,15 +1270,15 @@ class ConfigParser(RawConfigParser):
1276
1270
def set (self , section , option , value = None ):
1277
1271
"""Set an option. Extends RawConfigParser.set by validating type and
1278
1272
interpolation syntax on the value."""
1279
- _ , option , value = self ._validate_value_types (option = option , value = value )
1280
- super (ConfigParser , self ).set (section , option , value )
1273
+ self ._validate_value_types (option = option , value = value )
1274
+ super ().set (section , option , value )
1281
1275
1282
1276
def add_section (self , section ):
1283
1277
"""Create a new section in the configuration. Extends
1284
1278
RawConfigParser.add_section by validating if the section name is
1285
1279
a string."""
1286
- section , _ , _ = self ._validate_value_types (section = section )
1287
- super (ConfigParser , self ).add_section (section )
1280
+ self ._validate_value_types (section = section )
1281
+ super ().add_section (section )
1288
1282
1289
1283
def _read_defaults (self , defaults ):
1290
1284
"""Reads the defaults passed in the initializer, implicitly converting
@@ -1304,7 +1298,7 @@ class SafeConfigParser(ConfigParser):
1304
1298
"""ConfigParser alias for backwards compatibility purposes."""
1305
1299
1306
1300
def __init__ (self , * args , ** kwargs ):
1307
- super (SafeConfigParser , self ).__init__ (* args , ** kwargs )
1301
+ super ().__init__ (* args , ** kwargs )
1308
1302
warnings .warn (
1309
1303
"The SafeConfigParser class has been renamed to ConfigParser "
1310
1304
"in Python 3.2. This alias will be removed in future versions."
@@ -1327,15 +1321,15 @@ def __init__(self, parser, name):
1327
1321
setattr (self , key , getter )
1328
1322
1329
1323
def __repr__ (self ):
1330
- return '<Section: {0 }>' .format (self ._name )
1324
+ return '<Section: {}>' .format (self ._name )
1331
1325
1332
1326
def __getitem__ (self , key ):
1333
1327
if not self ._parser .has_option (self ._name , key ):
1334
1328
raise KeyError (key )
1335
1329
return self ._parser .get (self ._name , key )
1336
1330
1337
1331
def __setitem__ (self , key , value ):
1338
- _ , key , value = self ._parser ._validate_value_types (option = key , value = value )
1332
+ self ._parser ._validate_value_types (option = key , value = value )
1339
1333
return self ._parser .set (self ._name , key , value )
1340
1334
1341
1335
def __delitem__ (self , key ):
@@ -1370,22 +1364,20 @@ def name(self):
1370
1364
# The name of the section on a proxy is read-only.
1371
1365
return self ._name
1372
1366
1373
- def get (self , option , fallback = None , ** kwargs ):
1367
+ def get (self , option , fallback = None , * , raw = False , vars = None , _impl = None , * *kwargs ):
1374
1368
"""Get an option value.
1375
1369
1376
1370
Unless `fallback` is provided, `None` will be returned if the option
1377
1371
is not found.
1378
1372
1379
1373
"""
1380
- # keyword-only arguments
1381
- kwargs .setdefault ('raw' , False )
1382
- kwargs .setdefault ('vars' , None )
1383
- _impl = kwargs .pop ('_impl' , None )
1384
1374
# If `_impl` is provided, it should be a getter method on the parser
1385
1375
# object that provides the desired type conversion.
1386
1376
if not _impl :
1387
1377
_impl = self ._parser .get
1388
- return _impl (self ._name , option , fallback = fallback , ** kwargs )
1378
+ return _impl (
1379
+ self ._name , option , raw = raw , vars = vars , fallback = fallback , ** kwargs
1380
+ )
1389
1381
1390
1382
1391
1383
class ConverterMapping (MutableMapping ):
0 commit comments