summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2008-05-05 05:32:07 (GMT)
committerBrett Cannon <bcannon@gmail.com>2008-05-05 05:32:07 (GMT)
commit8a232cc385343c17d9f615f0aff49fc378bdebae (patch)
tree35dbd69774e9a32275293638dcfaeb322dc8790b /Lib/warnings.py
parent9ae080ee5acad3c6207db75843cf1a27f868c540 (diff)
downloadcpython-8a232cc385343c17d9f615f0aff49fc378bdebae.zip
cpython-8a232cc385343c17d9f615f0aff49fc378bdebae.tar.gz
cpython-8a232cc385343c17d9f615f0aff49fc378bdebae.tar.bz2
Add a DeprecationWarning for when warnings.showwarning() is set to a function
that lacks support for the new 'line' argument.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 3c7357b..cefa961 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -3,6 +3,7 @@
# Note: function level imports should *not* be used
# in this module as it may cause import lock deadlock.
# See bug 683658.
+import inspect
import linecache
import sys
import types
@@ -21,7 +22,7 @@ def warnpy3k(message, category=None, stacklevel=1):
category = DeprecationWarning
warn(message, category, stacklevel+1)
-def showwarning(message, category, filename, lineno, file=None, line=None):
+def _show_warning(message, category, filename, lineno, file=None, line=None):
"""Hook to write a warning to a file; replace if you like."""
if file is None:
file = sys.stderr
@@ -29,6 +30,9 @@ def showwarning(message, category, filename, lineno, file=None, line=None):
file.write(formatwarning(message, category, filename, lineno, line))
except IOError:
pass # the file (probably stderr) is invalid - this warning gets lost.
+# Keep a worrking version around in case the deprecation of the old API is
+# triggered.
+showwarning = _show_warning
def formatwarning(message, category, filename, lineno, line=None):
"""Function to format a warning the standard way."""
@@ -259,6 +263,15 @@ def warn_explicit(message, category, filename, lineno,
"Unrecognized action (%r) in warnings.filters:\n %s" %
(action, item))
# Print message and context
+ if inspect.isfunction(showwarning):
+ arg_spec = inspect.getargspec(showwarning)
+ if 'line' not in arg_spec.args:
+ showwarning_msg = ("functions overriding warnings.showwarning() "
+ "must support the 'line' argument")
+ if message == showwarning_msg:
+ _show_warning(message, category, filename, lineno)
+ else:
+ warn(showwarning_msg, DeprecationWarning)
showwarning(message, category, filename, lineno)