diff options
author | Ronan Lamy <ronan.lamy@gmail.com> | 2019-08-29 15:01:05 +0100 |
---|---|---|
committer | Ronan Lamy <ronan.lamy@gmail.com> | 2019-08-29 15:01:05 +0100 |
commit | 325138f6bff8829330ea3dc0d9b28157a61a6d44 (patch) | |
tree | e9a45e613fcb425c4f1ae1099f9e62e1c1f30cf9 /lib-python/3/configparser.py | |
parent | Update to git tag v3.6.9 (diff) | |
download | pypy-325138f6bff8829330ea3dc0d9b28157a61a6d44.tar.gz pypy-325138f6bff8829330ea3dc0d9b28157a61a6d44.tar.bz2 pypy-325138f6bff8829330ea3dc0d9b28157a61a6d44.zip |
Import CPython stdlib at tag v3.7.4
Diffstat (limited to 'lib-python/3/configparser.py')
-rw-r--r-- | lib-python/3/configparser.py | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/lib-python/3/configparser.py b/lib-python/3/configparser.py index 0e529e9693..4caf3faadc 100644 --- a/lib-python/3/configparser.py +++ b/lib-python/3/configparser.py @@ -610,9 +610,6 @@ class RawConfigParser(MutableMapping): self._converters = ConverterMapping(self) self._proxies = self._dict() self._proxies[default_section] = SectionProxy(self, default_section) - if defaults: - for key, value in defaults.items(): - self._defaults[self.optionxform(key)] = value self._delimiters = tuple(delimiters) if delimiters == ('=', ':'): self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE @@ -637,6 +634,8 @@ class RawConfigParser(MutableMapping): self._interpolation = Interpolation() if converters is not _UNSET: self._converters.update(converters) + if defaults: + self._read_defaults(defaults) def defaults(self): return self._defaults @@ -688,7 +687,7 @@ class RawConfigParser(MutableMapping): Return list of successfully read files. """ - if isinstance(filenames, (str, os.PathLike)): + if isinstance(filenames, (str, bytes, os.PathLike)): filenames = [filenames] read_ok = [] for filename in filenames: @@ -1122,6 +1121,12 @@ class RawConfigParser(MutableMapping): section, name, val) + def _read_defaults(self, defaults): + """Read the defaults passed in the initializer. + Note: values can be non-string.""" + for key, value in defaults.items(): + self._defaults[self.optionxform(key)] = value + def _handle_error(self, exc, fpname, lineno, line): if not exc: exc = ParsingError(fpname) @@ -1138,7 +1143,7 @@ class RawConfigParser(MutableMapping): sectiondict = self._sections[section] except KeyError: if section != self.default_section: - raise NoSectionError(section) + raise NoSectionError(section) from None # Update with the entry specific variables vardict = {} if vars: @@ -1199,6 +1204,19 @@ class ConfigParser(RawConfigParser): self._validate_value_types(section=section) super().add_section(section) + def _read_defaults(self, defaults): + """Reads the defaults passed in the initializer, implicitly converting + values to strings like the rest of the API. + + Does not perform interpolation for backwards compatibility. + """ + try: + hold_interpolation = self._interpolation + self._interpolation = Interpolation() + self.read_dict({self.default_section: defaults}) + finally: + self._interpolation = hold_interpolation + class SafeConfigParser(ConfigParser): """ConfigParser alias for backwards compatibility purposes.""" |