From 8fe0a945a4d65c00893e95e9a6b8383264d0f696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Fri, 8 Jun 2018 02:16:26 -0700 Subject: [PATCH 1/3] bpo-33802: Do not interpolate in ConfigParser while reading defaults This solves a regression in logging config due to changes in BPO-23835. --- Lib/configparser.py | 12 ++++++++++-- Lib/test/test_logging.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index c88605feff7877..ea788aec510079 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -1208,8 +1208,16 @@ def add_section(self, section): 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 5098866237c864..5efe219a50e0cf 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1451,6 +1451,43 @@ def test_logger_disabling(self): 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() + with tempfile.NamedTemporaryFile(mode='w+t', encoding='utf-8') as fp: + fp.write(ini) + fp.flush() + logging.config.fileConfig(fp.name, 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" + }, + }, + )) + class SocketHandlerTest(BaseTest): From ad238bbb4499e50c3f5202702857b3cdba812a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Fri, 8 Jun 2018 02:42:05 -0700 Subject: [PATCH 2/3] Don't try to re-open an open temporary file; that doesn't work on Windows --- Lib/test/test_logging.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 5efe219a50e0cf..7831064345f4f5 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1473,20 +1473,27 @@ def test_defaults_do_no_interpolation(self): formatter=default handlers=console """).strip() - with tempfile.NamedTemporaryFile(mode='w+t', encoding='utf-8') as fp: - fp.write(ini) - fp.flush() - logging.config.fileConfig(fp.name, 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" + try: + with tempfile.NamedTemporaryFile( + mode='w+t', encoding='utf-8', delete='False' + ) as ntf: + ntf.write(ini) + logging.config.fileConfig( + ntf.name, + 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(ntf.name) class SocketHandlerTest(BaseTest): From 89bbe75907409496a876fb1a024a773262ee2ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Fri, 8 Jun 2018 03:16:09 -0700 Subject: [PATCH 3/3] Use mkstemp, couldn't get NamedTemporaryFile to work --- Lib/test/test_logging.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 7831064345f4f5..ba70b117d1e69c 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1473,13 +1473,12 @@ def test_defaults_do_no_interpolation(self): formatter=default handlers=console """).strip() + fd, fn = tempfile.mkstemp(prefix='test_logging_', suffix='.ini') try: - with tempfile.NamedTemporaryFile( - mode='w+t', encoding='utf-8', delete='False' - ) as ntf: - ntf.write(ini) + os.write(fd, ini.encode('ascii')) + os.close(fd) logging.config.fileConfig( - ntf.name, + fn, defaults=dict( version=1, disable_existing_loggers=False, @@ -1493,7 +1492,7 @@ def test_defaults_do_no_interpolation(self): ) ) finally: - os.unlink(ntf.name) + os.unlink(fn) class SocketHandlerTest(BaseTest):