From f44203d782e397941c17d96e6a1f9dc1df08b3e6 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 8 Jun 2018 07:01:56 -0700 Subject: [PATCH] bpo-33802: Do not interpolate in ConfigParser while reading defaults (GH-7524) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This solves a regression in logging config due to changes in BPO-23835. (cherry picked from commit 214f18e49feb6a9d6c05aa09a4bb304905e81334) Co-authored-by: Łukasz Langa --- Lib/configparser.py | 12 +++++++++-- Lib/test/test_logging.py | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 33dc9b9046..445fa99ccb 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -1206,8 +1206,16 @@ class ConfigParser(RawConfigParser): def _read_defaults(self, defaults): """Reads the defaults passed in the initializer, implicitly converting - values to strings like the rest of the API.""" - self.read_dict({self.default_section: defaults}) + values to strings like the rest of the API. + + Does not perform interpolation for backwards compatibility. + """ + try: + hold_interpolation = self._interpolation + self._interpolation = Interpolation() + self.read_dict({self.default_section: defaults}) + finally: + self._interpolation = hold_interpolation class SafeConfigParser(ConfigParser): diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index eee2ed0c19..0fa3892e57 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1451,6 +1451,49 @@ class ConfigFileTest(BaseTest): self.apply_config(self.disable_test, disable_existing_loggers=False) self.assertFalse(logger.disabled) + def test_defaults_do_no_interpolation(self): + """bpo-33802 defaults should not get interpolated""" + ini = textwrap.dedent(""" + [formatters] + keys=default + + [formatter_default] + + [handlers] + keys=console + + [handler_console] + class=logging.StreamHandler + args=tuple() + + [loggers] + keys=root + + [logger_root] + formatter=default + handlers=console + """).strip() + fd, fn = tempfile.mkstemp(prefix='test_logging_', suffix='.ini') + try: + os.write(fd, ini.encode('ascii')) + os.close(fd) + logging.config.fileConfig( + fn, + defaults=dict( + version=1, + disable_existing_loggers=False, + formatters={ + "generic": { + "format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s", + "datefmt": "[%Y-%m-%d %H:%M:%S %z]", + "class": "logging.Formatter" + }, + }, + ) + ) + finally: + os.unlink(fn) + class SocketHandlerTest(BaseTest): -- 2.40.0