Commit 71b37a5d authored by Łukasz Langa's avatar Łukasz Langa

100% test coverage, better mapping protocol compatibility, some minor bugfixes

parent 0e74cacd
...@@ -391,17 +391,20 @@ However, there are a few differences that should be taken into account: ...@@ -391,17 +391,20 @@ However, there are a few differences that should be taken into account:
* Trying to delete the ``DEFAULTSECT`` raises ``ValueError``. * Trying to delete the ``DEFAULTSECT`` raises ``ValueError``.
* There are two parser-level methods in the legacy API that hide the dictionary * ``parser.get(section, option, **kwargs)`` - the second argument is **not**
interface and are incompatible: a fallback value. Note however that the section-level ``get()`` methods are
compatible both with the mapping protocol and the classic configparser API.
* ``parser.get(section, option, **kwargs)`` - the second argument is **not** a * ``parser.items()`` is compatible with the mapping protocol (returns a list of
fallback value *section_name*, *section_proxy* pairs including the DEFAULTSECT). However,
this method can also be invoked with arguments: ``parser.items(section, raw,
* ``parser.items(section)`` - this returns a list of *option*, *value* pairs vars)``. The latter call returns a list of *option*, *value* pairs for
for a specified ``section`` a specified ``section``, with all interpolations expanded (unless
``raw=True`` is provided).
The mapping protocol is implemented on top of the existing legacy API so that The mapping protocol is implemented on top of the existing legacy API so that
subclassing the original interface makes the mappings work as expected as well. subclasses overriding the original interface still should have mappings working
as expected.
Customizing Parser Behaviour Customizing Parser Behaviour
...@@ -906,7 +909,8 @@ ConfigParser Objects ...@@ -906,7 +909,8 @@ ConfigParser Objects
.. method:: has_option(section, option) .. method:: has_option(section, option)
If the given *section* exists, and contains the given *option*, return If the given *section* exists, and contains the given *option*, return
:const:`True`; otherwise return :const:`False`. :const:`True`; otherwise return :const:`False`. If the specified
*section* is :const:`None` or an empty string, DEFAULT is assumed.
.. method:: read(filenames, encoding=None) .. method:: read(filenames, encoding=None)
...@@ -964,14 +968,17 @@ ConfigParser Objects ...@@ -964,14 +968,17 @@ ConfigParser Objects
.. method:: read_dict(dictionary, source='<dict>') .. method:: read_dict(dictionary, source='<dict>')
Load configuration from a dictionary. Keys are section names, values are Load configuration from any object that provides a dict-like ``items()``
dictionaries with keys and values that should be present in the section. method. Keys are section names, values are dictionaries with keys and
If the used dictionary type preserves order, sections and their keys will values that should be present in the section. If the used dictionary
be added in order. Values are automatically converted to strings. type preserves order, sections and their keys will be added in order.
Values are automatically converted to strings.
Optional argument *source* specifies a context-specific name of the Optional argument *source* specifies a context-specific name of the
dictionary passed. If not given, ``<dict>`` is used. dictionary passed. If not given, ``<dict>`` is used.
This method can be used to copy state between parsers.
.. versionadded:: 3.2 .. versionadded:: 3.2
...@@ -1019,10 +1026,13 @@ ConfigParser Objects ...@@ -1019,10 +1026,13 @@ ConfigParser Objects
*fallback*. *fallback*.
.. method:: items(section, raw=False, vars=None) .. method:: items([section], raw=False, vars=None)
When *section* is not given, return a list of *section_name*,
*section_proxy* pairs, including DEFAULTSECT.
Return a list of *name*, *value* pairs for the options in the given Otherwise, return a list of *name*, *value* pairs for the options in the
*section*. Optional arguments have the same meaning as for the given *section*. Optional arguments have the same meaning as for the
:meth:`get` method. :meth:`get` method.
......
...@@ -98,8 +98,10 @@ ConfigParser -- responsible for parsing a list of ...@@ -98,8 +98,10 @@ ConfigParser -- responsible for parsing a list of
insensitively defined as 0, false, no, off for False, and 1, true, insensitively defined as 0, false, no, off for False, and 1, true,
yes, on for True). Returns False or True. yes, on for True). Returns False or True.
items(section, raw=False, vars=None) items(section=_UNSET, raw=False, vars=None)
Return a list of tuples with (name, value) for each option If section is given, return a list of tuples with (section_name,
section_proxy) for each section, including DEFAULTSECT. Otherwise,
return a list of tuples with (name, value) for each option
in the section. in the section.
remove_section(section) remove_section(section)
...@@ -495,9 +497,9 @@ class ExtendedInterpolation(Interpolation): ...@@ -495,9 +497,9 @@ class ExtendedInterpolation(Interpolation):
raise InterpolationSyntaxError( raise InterpolationSyntaxError(
option, section, option, section,
"More than one ':' found: %r" % (rest,)) "More than one ':' found: %r" % (rest,))
except KeyError: except (KeyError, NoSectionError, NoOptionError):
raise InterpolationMissingOptionError( raise InterpolationMissingOptionError(
option, section, rest, var) option, section, rest, ":".join(path))
if "$" in v: if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect, self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)), dict(parser.items(sect, raw=True)),
...@@ -730,7 +732,7 @@ class RawConfigParser(MutableMapping): ...@@ -730,7 +732,7 @@ class RawConfigParser(MutableMapping):
except (DuplicateSectionError, ValueError): except (DuplicateSectionError, ValueError):
if self._strict and section in elements_added: if self._strict and section in elements_added:
raise raise
elements_added.add(section) elements_added.add(section)
for key, value in keys.items(): for key, value in keys.items():
key = self.optionxform(str(key)) key = self.optionxform(str(key))
if value is not None: if value is not None:
...@@ -820,7 +822,7 @@ class RawConfigParser(MutableMapping): ...@@ -820,7 +822,7 @@ class RawConfigParser(MutableMapping):
else: else:
return fallback return fallback
def items(self, section, raw=False, vars=None): def items(self, section=_UNSET, raw=False, vars=None):
"""Return a list of (name, value) tuples for each option in a section. """Return a list of (name, value) tuples for each option in a section.
All % interpolations are expanded in the return values, based on the All % interpolations are expanded in the return values, based on the
...@@ -831,6 +833,8 @@ class RawConfigParser(MutableMapping): ...@@ -831,6 +833,8 @@ class RawConfigParser(MutableMapping):
The section DEFAULT is special. The section DEFAULT is special.
""" """
if section is _UNSET:
return super().items()
d = self._defaults.copy() d = self._defaults.copy()
try: try:
d.update(self._sections[section]) d.update(self._sections[section])
...@@ -851,7 +855,9 @@ class RawConfigParser(MutableMapping): ...@@ -851,7 +855,9 @@ class RawConfigParser(MutableMapping):
return optionstr.lower() return optionstr.lower()
def has_option(self, section, option): def has_option(self, section, option):
"""Check for the existence of a given option in a given section.""" """Check for the existence of a given option in a given section.
If the specified `section' is None or an empty string, DEFAULT is
assumed. If the specified `section' does not exist, returns False."""
if not section or section == self.default_section: if not section or section == self.default_section:
option = self.optionxform(option) option = self.optionxform(option)
return option in self._defaults return option in self._defaults
...@@ -1059,9 +1065,6 @@ class RawConfigParser(MutableMapping): ...@@ -1059,9 +1065,6 @@ class RawConfigParser(MutableMapping):
# match if it would set optval to None # match if it would set optval to None
if optval is not None: if optval is not None:
optval = optval.strip() optval = optval.strip()
# allow empty values
if optval == '""':
optval = ''
cursect[optname] = [optval] cursect[optname] = [optval]
else: else:
# valueless option handling # valueless option handling
...@@ -1196,21 +1199,24 @@ class SectionProxy(MutableMapping): ...@@ -1196,21 +1199,24 @@ class SectionProxy(MutableMapping):
return self._parser.set(self._name, key, value) return self._parser.set(self._name, key, value)
def __delitem__(self, key): def __delitem__(self, key):
if not self._parser.has_option(self._name, key): if not (self._parser.has_option(self._name, key) and
self._parser.remove_option(self._name, key)):
raise KeyError(key) raise KeyError(key)
return self._parser.remove_option(self._name, key)
def __contains__(self, key): def __contains__(self, key):
return self._parser.has_option(self._name, key) return self._parser.has_option(self._name, key)
def __len__(self): def __len__(self):
# XXX weak performance return len(self._options())
return len(self._parser.options(self._name))
def __iter__(self): def __iter__(self):
# XXX weak performance return self._options().__iter__()
# XXX does not break when underlying container state changed
return self._parser.options(self._name).__iter__() def _options(self):
if self._name != self._parser.default_section:
return self._parser.options(self._name)
else:
return self._parser.defaults()
def get(self, option, fallback=None, *, raw=False, vars=None): def get(self, option, fallback=None, *, raw=False, vars=None):
return self._parser.get(self._name, option, raw=raw, vars=vars, return self._parser.get(self._name, option, raw=raw, vars=vars,
......
This diff is collapsed.
...@@ -293,6 +293,8 @@ Library ...@@ -293,6 +293,8 @@ Library
- Issue #10467: Fix BytesIO.readinto() after seeking into a position after the - Issue #10467: Fix BytesIO.readinto() after seeking into a position after the
end of the file. end of the file.
- configparser: 100% test coverage.
- Issue #10499: configparser supports pluggable interpolation handlers. The - Issue #10499: configparser supports pluggable interpolation handlers. The
default classic interpolation handler is called BasicInterpolation. Another default classic interpolation handler is called BasicInterpolation. Another
interpolation handler added (ExtendedInterpolation) which supports the syntax interpolation handler added (ExtendedInterpolation) which supports the syntax
...@@ -314,7 +316,9 @@ Library ...@@ -314,7 +316,9 @@ Library
- Issue #9421: configparser's getint(), getfloat() and getboolean() methods - Issue #9421: configparser's getint(), getfloat() and getboolean() methods
accept vars and default arguments just like get() does. accept vars and default arguments just like get() does.
- Issue #9452: configparser supports reading from strings and dictionaries. - Issue #9452: configparser supports reading from strings and dictionaries
(thanks to the mapping protocol API, the latter can be used to copy data
between parsers).
- configparser: accepted INI file structure is now customizable, including - configparser: accepted INI file structure is now customizable, including
comment prefixes, name of the DEFAULT section, empty lines in multiline comment prefixes, name of the DEFAULT section, empty lines in multiline
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment