Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b25c2b0

Browse files
committed
[Apply SF patch #504943]
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).
1 parent 047c05e commit b25c2b0

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

Doc/lib/libwarnings.tex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ \subsection{Available Functions \label{warning-functions}}
145145
\begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}}
146146
Issue a warning, or maybe ignore it or raise an exception. The
147147
\var{category} argument, if given, must be a warning category class
148-
(see above); it defaults to \exception{UserWarning}. This function
148+
(see above); it defaults to \exception{UserWarning}. Alternatively
149+
\var{message} can be a \exception{Warning} instance, in which case
150+
\var{category} will be ignore and \code{message.__class__} will be used.
151+
In this case the message text will be \code{str(message)}. This function
149152
raises an exception if the particular warning issued is changed
150153
into an error by the warnings filter see above. The \var{stacklevel}
151154
argument can be used by wrapper functions written in Python, like
@@ -169,6 +172,9 @@ \subsection{Available Functions \label{warning-functions}}
169172
registry (which should be the \code{__warningregistry__} dictionary of
170173
the module). The module name defaults to the filename with \code{.py}
171174
stripped; if no registry is passed, the warning is never suppressed.
175+
\var{message} must be a string and \var{category} a subclass of
176+
\exception{Warning} or \var{message} may be a \exception{Warning} instance,
177+
in which case \var{category} will be ignored.
172178
\end{funcdesc}
173179

174180
\begin{funcdesc}{showwarning}{message, category, filename,

Lib/warnings.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
def warn(message, category=None, stacklevel=1):
1313
"""Issue a warning, or maybe ignore it or raise an exception."""
14+
# Check if message is already a Warning object
15+
if isinstance(message, Warning):
16+
category = message.__class__
1417
# Check category argument
1518
if category is None:
1619
category = UserWarning
@@ -49,14 +52,20 @@ def warn_explicit(message, category, filename, lineno,
4952
module = module[:-3] # XXX What about leading pathname?
5053
if registry is None:
5154
registry = {}
52-
key = (message, category, lineno)
55+
if isinstance(message, Warning):
56+
text = str(message)
57+
category = message.__class__
58+
else:
59+
text = message
60+
message = category(message)
61+
key = (text, category, lineno)
5362
# Quick test for common case
5463
if registry.get(key):
5564
return
5665
# Search the filters
5766
for item in filters:
5867
action, msg, cat, mod, ln = item
59-
if (msg.match(message) and
68+
if (msg.match(text) and
6069
issubclass(category, cat) and
6170
mod.match(module) and
6271
(ln == 0 or lineno == ln)):
@@ -68,19 +77,19 @@ def warn_explicit(message, category, filename, lineno,
6877
registry[key] = 1
6978
return
7079
if action == "error":
71-
raise category(message)
80+
raise message
7281
# Other actions
7382
if action == "once":
7483
registry[key] = 1
75-
oncekey = (message, category)
84+
oncekey = (text, category)
7685
if onceregistry.get(oncekey):
7786
return
7887
onceregistry[oncekey] = 1
7988
elif action == "always":
8089
pass
8190
elif action == "module":
8291
registry[key] = 1
83-
altkey = (message, category, 0)
92+
altkey = (text, category, 0)
8493
if registry.get(altkey):
8594
return
8695
registry[altkey] = 1

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Library
7070

7171
- distutils bdist commands now offer a --skip-build option.
7272

73+
- warnings.warn now accepts a Warning instance as first argument.
74+
7375
Tools/Demos
7476

7577
Build

0 commit comments

Comments
 (0)