1 | \section{\module{warnings} ---
|
---|
2 | Warning control}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{warnings}
|
---|
5 | \modulesynopsis{Issue warning messages and control their disposition.}
|
---|
6 | \index{warnings}
|
---|
7 |
|
---|
8 | \versionadded{2.1}
|
---|
9 |
|
---|
10 | Warning messages are typically issued in situations where it is useful
|
---|
11 | to alert the user of some condition in a program, where that condition
|
---|
12 | (normally) doesn't warrant raising an exception and terminating the
|
---|
13 | program. For example, one might want to issue a warning when a
|
---|
14 | program uses an obsolete module.
|
---|
15 |
|
---|
16 | Python programmers issue warnings by calling the \function{warn()}
|
---|
17 | function defined in this module. (C programmers use
|
---|
18 | \cfunction{PyErr_Warn()}; see the
|
---|
19 | \citetitle[../api/exceptionHandling.html]{Python/C API Reference
|
---|
20 | Manual} for details).
|
---|
21 |
|
---|
22 | Warning messages are normally written to \code{sys.stderr}, but their
|
---|
23 | disposition can be changed flexibly, from ignoring all warnings to
|
---|
24 | turning them into exceptions. The disposition of warnings can vary
|
---|
25 | based on the warning category (see below), the text of the warning
|
---|
26 | message, and the source location where it is issued. Repetitions of a
|
---|
27 | particular warning for the same source location are typically
|
---|
28 | suppressed.
|
---|
29 |
|
---|
30 | There are two stages in warning control: first, each time a warning is
|
---|
31 | issued, a determination is made whether a message should be issued or
|
---|
32 | not; next, if a message is to be issued, it is formatted and printed
|
---|
33 | using a user-settable hook.
|
---|
34 |
|
---|
35 | The determination whether to issue a warning message is controlled by
|
---|
36 | the warning filter, which is a sequence of matching rules and actions.
|
---|
37 | Rules can be added to the filter by calling
|
---|
38 | \function{filterwarnings()} and reset to its default state by calling
|
---|
39 | \function{resetwarnings()}.
|
---|
40 |
|
---|
41 | The printing of warning messages is done by calling
|
---|
42 | \function{showwarning()}, which may be overridden; the default
|
---|
43 | implementation of this function formats the message by calling
|
---|
44 | \function{formatwarning()}, which is also available for use by custom
|
---|
45 | implementations.
|
---|
46 |
|
---|
47 |
|
---|
48 | \subsection{Warning Categories \label{warning-categories}}
|
---|
49 |
|
---|
50 | There are a number of built-in exceptions that represent warning
|
---|
51 | categories. This categorization is useful to be able to filter out
|
---|
52 | groups of warnings. The following warnings category classes are
|
---|
53 | currently defined:
|
---|
54 |
|
---|
55 | \begin{tableii}{l|l}{exception}{Class}{Description}
|
---|
56 |
|
---|
57 | \lineii{Warning}{This is the base class of all warning category
|
---|
58 | classes. It is a subclass of \exception{Exception}.}
|
---|
59 |
|
---|
60 | \lineii{UserWarning}{The default category for \function{warn()}.}
|
---|
61 |
|
---|
62 | \lineii{DeprecationWarning}{Base category for warnings about
|
---|
63 | deprecated features.}
|
---|
64 |
|
---|
65 | \lineii{SyntaxWarning}{Base category for warnings about dubious
|
---|
66 | syntactic features.}
|
---|
67 |
|
---|
68 | \lineii{RuntimeWarning}{Base category for warnings about dubious
|
---|
69 | runtime features.}
|
---|
70 |
|
---|
71 | \lineii{FutureWarning}{Base category for warnings about constructs
|
---|
72 | that will change semantically in the future.}
|
---|
73 |
|
---|
74 | \lineii{PendingDeprecationWarning}{Base category for warnings about
|
---|
75 | features that will be deprecated in the future (ignored by default).}
|
---|
76 |
|
---|
77 | \lineii{ImportWarning}{Base category for warnings triggered during the
|
---|
78 | process of importing a module (ignored by default).}
|
---|
79 |
|
---|
80 | \lineii{UnicodeWarning}{Base category for warnings related to Unicode.}
|
---|
81 |
|
---|
82 | \end{tableii}
|
---|
83 |
|
---|
84 | While these are technically built-in exceptions, they are documented
|
---|
85 | here, because conceptually they belong to the warnings mechanism.
|
---|
86 |
|
---|
87 | User code can define additional warning categories by subclassing one
|
---|
88 | of the standard warning categories. A warning category must always be
|
---|
89 | a subclass of the \exception{Warning} class.
|
---|
90 |
|
---|
91 |
|
---|
92 | \subsection{The Warnings Filter \label{warning-filter}}
|
---|
93 |
|
---|
94 | The warnings filter controls whether warnings are ignored, displayed,
|
---|
95 | or turned into errors (raising an exception).
|
---|
96 |
|
---|
97 | Conceptually, the warnings filter maintains an ordered list of filter
|
---|
98 | specifications; any specific warning is matched against each filter
|
---|
99 | specification in the list in turn until a match is found; the match
|
---|
100 | determines the disposition of the match. Each entry is a tuple of the
|
---|
101 | form (\var{action}, \var{message}, \var{category}, \var{module},
|
---|
102 | \var{lineno}), where:
|
---|
103 |
|
---|
104 | \begin{itemize}
|
---|
105 |
|
---|
106 | \item \var{action} is one of the following strings:
|
---|
107 |
|
---|
108 | \begin{tableii}{l|l}{code}{Value}{Disposition}
|
---|
109 |
|
---|
110 | \lineii{"error"}{turn matching warnings into exceptions}
|
---|
111 |
|
---|
112 | \lineii{"ignore"}{never print matching warnings}
|
---|
113 |
|
---|
114 | \lineii{"always"}{always print matching warnings}
|
---|
115 |
|
---|
116 | \lineii{"default"}{print the first occurrence of matching
|
---|
117 | warnings for each location where the warning is issued}
|
---|
118 |
|
---|
119 | \lineii{"module"}{print the first occurrence of matching
|
---|
120 | warnings for each module where the warning is issued}
|
---|
121 |
|
---|
122 | \lineii{"once"}{print only the first occurrence of matching
|
---|
123 | warnings, regardless of location}
|
---|
124 |
|
---|
125 | \end{tableii}
|
---|
126 |
|
---|
127 | \item \var{message} is a string containing a regular expression that
|
---|
128 | the warning message must match (the match is compiled to always be
|
---|
129 | case-insensitive)
|
---|
130 |
|
---|
131 | \item \var{category} is a class (a subclass of \exception{Warning}) of
|
---|
132 | which the warning category must be a subclass in order to match
|
---|
133 |
|
---|
134 | \item \var{module} is a string containing a regular expression that the module
|
---|
135 | name must match (the match is compiled to be case-sensitive)
|
---|
136 |
|
---|
137 | \item \var{lineno} is an integer that the line number where the
|
---|
138 | warning occurred must match, or \code{0} to match all line
|
---|
139 | numbers
|
---|
140 |
|
---|
141 | \end{itemize}
|
---|
142 |
|
---|
143 | Since the \exception{Warning} class is derived from the built-in
|
---|
144 | \exception{Exception} class, to turn a warning into an error we simply
|
---|
145 | raise \code{category(message)}.
|
---|
146 |
|
---|
147 | The warnings filter is initialized by \programopt{-W} options passed
|
---|
148 | to the Python interpreter command line. The interpreter saves the
|
---|
149 | arguments for all \programopt{-W} options without interpretation in
|
---|
150 | \code{sys.warnoptions}; the \module{warnings} module parses these when
|
---|
151 | it is first imported (invalid options are ignored, after printing a
|
---|
152 | message to \code{sys.stderr}).
|
---|
153 |
|
---|
154 | The warnings that are ignored by default may be enabled by passing
|
---|
155 | \programopt{-Wd} to the interpreter. This enables default handling
|
---|
156 | for all warnings, including those that are normally ignored by
|
---|
157 | default. This is particular useful for enabling ImportWarning when
|
---|
158 | debugging problems importing a developed package. ImportWarning can
|
---|
159 | also be enabled explicitly in Python code using:
|
---|
160 |
|
---|
161 | \begin{verbatim}
|
---|
162 | warnings.simplefilter('default', ImportWarning)
|
---|
163 | \end{verbatim}
|
---|
164 |
|
---|
165 |
|
---|
166 | \subsection{Available Functions \label{warning-functions}}
|
---|
167 |
|
---|
168 | \begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}}
|
---|
169 | Issue a warning, or maybe ignore it or raise an exception. The
|
---|
170 | \var{category} argument, if given, must be a warning category class
|
---|
171 | (see above); it defaults to \exception{UserWarning}. Alternatively
|
---|
172 | \var{message} can be a \exception{Warning} instance, in which case
|
---|
173 | \var{category} will be ignored and \code{message.__class__} will be used.
|
---|
174 | In this case the message text will be \code{str(message)}. This function
|
---|
175 | raises an exception if the particular warning issued is changed
|
---|
176 | into an error by the warnings filter see above. The \var{stacklevel}
|
---|
177 | argument can be used by wrapper functions written in Python, like
|
---|
178 | this:
|
---|
179 |
|
---|
180 | \begin{verbatim}
|
---|
181 | def deprecation(message):
|
---|
182 | warnings.warn(message, DeprecationWarning, stacklevel=2)
|
---|
183 | \end{verbatim}
|
---|
184 |
|
---|
185 | This makes the warning refer to \function{deprecation()}'s caller,
|
---|
186 | rather than to the source of \function{deprecation()} itself (since
|
---|
187 | the latter would defeat the purpose of the warning message).
|
---|
188 | \end{funcdesc}
|
---|
189 |
|
---|
190 | \begin{funcdesc}{warn_explicit}{message, category, filename,
|
---|
191 | lineno\optional{, module\optional{, registry\optional{,
|
---|
192 | module_globals}}}}
|
---|
193 | This is a low-level interface to the functionality of
|
---|
194 | \function{warn()}, passing in explicitly the message, category,
|
---|
195 | filename and line number, and optionally the module name and the
|
---|
196 | registry (which should be the \code{__warningregistry__} dictionary of
|
---|
197 | the module). The module name defaults to the filename with \code{.py}
|
---|
198 | stripped; if no registry is passed, the warning is never suppressed.
|
---|
199 | \var{message} must be a string and \var{category} a subclass of
|
---|
200 | \exception{Warning} or \var{message} may be a \exception{Warning} instance,
|
---|
201 | in which case \var{category} will be ignored.
|
---|
202 |
|
---|
203 | \var{module_globals}, if supplied, should be the global namespace in use
|
---|
204 | by the code for which the warning is issued. (This argument is used to
|
---|
205 | support displaying source for modules found in zipfiles or other
|
---|
206 | non-filesystem import sources, and was added in Python 2.5.)
|
---|
207 | \end{funcdesc}
|
---|
208 |
|
---|
209 | \begin{funcdesc}{showwarning}{message, category, filename,
|
---|
210 | lineno\optional{, file}}
|
---|
211 | Write a warning to a file. The default implementation calls
|
---|
212 | \code{formatwarning(\var{message}, \var{category}, \var{filename},
|
---|
213 | \var{lineno})} and writes the resulting string to \var{file}, which
|
---|
214 | defaults to \code{sys.stderr}. You may replace this function with an
|
---|
215 | alternative implementation by assigning to
|
---|
216 | \code{warnings.showwarning}.
|
---|
217 | \end{funcdesc}
|
---|
218 |
|
---|
219 | \begin{funcdesc}{formatwarning}{message, category, filename, lineno}
|
---|
220 | Format a warning the standard way. This returns a string which may
|
---|
221 | contain embedded newlines and ends in a newline.
|
---|
222 | \end{funcdesc}
|
---|
223 |
|
---|
224 | \begin{funcdesc}{filterwarnings}{action\optional{,
|
---|
225 | message\optional{, category\optional{,
|
---|
226 | module\optional{, lineno\optional{, append}}}}}}
|
---|
227 | Insert an entry into the list of warnings filters. The entry is
|
---|
228 | inserted at the front by default; if \var{append} is true, it is
|
---|
229 | inserted at the end.
|
---|
230 | This checks the types of the arguments, compiles the message and
|
---|
231 | module regular expressions, and inserts them as a tuple in the
|
---|
232 | list of warnings filters. Entries closer to the front of the list
|
---|
233 | override entries later in the list, if both match a particular
|
---|
234 | warning. Omitted arguments default to a value that matches
|
---|
235 | everything.
|
---|
236 | \end{funcdesc}
|
---|
237 |
|
---|
238 | \begin{funcdesc}{simplefilter}{action\optional{,
|
---|
239 | category\optional{,
|
---|
240 | lineno\optional{, append}}}}
|
---|
241 | Insert a simple entry into the list of warnings filters. The meaning
|
---|
242 | of the function parameters is as for \function{filterwarnings()}, but
|
---|
243 | regular expressions are not needed as the filter inserted always
|
---|
244 | matches any message in any module as long as the category and line
|
---|
245 | number match.
|
---|
246 | \end{funcdesc}
|
---|
247 |
|
---|
248 | \begin{funcdesc}{resetwarnings}{}
|
---|
249 | Reset the warnings filter. This discards the effect of all previous
|
---|
250 | calls to \function{filterwarnings()}, including that of the
|
---|
251 | \programopt{-W} command line options and calls to
|
---|
252 | \function{simplefilter()}.
|
---|
253 | \end{funcdesc}
|
---|