]> granicus.if.org Git - python/commitdiff
[Apply SF patch #504943]
authorWalter Dörwald <walter@livinglogic.de>
Thu, 21 Mar 2002 10:38:40 +0000 (10:38 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Thu, 21 Mar 2002 10:38:40 +0000 (10:38 +0000)
This patch makes it possible to pass Warning instances as the first
argument to warnings.warn. In this case the category argument
will be ignored. The message text used will be str(warninginstance).

Doc/lib/libwarnings.tex
Lib/warnings.py
Misc/NEWS

index b7c7d63551b6df419a43ebb2c62a4f17702c5c13..573d99c163ba70a55861131ab8732d037fd55902 100644 (file)
@@ -145,7 +145,10 @@ message to \code{sys.stderr}).
 \begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}}
 Issue a warning, or maybe ignore it or raise an exception.  The
 \var{category} argument, if given, must be a warning category class
-(see above); it defaults to \exception{UserWarning}.  This function
+(see above); it defaults to \exception{UserWarning}.  Alternatively
+\var{message} can be a \exception{Warning} instance, in which case
+\var{category} will be ignore and \code{message.__class__} will be used.
+In this case the message text will be \code{str(message)}. This function
 raises an exception if the particular warning issued is changed
 into an error by the warnings filter see above.  The \var{stacklevel}
 argument can be used by wrapper functions written in Python, like
@@ -169,6 +172,9 @@ filename and line number, and optionally the module name and the
 registry (which should be the \code{__warningregistry__} dictionary of
 the module).  The module name defaults to the filename with \code{.py}
 stripped; if no registry is passed, the warning is never suppressed.
+\var{message} must be a string and \var{category} a subclass of
+\exception{Warning} or \var{message} may be a \exception{Warning} instance,
+in which case \var{category} will be ignored.
 \end{funcdesc}
 
 \begin{funcdesc}{showwarning}{message, category, filename,
index 5bb00c19758cf846f9e15fa98dd8c79ea4155b13..7ae98209699f13bcbd83578cef3add8121c8a73f 100644 (file)
@@ -11,6 +11,9 @@ onceregistry = {}
 
 def warn(message, category=None, stacklevel=1):
     """Issue a warning, or maybe ignore it or raise an exception."""
+    # Check if message is already a Warning object
+    if isinstance(message, Warning):
+       category = message.__class__
     # Check category argument
     if category is None:
         category = UserWarning
@@ -49,14 +52,20 @@ def warn_explicit(message, category, filename, lineno,
             module = module[:-3] # XXX What about leading pathname?
     if registry is None:
         registry = {}
-    key = (message, category, lineno)
+    if isinstance(message, Warning):
+       text = str(message)
+       category = message.__class__
+    else:
+       text = message
+       message = category(message)
+    key = (text, category, lineno)
     # Quick test for common case
     if registry.get(key):
         return
     # Search the filters
     for item in filters:
         action, msg, cat, mod, ln = item
-        if (msg.match(message) and
+        if (msg.match(text) and
             issubclass(category, cat) and
             mod.match(module) and
             (ln == 0 or lineno == ln)):
@@ -68,11 +77,11 @@ def warn_explicit(message, category, filename, lineno,
         registry[key] = 1
         return
     if action == "error":
-        raise category(message)
+        raise message
     # Other actions
     if action == "once":
         registry[key] = 1
-        oncekey = (message, category)
+        oncekey = (text, category)
         if onceregistry.get(oncekey):
             return
         onceregistry[oncekey] = 1
@@ -80,7 +89,7 @@ def warn_explicit(message, category, filename, lineno,
         pass
     elif action == "module":
         registry[key] = 1
-        altkey = (message, category, 0)
+        altkey = (text, category, 0)
         if registry.get(altkey):
             return
         registry[altkey] = 1
index ada779e807c8da70110b8f362e236afd2606d745..4373f1af4284d7e858f817e921d81d2837c88973 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -70,6 +70,8 @@ Library
 
 - distutils bdist commands now offer a --skip-build option.
 
+- warnings.warn now accepts a Warning instance as first argument.
+
 Tools/Demos
 
 Build