From 01db1ce794edd4244108997558d2fb42c08c39b4 Mon Sep 17 00:00:00 2001 From: bugstan <29607505+bugstan@users.noreply.github.com> Date: Mon, 26 Oct 2026 06:11:08 +0700 Subject: [PATCH] gh-160684: Normalize non-string vars in ConfigParser.items() --- Lib/configparser.py | 2 ++ Lib/test/test_configparser.py | 15 +++++++++++++++ ...2026-10-26-06-10-00.gh-issue-160684.vArs42.rst | 2 ++ 3 files changed, 19 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst diff --git a/Lib/configparser.py b/Lib/configparser.py index f28aba7c3db7b6d..3a3b6d26f28feea 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -991,6 +991,8 @@ def items(self, section=_UNSET, raw=False, vars=None): # Update with the entry specific variables if vars: for key, value in vars.items(): + if value is not None: + value = str(value) d[self.optionxform(key)] = value value_getter = lambda option: self._interpolation.before_get( self, section, option, d[option], d diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index f24d7b869991c22..394ffb5ad4aee77 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -904,6 +904,21 @@ def check_items_config(self, expected): with self.assertRaises(configparser.NoSectionError): cf.items("no such section") + def test_items_non_string_vars(self): + cf = self.newconfig(defaults={'default': 'original'}) + cf.read_dict({'section': {'value': 'original'}}) + for value in (42, 0, False): + for raw in (False, True): + with self.subTest(value=value, raw=raw): + self.assertEqual( + dict(cf.items('section', raw=raw, vars={ + 'default': value, + 'VALUE': value, + 'extra': value, + })), + {'default': str(value), 'value': str(value)}, + ) + def test_popitem(self): cf = self.fromstring( f""" diff --git a/Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst b/Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst new file mode 100644 index 000000000000000..d4a57c66aea3c94 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-26-06-10-00.gh-issue-160684.vArs42.rst @@ -0,0 +1,2 @@ +Fix :meth:`configparser.ConfigParser.items` to convert non-``None`` values +passed in *vars* to strings, consistently with :meth:`~configparser.ConfigParser.get`.