Fix test_logging
Issue #26568: Fix implementation of showwarning() and formatwarning() for test_logging.
This commit is contained in:
parent
914cde89d4
commit
eedf13fe23
|
@ -10,30 +10,14 @@ __all__ = ["warn", "warn_explicit", "showwarning",
|
||||||
def showwarning(message, category, filename, lineno, file=None, line=None):
|
def showwarning(message, category, filename, lineno, file=None, line=None):
|
||||||
"""Hook to write a warning to a file; replace if you like."""
|
"""Hook to write a warning to a file; replace if you like."""
|
||||||
msg = WarningMessage(message, category, filename, lineno, file, line)
|
msg = WarningMessage(message, category, filename, lineno, file, line)
|
||||||
_showwarnmsg(msg)
|
_showwarnmsg_impl(msg)
|
||||||
|
|
||||||
def formatwarning(message, category, filename, lineno, line=None):
|
def formatwarning(message, category, filename, lineno, line=None):
|
||||||
"""Function to format a warning the standard way."""
|
"""Function to format a warning the standard way."""
|
||||||
msg = WarningMessage(message, category, filename, lineno, None, line)
|
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
|
file = msg.file
|
||||||
if file is None:
|
if file is None:
|
||||||
file = sys.stderr
|
file = sys.stderr
|
||||||
|
@ -48,14 +32,7 @@ def _showwarnmsg(msg):
|
||||||
# the file (probably stderr) is invalid - this warning gets lost.
|
# the file (probably stderr) is invalid - this warning gets lost.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _formatwarnmsg(msg):
|
def _formatwarnmsg_impl(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)
|
|
||||||
|
|
||||||
import linecache
|
import linecache
|
||||||
s = ("%s:%s: %s: %s\n"
|
s = ("%s:%s: %s: %s\n"
|
||||||
% (msg.filename, msg.lineno, msg.category.__name__,
|
% (msg.filename, msg.lineno, msg.category.__name__,
|
||||||
|
@ -81,6 +58,35 @@ def _formatwarnmsg(msg):
|
||||||
s += ' %s\n' % line
|
s += ' %s\n' % line
|
||||||
return s
|
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,
|
def filterwarnings(action, message="", category=Warning, module="", lineno=0,
|
||||||
append=False):
|
append=False):
|
||||||
"""Insert an entry into the list of warnings filters (at the front).
|
"""Insert an entry into the list of warnings filters (at the front).
|
||||||
|
|
Loading…
Reference in New Issue