Skip to content

bpo-31129: Split RawConfigParser.items() in to two methods #3012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@
insensitively defined as 0, false, no, off for False, and 1, true,
yes, on for True). Returns False or True.

items(section=_UNSET, raw=False, vars=None)
If section is given, return a list of tuples with (name, value) for
each option in the section. Otherwise, return a list of tuples with
(section_name, section_proxy) for each section, including DEFAULTSECT.
items()
Return a list of tuples with (section_name, section_proxy) for each
section, including DEFAULTSECT.

section_items(section, raw=False, vars=None)
Return a list of tuples with (name, value) for each option in the
section.

remove_section(section)
Remove the given file section and all its options.
Expand Down Expand Up @@ -507,7 +510,8 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
option, section, rawval, ":".join(path)) from None
if "$" in v:
self._interpolate_some(parser, opt, accum, v, sect,
dict(parser.items(sect, raw=True)),
dict(parser.section_items(
sect, raw=True)),
depth + 1)
else:
accum.append(v)
Expand Down Expand Up @@ -828,7 +832,15 @@ def getboolean(self, section, option, *, raw=False, vars=None,
return self._get_conv(section, option, self._convert_to_boolean,
raw=raw, vars=vars, fallback=fallback, **kwargs)

def items(self, section=_UNSET, raw=False, vars=None):
def items(self, *args, **kwargs):
if len(args) != 0 or len(kwargs) != 0:
warnings.warn("Calling the 'items' method with arguments is"
" deprecated, use 'section_items' instead",
DeprecationWarning, stacklevel=2)
return self.section_items(*args, **kwargs)
return super().items()

def section_items(self, section, 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
Expand All @@ -839,8 +851,6 @@ def items(self, section=_UNSET, raw=False, vars=None):

The section DEFAULT is special.
"""
if section is _UNSET:
return super().items()
d = self._defaults.copy()
try:
d.update(self._sections[section])
Expand Down
29 changes: 23 additions & 6 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def basic_test(self, cf):
L.sort()
eq = self.assertEqual
eq(L, E)
L = cf.items('Spacey Bar From The Beginning')
L = cf.section_items('Spacey Bar From The Beginning')
L.sort()
eq(L, F)
with self.assertWarns(DeprecationWarning):
L = cf.items('Spacey Bar From The Beginning')
L.sort()
eq(L, F)

Expand Down Expand Up @@ -775,11 +779,19 @@ def check_items_config(self, expected):
key{0[1]} |%(name)s|
getdefault{0[1]} |%(default)s|
""".format(self.delimiters), defaults={"default": "<default>"})
L = list(cf.items("section", vars={'value': 'value'}))
L = list(cf.section_items("section", vars={'value': 'value'}))
L.sort()
self.assertEqual(L, expected)
with self.assertRaises(configparser.NoSectionError):
cf.items("no such section")
cf.section_items("no such section")

with self.assertWarns(DeprecationWarning):
L = list(cf.items("section", vars={'value': 'value'}))
L.sort()
self.assertEqual(L, expected)
with self.assertRaises(configparser.NoSectionError):
with self.assertWarns(DeprecationWarning):
cf.section_items("no such section")

def test_popitem(self):
cf = self.fromstring("""
Expand Down Expand Up @@ -1321,10 +1333,15 @@ def test_cfgparser_dot_3(self):
longname = 'yeah, sections can be indented as well'
self.assertFalse(cf.getboolean(longname, 'are they subsections'))
self.assertEqual(cf.get(longname, 'lets use some Unicode'), '片仮名')
self.assertEqual(len(cf.items('another one!')), 5) # 4 in section and
# `go` from DEFAULT
# 4 in section and `go` from DEFAULT
self.assertEqual(len(cf.section_items('another one!')), 5)
with self.assertRaises(configparser.InterpolationMissingOptionError):
cf.section_items('no values here')
with self.assertWarns(DeprecationWarning):
self.assertEqual(len(cf.items('another one!')), 5)
with self.assertRaises(configparser.InterpolationMissingOptionError):
cf.items('no values here')
with self.assertWarns(DeprecationWarning):
cf.section_items('no values here')
self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this')
self.assertEqual(cf.get('tricky interpolation', 'lets'),
cf.get('tricky interpolation', 'go'))
Expand Down