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:
* Trying to delete the ``DEFAULTSECT`` raises ``ValueError``.
* There are two parser-level methods in the legacy API that hide the dictionary
interface and are incompatible:
* ``parser.get(section, option, **kwargs)`` - the second argument is **not**
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
fallback value
* ``parser.items(section)`` - this returns a list of *option*, *value* pairs
for a specified ``section``
* ``parser.items()`` is compatible with the mapping protocol (returns a list of
*section_name*, *section_proxy* pairs including the DEFAULTSECT). However,
this method can also be invoked with arguments: ``parser.items(section, raw,
vars)``. The latter call returns a list of *option*, *value* pairs for
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
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
......@@ -906,7 +909,8 @@ ConfigParser Objects
.. method:: has_option(section, option)
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)
......@@ -964,14 +968,17 @@ ConfigParser Objects
.. method:: read_dict(dictionary, source='<dict>')
Load configuration from a dictionary. Keys are section names, values are
dictionaries with keys and values that should be present in the section.
If the used dictionary type preserves order, sections and their keys will
be added in order. Values are automatically converted to strings.
Load configuration from any object that provides a dict-like ``items()``
method. Keys are section names, values are dictionaries with keys and
values that should be present in the section. If the used dictionary
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
dictionary passed. If not given, ``<dict>`` is used.
This method can be used to copy state between parsers.
.. versionadded:: 3.2
......@@ -1019,10 +1026,13 @@ ConfigParser Objects
*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
*section*. Optional arguments have the same meaning as for the
Otherwise, return a list of *name*, *value* pairs for the options in the
given *section*. Optional arguments have the same meaning as for the
:meth:`get` method.
......
......@@ -98,8 +98,10 @@ ConfigParser -- responsible for parsing a list of
insensitively defined as 0, false, no, off for False, and 1, true,
yes, on for True). Returns False or True.
items(section, raw=False, vars=None)
Return a list of tuples with (name, value) for each option
items(section=_UNSET, raw=False, vars=None)
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.
remove_section(section)
......@@ -495,9 +497,9 @@ class ExtendedInterpolation(Interpolation):
raise InterpolationSyntaxError(
option, section,
"More than one ':' found: %r" % (rest,))
except KeyError:
except (KeyError, NoSectionError, NoOptionError):
raise InterpolationMissingOptionError(
option, section, rest, var)
option, section, rest, ":".join(path))
if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)),
......@@ -730,7 +732,7 @@ class RawConfigParser(MutableMapping):
except (DuplicateSectionError, ValueError):
if self._strict and section in elements_added:
raise
elements_added.add(section)
elements_added.add(section)
for key, value in keys.items():
key = self.optionxform(str(key))
if value is not None:
......@@ -820,7 +822,7 @@ class RawConfigParser(MutableMapping):
else:
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.
All % interpolations are expanded in the return values, based on the
......@@ -831,6 +833,8 @@ class RawConfigParser(MutableMapping):
The section DEFAULT is special.
"""
if section is _UNSET:
return super().items()
d = self._defaults.copy()
try:
d.update(self._sections[section])
......@@ -851,7 +855,9 @@ class RawConfigParser(MutableMapping):
return optionstr.lower()
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:
option = self.optionxform(option)
return option in self._defaults
......@@ -1059,9 +1065,6 @@ class RawConfigParser(MutableMapping):
# match if it would set optval to None
if optval is not None:
optval = optval.strip()
# allow empty values
if optval == '""':
optval = ''
cursect[optname] = [optval]
else:
# valueless option handling
......@@ -1196,21 +1199,24 @@ class SectionProxy(MutableMapping):
return self._parser.set(self._name, key, value)
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)
return self._parser.remove_option(self._name, key)
def __contains__(self, key):
return self._parser.has_option(self._name, key)
def __len__(self):
# XXX weak performance
return len(self._parser.options(self._name))
return len(self._options())
def __iter__(self):
# XXX weak performance
# XXX does not break when underlying container state changed
return self._parser.options(self._name).__iter__()
return self._options().__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):
return self._parser.get(self._name, option, raw=raw, vars=vars,
......
This diff is collapsed.
......@@ -293,6 +293,8 @@ Library
- Issue #10467: Fix BytesIO.readinto() after seeking into a position after the
end of the file.
- configparser: 100% test coverage.
- Issue #10499: configparser supports pluggable interpolation handlers. The
default classic interpolation handler is called BasicInterpolation. Another
interpolation handler added (ExtendedInterpolation) which supports the syntax
......@@ -314,7 +316,9 @@ Library
- Issue #9421: configparser's getint(), getfloat() and getboolean() methods
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
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