Skip to content

Commit c047df3

Browse files
committed
Update syntax matching upstream where possible.
1 parent 77efa8a commit c047df3

File tree

2 files changed

+72
-79
lines changed

2 files changed

+72
-79
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
v5.0.3
1+
v5.1.0
22
======
33

44
* Synced with Python 3.10.0.
55
* Packaging refresh.
66
* Tests now run on Python 3.10.
7+
* Modernized code relying on Python 3.6.
78

89
v5.0.2
910
======

src/backports/configparser/__init__.py

Lines changed: 70 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ class InterpolationMissingOptionError(InterpolationError):
279279

280280
def __init__(self, option, section, rawval, reference):
281281
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)
285285
)
286286
InterpolationError.__init__(self, option, section, msg)
287287
self.reference = reference
@@ -301,9 +301,9 @@ class InterpolationDepthError(InterpolationError):
301301

302302
def __init__(self, option, section, rawval):
303303
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}"
307307
"".format(option, section, MAX_INTERPOLATION_DEPTH, rawval)
308308
)
309309
InterpolationError.__init__(self, option, section, msg)
@@ -377,7 +377,7 @@ def __init__(self, filename, lineno, line):
377377
_UNSET = object()
378378

379379

380-
class Interpolation(object):
380+
class Interpolation:
381381
"""Dummy interpolation that passes the value through with no changes."""
382382

383383
def before_get(self, parser, section, option, value, defaults):
@@ -650,19 +650,21 @@ class RawConfigParser(MutableMapping):
650650
}
651651

652652
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,
654666
):
655667

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-
666668
self._dict = dict_type
667669
self._sections = self._dict()
668670
self._defaults = self._dict()
@@ -749,13 +751,13 @@ def read(self, filenames, encoding=None):
749751
encoding = io.text_encoding(encoding)
750752
read_ok = []
751753
for filename in filenames:
752-
if isinstance(filename, os.PathLike):
753-
filename = os.fspath(filename)
754754
try:
755755
with open(filename, encoding=encoding) as fp:
756756
self._read(fp, filename)
757-
except IOError:
757+
except OSError:
758758
continue
759+
if isinstance(filename, os.PathLike):
760+
filename = os.fspath(filename)
759761
read_ok.append(filename)
760762
return read_ok
761763

@@ -820,7 +822,7 @@ def readfp(self, fp, filename=None):
820822
)
821823
self.read_file(fp, source=filename)
822824

823-
def get(self, section, option, **kwargs):
825+
def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
824826
"""Get an option value for a given section.
825827
826828
If `vars' is provided, it must be a dictionary. The option is looked up
@@ -835,11 +837,6 @@ def get(self, section, option, **kwargs):
835837
836838
The section DEFAULT is special.
837839
"""
838-
# keyword-only arguments
839-
raw = kwargs.get('raw', False)
840-
vars = kwargs.get('vars', None)
841-
fallback = kwargs.get('fallback', _UNSET)
842-
843840
try:
844841
d = self._unify_values(section, vars)
845842
except NoSectionError:
@@ -864,39 +861,43 @@ def get(self, section, option, **kwargs):
864861
def _get(self, section, conv, option, **kwargs):
865862
return conv(self.get(section, option, **kwargs))
866863

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+
):
872867
try:
873-
return self._get(section, conv, option, **kwargs)
868+
return self._get(section, conv, option, raw=raw, vars=vars, **kwargs)
874869
except (NoSectionError, NoOptionError):
875870
if fallback is _UNSET:
876871
raise
877872
return fallback
878873

879874
# 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+
)
900901

901902
def items(self, section=_UNSET, raw=False, vars=None):
902903
"""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):
985986
preserved when writing the configuration back.
986987
"""
987988
if space_around_delimiters:
988-
d = " {0} ".format(self._delimiters[0])
989+
d = " {} ".format(self._delimiters[0])
989990
else:
990991
d = self._delimiters[0]
991992
if self._defaults:
@@ -995,14 +996,14 @@ def write(self, fp, space_around_delimiters=True):
995996

996997
def _write_section(self, fp, section_name, section_items, delimiter):
997998
"""Write a single section to the specified `fp'."""
998-
fp.write("[{0}]\n".format(section_name))
999+
fp.write("[{}]\n".format(section_name))
9991000
for key, value in section_items:
10001001
value = self._interpolation.before_write(self, section_name, key, value)
10011002
if value is not None or not self._allow_no_value:
10021003
value = delimiter + str(value).replace('\n', '\n\t')
10031004
else:
10041005
value = ""
1005-
fp.write("{0}{1}\n".format(key, value))
1006+
fp.write("{}{}\n".format(key, value))
10061007
fp.write("\n")
10071008

10081009
def remove_option(self, section, option):
@@ -1091,7 +1092,7 @@ def _read(self, fp, fpname): # noqa: C901
10911092
for lineno, line in enumerate(fp, start=1):
10921093
comment_start = sys.maxsize
10931094
# 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}
10951096
while comment_start == sys.maxsize and inline_prefixes:
10961097
next_prefixes = {}
10971098
for prefix, index in inline_prefixes.items():
@@ -1235,7 +1236,7 @@ def _convert_to_boolean(self, value):
12351236
raise ValueError('Not a boolean: %s' % value)
12361237
return self.BOOLEAN_STATES[value.lower()]
12371238

1238-
def _validate_value_types(self, **kwargs):
1239+
def _validate_value_types(self, *, section="", option="", value=""):
12391240
"""Raises a TypeError for non-string values.
12401241
12411242
The only legal non-string value if we allow valueless
@@ -1248,11 +1249,6 @@ def _validate_value_types(self, **kwargs):
12481249
for RawConfigParsers. It is invoked in every case for mapping protocol
12491250
access and in ConfigParser.set().
12501251
"""
1251-
# keyword-only arguments
1252-
section = kwargs.get('section', "")
1253-
option = kwargs.get('option', "")
1254-
value = kwargs.get('value', "")
1255-
12561252
if not isinstance(section, str):
12571253
raise TypeError("section names must be strings")
12581254
if not isinstance(option, str):
@@ -1261,8 +1257,6 @@ def _validate_value_types(self, **kwargs):
12611257
if not isinstance(value, str):
12621258
raise TypeError("option values must be strings")
12631259

1264-
return section, option, value
1265-
12661260
@property
12671261
def converters(self):
12681262
return self._converters
@@ -1276,15 +1270,15 @@ class ConfigParser(RawConfigParser):
12761270
def set(self, section, option, value=None):
12771271
"""Set an option. Extends RawConfigParser.set by validating type and
12781272
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)
12811275

12821276
def add_section(self, section):
12831277
"""Create a new section in the configuration. Extends
12841278
RawConfigParser.add_section by validating if the section name is
12851279
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)
12881282

12891283
def _read_defaults(self, defaults):
12901284
"""Reads the defaults passed in the initializer, implicitly converting
@@ -1304,7 +1298,7 @@ class SafeConfigParser(ConfigParser):
13041298
"""ConfigParser alias for backwards compatibility purposes."""
13051299

13061300
def __init__(self, *args, **kwargs):
1307-
super(SafeConfigParser, self).__init__(*args, **kwargs)
1301+
super().__init__(*args, **kwargs)
13081302
warnings.warn(
13091303
"The SafeConfigParser class has been renamed to ConfigParser "
13101304
"in Python 3.2. This alias will be removed in future versions."
@@ -1327,15 +1321,15 @@ def __init__(self, parser, name):
13271321
setattr(self, key, getter)
13281322

13291323
def __repr__(self):
1330-
return '<Section: {0}>'.format(self._name)
1324+
return '<Section: {}>'.format(self._name)
13311325

13321326
def __getitem__(self, key):
13331327
if not self._parser.has_option(self._name, key):
13341328
raise KeyError(key)
13351329
return self._parser.get(self._name, key)
13361330

13371331
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)
13391333
return self._parser.set(self._name, key, value)
13401334

13411335
def __delitem__(self, key):
@@ -1370,22 +1364,20 @@ def name(self):
13701364
# The name of the section on a proxy is read-only.
13711365
return self._name
13721366

1373-
def get(self, option, fallback=None, **kwargs):
1367+
def get(self, option, fallback=None, *, raw=False, vars=None, _impl=None, **kwargs):
13741368
"""Get an option value.
13751369
13761370
Unless `fallback` is provided, `None` will be returned if the option
13771371
is not found.
13781372
13791373
"""
1380-
# keyword-only arguments
1381-
kwargs.setdefault('raw', False)
1382-
kwargs.setdefault('vars', None)
1383-
_impl = kwargs.pop('_impl', None)
13841374
# If `_impl` is provided, it should be a getter method on the parser
13851375
# object that provides the desired type conversion.
13861376
if not _impl:
13871377
_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+
)
13891381

13901382

13911383
class ConverterMapping(MutableMapping):

0 commit comments

Comments
 (0)