[Python-checkins] cpython (merge 3.3 -> default): Closes #20242: Merged fix from 3.3.

vinay.sajip python-checkins at python.org
Mon Jan 13 23:01:31 CET 2014


http://hg.python.org/cpython/rev/c1605d24fb35
changeset:   88448:c1605d24fb35
parent:      88446:cd728dc893c9
parent:      88447:e7fcf0d8008f
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Mon Jan 13 22:01:16 2014 +0000
summary:
  Closes #20242: Merged fix from 3.3.

files:
  Lib/logging/__init__.py  |  17 ++++++++++-------
  Lib/test/test_logging.py |  16 ++++++++++++++++
  Misc/NEWS                |   3 +++
  3 files changed, 29 insertions(+), 7 deletions(-)


diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -390,10 +390,12 @@
     def format(self, record):
         return self._tpl.substitute(**record.__dict__)
 
+BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
+
 _STYLES = {
-    '%': PercentStyle,
-    '{': StrFormatStyle,
-    '$': StringTemplateStyle
+    '%': (PercentStyle, BASIC_FORMAT),
+    '{': (StrFormatStyle, '{levelname}:{name}:{message}'),
+    '$': (StringTemplateStyle, '${levelname}:${name}:${message}'),
 }
 
 class Formatter(object):
@@ -458,7 +460,7 @@
         if style not in _STYLES:
             raise ValueError('Style must be one of: %s' % ','.join(
                              _STYLES.keys()))
-        self._style = _STYLES[style](fmt)
+        self._style = _STYLES[style][0](fmt)
         self._fmt = self._style._fmt
         self.datefmt = datefmt
 
@@ -1643,8 +1645,6 @@
 # Configuration classes and functions
 #---------------------------------------------------------------------------
 
-BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
-
 def basicConfig(**kwargs):
     """
     Do basic configuration for the logging system.
@@ -1718,9 +1718,12 @@
                     stream = kwargs.get("stream")
                     h = StreamHandler(stream)
                 handlers = [h]
-            fs = kwargs.get("format", BASIC_FORMAT)
             dfs = kwargs.get("datefmt", None)
             style = kwargs.get("style", '%')
+            if style not in _STYLES:
+                raise ValueError('Style must be one of: %s' % ','.join(
+                                 _STYLES.keys()))
+            fs = kwargs.get("format", _STYLES[style][1])
             fmt = Formatter(fs, dfs, style)
             for h in handlers:
                 if h.formatter is None:
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3517,6 +3517,22 @@
         # level is not explicitly set
         self.assertEqual(logging.root.level, self.original_logging_level)
 
+    def test_strformatstyle(self):
+        with captured_stdout() as output:
+            logging.basicConfig(stream=sys.stdout, style="{")
+            logging.error("Log an error")
+            sys.stdout.seek(0)
+            self.assertEqual(output.getvalue().strip(),
+                "ERROR:root:Log an error")
+
+    def test_stringtemplatestyle(self):
+        with captured_stdout() as output:
+            logging.basicConfig(stream=sys.stdout, style="$")
+            logging.error("Log an error")
+            sys.stdout.seek(0)
+            self.assertEqual(output.getvalue().strip(),
+                "ERROR:root:Log an error")
+
     def test_filename(self):
 
         def cleanup(h1, h2, fn):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@
 Library
 -------
 
+- Issue #20242: Fixed basicConfig() format strings for the alternative
+  formatting styles. Thanks to kespindler for the bug report and patch.
+
 - Issues #20206 and #5803: Fix edge case in email.quoprimime.encode where it
   truncated lines ending in a character needing encoding but no newline by
   using a more efficient algorithm that doesn't have the bug.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list