From 6a27ed62d148a2422252b3b7ffe515966d3bd3ca Mon Sep 17 00:00:00 2001 From: chrBrd Date: Tue, 10 Apr 2018 19:46:02 +0100 Subject: [PATCH 1/5] bpo-33251: ConfigParser.items no longer returns items present in vars. Documentation for `ConfigParser.items()` states: 'Items present in vars no longer appear in the result.' This fix aligns behaviour to that specified in the documentation. --- Lib/configparser.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 33dc9b9046f102..c3b50738174fb9 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -850,11 +850,15 @@ def items(self, section=_UNSET, raw=False, vars=None): if vars: for key, value in vars.items(): d[self.optionxform(key)] = value + # Change `vars` from None to a dict to avoid TypeError on return. + else: + vars = {} value_getter = lambda option: self._interpolation.before_get(self, 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 not in vars] def popitem(self): """Remove a section from the parser and return it as From 5820c8fa67c788ea01900749a0598eaa5a36836b Mon Sep 17 00:00:00 2001 From: chrBrd Date: Tue, 10 Apr 2018 20:58:40 +0100 Subject: [PATCH 2/5] test: Amend unit tests to reflect updated code. --- Lib/test/test_configparser.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 4d07203b9dac28..87b811f09b6ead 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -915,8 +915,7 @@ def test_items(self): self.check_items_config([('default', ''), ('getdefault', '||'), ('key', '|value|'), - ('name', 'value'), - ('value', 'value')]) + ('name', 'value')]) def test_safe_interpolation(self): # See http://www.python.org/sf/511737 @@ -1093,8 +1092,7 @@ def test_items(self): self.check_items_config([('default', ''), ('getdefault', '|%(default)s|'), ('key', '|%(name)s|'), - ('name', '%(value)s'), - ('value', 'value')]) + ('name', '%(value)s')]) def test_set_nonstring_types(self): cf = self.newconfig() From aef9aeab8e01377095518970154fbe0ba3c902c7 Mon Sep 17 00:00:00 2001 From: chrBrd Date: Mon, 23 Apr 2018 17:53:56 +0100 Subject: [PATCH 3/5] Amend solution so it's more Pythonic. --- Lib/configparser.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index c3b50738174fb9..a681d3990e7261 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -846,19 +846,16 @@ def items(self, section=_UNSET, raw=False, vars=None): except KeyError: if section != self.default_section: raise NoSectionError(section) + orig_keys = list(d.keys()) # Update with the entry specific variables if vars: for key, value in vars.items(): d[self.optionxform(key)] = value - # Change `vars` from None to a dict to avoid TypeError on return. - else: - vars = {} value_getter = lambda option: self._interpolation.before_get(self, section, option, d[option], d) if raw: value_getter = lambda option: d[option] - return [(option, value_getter(option)) for option in d.keys() - if option not in vars] + return [(option, value_getter(option)) for option in orig_keys] def popitem(self): """Remove a section from the parser and return it as From c4f70761c95e86a4714dcdf5abfd4d15646ddddc Mon Sep 17 00:00:00 2001 From: chrBrd Date: Mon, 23 Apr 2018 18:26:53 +0100 Subject: [PATCH 4/5] Add news entry. --- .../next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst diff --git a/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst b/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst new file mode 100644 index 00000000000000..bcbbce224ff2f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst @@ -0,0 +1,2 @@ +`ConfigParser.items` amended so any values passed in using the `vars` +argument are not included in the resulting output. From 1b6cf2dd5a5cabd1f1e810be259fbc0faf01a5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 23 Apr 2018 11:25:54 -0700 Subject: [PATCH 5/5] Update 2018-04-23-18-25-36.bpo-33251.C_K-J9.rst --- .../next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst b/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst index bcbbce224ff2f5..fc6861f0d368ec 100644 --- a/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst +++ b/Misc/NEWS.d/next/Library/2018-04-23-18-25-36.bpo-33251.C_K-J9.rst @@ -1,2 +1,2 @@ -`ConfigParser.items` amended so any values passed in using the `vars` -argument are not included in the resulting output. +`ConfigParser.items()` was fixed so that key-value pairs passed in via `vars` +are not included in the resulting output.