]> granicus.if.org Git - python/commitdiff
bpo-33802: Do not interpolate in ConfigParser while reading defaults (GH-7524)
authorŁukasz Langa <lukasz@langa.pl>
Fri, 8 Jun 2018 11:02:48 +0000 (04:02 -0700)
committerGitHub <noreply@github.com>
Fri, 8 Jun 2018 11:02:48 +0000 (04:02 -0700)
This solves a regression in logging config due to changes in BPO-23835.

Lib/configparser.py
Lib/test/test_logging.py

index c88605feff7877d45105870246a2fba320cc8f21..ea788aec51007900ff8effecd4d28f687d2597ca 100644 (file)
@@ -1208,8 +1208,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):
index 5098866237c8648b1db30fb4299c2185e2fef8f7..ba70b117d1e69cf208fb2af557788739cbc48373 100644 (file)
@@ -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):