[Python-checkins] cpython: Fix test_logging

victor.stinner python-checkins at python.org
Fri Mar 18 21:19:03 EDT 2016


https://hg.python.org/cpython/rev/9c92352324e8
changeset:   100599:9c92352324e8
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Mar 19 02:11:56 2016 +0100
summary:
  Fix test_logging

Issue #26568: Fix implementation of showwarning() and formatwarning() for
test_logging.

files:
  Lib/warnings.py |  60 ++++++++++++++++++++----------------
  1 files changed, 33 insertions(+), 27 deletions(-)


diff --git a/Lib/warnings.py b/Lib/warnings.py
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -10,30 +10,14 @@
 def showwarning(message, category, filename, lineno, file=None, line=None):
     """Hook to write a warning to a file; replace if you like."""
     msg = WarningMessage(message, category, filename, lineno, file, line)
-    _showwarnmsg(msg)
+    _showwarnmsg_impl(msg)
 
 def formatwarning(message, category, filename, lineno, line=None):
     """Function to format a warning the standard way."""
     msg = WarningMessage(message, category, filename, lineno, None, line)
-    return _formatwarnmsg(msg)
+    return _formatwarnmsg_impl(msg)
 
-# Keep references to check if the functions were replaced
-_showwarning = showwarning
-_formatwarning = formatwarning
-
-def _showwarnmsg(msg):
-    """Hook to write a warning to a file; replace if you like."""
-    showwarning = globals().get('showwarning', _showwarning)
-    if showwarning is not _showwarning:
-        # warnings.showwarning() was replaced
-        if not callable(showwarning):
-            raise TypeError("warnings.showwarning() must be set to a "
-                            "function or method")
-
-        showwarning(msg.message, msg.category, msg.filename, msg.lineno,
-                    msg.file, msg.line)
-        return
-
+def _showwarnmsg_impl(msg):
     file = msg.file
     if file is None:
         file = sys.stderr
@@ -48,14 +32,7 @@
         # the file (probably stderr) is invalid - this warning gets lost.
         pass
 
-def _formatwarnmsg(msg):
-    """Function to format a warning the standard way."""
-    formatwarning = globals().get('formatwarning', _formatwarning)
-    if formatwarning is not _formatwarning:
-        # warnings.formatwarning() was replaced
-        return formatwarning(msg.message, msg.category,
-                             msg.filename, msg.lineno, line=msg.line)
-
+def _formatwarnmsg_impl(msg):
     import linecache
     s =  ("%s:%s: %s: %s\n"
           % (msg.filename, msg.lineno, msg.category.__name__,
@@ -81,6 +58,35 @@
                     s += '    %s\n' % line
     return s
 
+# Keep a reference to check if the function was replaced
+_showwarning = showwarning
+
+def _showwarnmsg(msg):
+    """Hook to write a warning to a file; replace if you like."""
+    showwarning = globals().get('showwarning', _showwarning)
+    if showwarning is not _showwarning:
+        # warnings.showwarning() was replaced
+        if not callable(showwarning):
+            raise TypeError("warnings.showwarning() must be set to a "
+                            "function or method")
+
+        showwarning(msg.message, msg.category, msg.filename, msg.lineno,
+                    msg.file, msg.line)
+        return
+    _showwarnmsg_impl(msg)
+
+# Keep a reference to check if the function was replaced
+_formatwarning = formatwarning
+
+def _formatwarnmsg(msg):
+    """Function to format a warning the standard way."""
+    formatwarning = globals().get('formatwarning', _formatwarning)
+    if formatwarning is not _formatwarning:
+        # warnings.formatwarning() was replaced
+        return formatwarning(msg.message, msg.category,
+                             msg.filename, msg.lineno, line=msg.line)
+    return _formatwarnmsg_impl(msg)
+
 def filterwarnings(action, message="", category=Warning, module="", lineno=0,
                    append=False):
     """Insert an entry into the list of warnings filters (at the front).

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


More information about the Python-checkins mailing list