summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-19 01:11:56 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-03-19 01:11:56 (GMT)
commiteedf13fe231b49b10c9a836ae8c9120d215deb84 (patch)
treea6b511bcc8340116ad8e1dc7cdf19d17fdcf96d3 /Lib/warnings.py
parent914cde89d4c94b0b9206d0fa22322a1142833a56 (diff)
downloadcpython-eedf13fe231b49b10c9a836ae8c9120d215deb84.zip
cpython-eedf13fe231b49b10c9a836ae8c9120d215deb84.tar.gz
cpython-eedf13fe231b49b10c9a836ae8c9120d215deb84.tar.bz2
Fix test_logging
Issue #26568: Fix implementation of showwarning() and formatwarning() for test_logging.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py60
1 files changed, 33 insertions, 27 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 1566065..9fb21a8 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -10,30 +10,14 @@ __all__ = ["warn", "warn_explicit", "showwarning",
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)
-
-# 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
+ return _formatwarnmsg_impl(msg)
+def _showwarnmsg_impl(msg):
file = msg.file
if file is None:
file = sys.stderr
@@ -48,14 +32,7 @@ def _showwarnmsg(msg):
# 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 @@ def _formatwarnmsg(msg):
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).