From 814cbfde3e774136e2c8619d7096b2b020452b96 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Sun, 6 Aug 2017 23:29:39 -0400 Subject: [PATCH 1/3] bpo-31129: Split RawConfigParser.items() in to two methods RawConfigParser.section_items() contains the codepath that was run in .items() when arguments were passed. .items() continues to pass through to super().items() when no arguments are passed. If arguments are given, it emits a DeprecationWarning and passes through to .section_items(). --- Lib/configparser.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index ea971f3933859a..66db3d58e9a544 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -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. @@ -828,7 +831,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 @@ -839,8 +850,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]) From d28bd9920f845e3d044f390713f8d0dc1a959d8d Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Sun, 6 Aug 2017 23:35:36 -0400 Subject: [PATCH 2/3] bpo-31129: Update configparser code to use new section_items() method --- Lib/configparser.py | 3 ++- Lib/test/test_configparser.py | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 66db3d58e9a544..4c870ca014b157 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -510,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) diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 72c3f19fb41f56..36575a41f9f49e 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -90,7 +90,7 @@ 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) @@ -775,11 +775,11 @@ def check_items_config(self, expected): key{0[1]} |%(name)s| getdefault{0[1]} |%(default)s| """.format(self.delimiters), defaults={"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") def test_popitem(self): cf = self.fromstring(""" @@ -1321,10 +1321,10 @@ 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.items('no values here') + 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')) From 319575d235e7239d2d725bc13dcbf5b5181f444e Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Sun, 6 Aug 2017 23:47:01 -0400 Subject: [PATCH 3/3] bpo-31129: Add to tests to ensure .items() behaviour is unchanged But ensure that we do get a DeprecationWarning. --- Lib/test/test_configparser.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 36575a41f9f49e..f42832b1b23c92 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -93,6 +93,10 @@ def basic_test(self, cf): 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) # mapping access L = [section for section in cf] @@ -781,6 +785,14 @@ def check_items_config(self, expected): with self.assertRaises(configparser.NoSectionError): 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(""" [section1] @@ -1325,6 +1337,11 @@ def test_cfgparser_dot_3(self): 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): + 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'))