From c9cae8151ad5bd5895500721943899752b8d296a Mon Sep 17 00:00:00 2001 From: chrBrd Date: Sun, 22 Apr 2018 21:19:39 +0100 Subject: [PATCH 1/3] `RawConfigParser.items` only returns options from specified section. --- Lib/configparser.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 33dc9b9046f102a..ae75b3ca405ccd6 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -843,9 +843,11 @@ def items(self, section=_UNSET, raw=False, vars=None): d = self._defaults.copy() try: d.update(self._sections[section]) + section_keys = list(self._sections[section]) except KeyError: if section != self.default_section: raise NoSectionError(section) + section_keys = list(d) # Update with the entry specific variables if vars: for key, value in vars.items(): @@ -854,7 +856,8 @@ def items(self, section=_UNSET, raw=False, vars=None): section, option, d[option], d) if raw: value_getter = lambda option: d[option] - return [(option, value_getter(option)) for option in d.keys()] + return [(option, value_getter(option)) for option in d.keys() + if option in section_keys] def popitem(self): """Remove a section from the parser and return it as From f98d0790398ec3f00bfbde72dc52bc7c2384cdce Mon Sep 17 00:00:00 2001 From: chrBrd Date: Sun, 22 Apr 2018 21:39:38 +0100 Subject: [PATCH 2/3] Amend tests for `RawConfigParser.items`. --- Lib/test/test_configparser.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 4d07203b9dac289..4335fa1cb135032 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -912,11 +912,9 @@ def test_interpolation_missing_value(self): '%(reference)s', 'reference')) def test_items(self): - self.check_items_config([('default', ''), - ('getdefault', '||'), + self.check_items_config([('getdefault', '||'), ('key', '|value|'), - ('name', 'value'), - ('value', 'value')]) + ('name', 'value')]) def test_safe_interpolation(self): # See http://www.python.org/sf/511737 @@ -1090,11 +1088,9 @@ def test_interpolation(self): "something %(with11)s lots of interpolation (11 steps)") def test_items(self): - self.check_items_config([('default', ''), - ('getdefault', '|%(default)s|'), + self.check_items_config([('getdefault', '|%(default)s|'), ('key', '|%(name)s|'), - ('name', '%(value)s'), - ('value', 'value')]) + ('name', '%(value)s')]) def test_set_nonstring_types(self): cf = self.newconfig() @@ -1356,10 +1352,7 @@ 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 - with self.assertRaises(configparser.InterpolationMissingOptionError): - cf.items('no values here') + self.assertEqual(len(cf.items('another one!')), 4) self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this') self.assertEqual(cf.get('tricky interpolation', 'lets'), cf.get('tricky interpolation', 'go')) From 33d82cd128118acc345838d346fd9f7042ba202f Mon Sep 17 00:00:00 2001 From: chrBrd Date: Sun, 22 Apr 2018 21:41:11 +0100 Subject: [PATCH 3/3] Fix `ExtendedInterpolation._interpolate_some` to work without default section keys being returned by `RawConfigParser.items`. --- Lib/configparser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index ae75b3ca405ccd6..1d7eebc2ae0d524 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -489,10 +489,11 @@ def _interpolate_some(self, parser, option, accum, rest, section, map, path = m.group(1).split(':') rest = rest[m.end():] sect = section - opt = option try: if len(path) == 1: opt = parser.optionxform(path[0]) + if opt not in map: + map.update(parser.items(parser.default_section)) v = map[opt] elif len(path) == 2: sect = path[0]