blob: d829b10ea606d8761060458410925fe7dcd63528 [file] [log] [blame]
Guido van Rossum5db5ba12000-12-19 06:01:23 +00001\section{\module{warnings} ---
2 Warning control}
3
4\declaremodule{standard}{warnings}
5\modulesynopsis{Issue warning messages and control their disposition.}
Guido van Rossum5db5ba12000-12-19 06:01:23 +00006\index{warnings}
7
Fred Drake288927f2001-01-04 05:59:37 +00008\versionadded{2.1}
Guido van Rossum5db5ba12000-12-19 06:01:23 +00009
10Warning messages are typically issued in situations where it is useful
11to alert the user of some condition in a program, where that condition
12(normally) doesn't warrant raising an exception and terminating the
13program. For example, one might want to issue a warning when a
14program uses an obsolete module.
15
16Python programmers issue warnings by calling the \function{warn()}
17function defined in this module. (C programmers use
Fred Drake288927f2001-01-04 05:59:37 +000018\cfunction{PyErr_Warn()}; see the
19\citetitle[../api/exceptionHandling.html]{Python/C API Reference
20Manual} for details).
Guido van Rossum5db5ba12000-12-19 06:01:23 +000021
22Warning messages are normally written to \code{sys.stderr}, but their
23disposition can be changed flexibly, from ignoring all warnings to
24turning them into exceptions. The disposition of warnings can vary
25based on the warning category (see below), the text of the warning
26message, and the source location where it is issued. Repetitions of a
27particular warning for the same source location are typically
28suppressed.
29
30There are two stages in warning control: first, each time a warning is
31issued, a determination is made whether a message should be issued or
32not; next, if a message is to be issued, it is formatted and printed
33using a user-settable hook.
34
35The determination whether to issue a warning message is controlled by
36the warning filter, which is a sequence of matching rules and actions.
37Rules can be added to the filter by calling
38\function{filterwarnings()} and reset to its default state by calling
39\function{resetwarnings()}.
40
41The printing of warning messages is done by calling
42\function{showwarning()}, which may be overidden; the default
43implementation of this function formats the message by calling
44\function{formatwarning()}, which is also available for use by custom
45implementations.
46
47
Fred Drake1dea7602000-12-25 06:19:08 +000048\subsection{Warning Categories \label{warning-categories}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +000049
50There are a number of built-in exceptions that represent warning
51categories. This categorization is useful to be able to filter out
52groups of warnings. The following warnings category classes are
53currently defined:
54
Fred Drake288927f2001-01-04 05:59:37 +000055\begin{tableii}{l|l}{exception}{Class}{Description}
Guido van Rossum5db5ba12000-12-19 06:01:23 +000056
57\lineii{Warning}{This is the base class of all warning category
Fred Drakece575ba2001-08-14 21:51:50 +000058classes. It is a subclass of \exception{Exception}.}
Guido van Rossum5db5ba12000-12-19 06:01:23 +000059
60\lineii{UserWarning}{The default category for \function{warn()}.}
61
62\lineii{DeprecationWarning}{Base category for warnings about
63deprecated features.}
64
65\lineii{SyntaxWarning}{Base category for warnings about dubious
66syntactic features.}
67
68\lineii{RuntimeWarning}{Base category for warnings about dubious
69runtime features.}
70
Barry Warsawb8c20a72002-08-14 16:40:54 +000071\lineii{FutureWarning}{Base category for warnings about constructs
72that will change semantically in the future.}
73
Guido van Rossum5db5ba12000-12-19 06:01:23 +000074\end{tableii}
75
76While these are technically built-in exceptions, they are documented
77here, because conceptually they belong to the warnings mechanism.
78
79User code can define additional warning categories by subclassing one
80of the standard warning categories. A warning category must always be
81a subclass of the \exception{Warning} class.
82
83
Fred Drake1dea7602000-12-25 06:19:08 +000084\subsection{The Warnings Filter \label{warning-filter}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +000085
86The warnings filter controls whether warnings are ignored, displayed,
87or turned into errors (raising an exception).
88
89Conceptually, the warnings filter maintains an ordered list of filter
90specifications; any specific warning is matched against each filter
91specification in the list in turn until a match is found; the match
92determines the disposition of the match. Each entry is a tuple of the
93form (\var{action}, \var{message}, \var{category}, \var{module},
94\var{lineno}), where:
95
96\begin{itemize}
97
98\item \var{action} is one of the following strings:
99
Fred Drake288927f2001-01-04 05:59:37 +0000100 \begin{tableii}{l|l}{code}{Value}{Disposition}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000101
Fred Drake288927f2001-01-04 05:59:37 +0000102 \lineii{"error"}{turn matching warnings into exceptions}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000103
Fred Drake288927f2001-01-04 05:59:37 +0000104 \lineii{"ignore"}{never print matching warnings}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000105
Fred Drake288927f2001-01-04 05:59:37 +0000106 \lineii{"always"}{always print matching warnings}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000107
Fred Drake288927f2001-01-04 05:59:37 +0000108 \lineii{"default"}{print the first occurrence of matching
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000109 warnings for each location where the warning is issued}
110
Fred Drake288927f2001-01-04 05:59:37 +0000111 \lineii{"module"}{print the first occurrence of matching
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000112 warnings for each module where the warning is issued}
113
Fred Drake288927f2001-01-04 05:59:37 +0000114 \lineii{"once"}{print only the first occurrence of matching
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000115 warnings, regardless of location}
116
117 \end{tableii}
118
Andrew M. Kuchlingba375242003-02-06 14:38:45 +0000119\item \var{message} is a string containing a regular expression that
120the warning message must match (the match is compiled to always be
121case-insensitive)
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000122
123\item \var{category} is a class (a subclass of \exception{Warning}) of
124 which the warning category must be a subclass in order to match
125
Andrew M. Kuchlingba375242003-02-06 14:38:45 +0000126\item \var{module} is a string containing a regular expression that the module
127 name must match (the match is compiled to be case-sensitive)
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000128
129\item \var{lineno} is an integer that the line number where the
130 warning occurred must match, or \code{0} to match all line
131 numbers
132
133\end{itemize}
134
135Since the \exception{Warning} class is derived from the built-in
136\exception{Exception} class, to turn a warning into an error we simply
137raise \code{category(message)}.
138
Fred Drake288927f2001-01-04 05:59:37 +0000139The warnings filter is initialized by \programopt{-W} options passed
140to the Python interpreter command line. The interpreter saves the
141arguments for all \programopt{-W} options without interpretation in
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000142\code{sys.warnoptions}; the \module{warnings} module parses these when
143it is first imported (invalid options are ignored, after printing a
144message to \code{sys.stderr}).
145
146
Fred Drake1dea7602000-12-25 06:19:08 +0000147\subsection{Available Functions \label{warning-functions}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000148
149\begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}}
150Issue a warning, or maybe ignore it or raise an exception. The
151\var{category} argument, if given, must be a warning category class
Walter Dörwaldb25c2b02002-03-21 10:38:40 +0000152(see above); it defaults to \exception{UserWarning}. Alternatively
153\var{message} can be a \exception{Warning} instance, in which case
Neal Norwitze22d3df2002-03-21 12:58:54 +0000154\var{category} will be ignored and \code{message.__class__} will be used.
Walter Dörwaldb25c2b02002-03-21 10:38:40 +0000155In this case the message text will be \code{str(message)}. This function
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000156raises an exception if the particular warning issued is changed
157into an error by the warnings filter see above. The \var{stacklevel}
158argument can be used by wrapper functions written in Python, like
159this:
160
161\begin{verbatim}
162def deprecation(message):
Fred Drakef9816172002-03-12 19:49:31 +0000163 warnings.warn(message, DeprecationWarning, stacklevel=2)
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000164\end{verbatim}
165
166This makes the warning refer to \function{deprecation()}'s caller,
167rather than to the source of \function{deprecation()} itself (since
168the latter would defeat the purpose of the warning message).
169\end{funcdesc}
170
Guido van Rossum56ba72a2001-02-28 23:34:21 +0000171\begin{funcdesc}{warn_explicit}{message, category, filename,
172 lineno\optional{, module\optional{, registry}}}
173This is a low-level interface to the functionality of
174\function{warn()}, passing in explicitly the message, category,
175filename and line number, and optionally the module name and the
176registry (which should be the \code{__warningregistry__} dictionary of
177the module). The module name defaults to the filename with \code{.py}
178stripped; if no registry is passed, the warning is never suppressed.
Walter Dörwaldb25c2b02002-03-21 10:38:40 +0000179\var{message} must be a string and \var{category} a subclass of
180\exception{Warning} or \var{message} may be a \exception{Warning} instance,
181in which case \var{category} will be ignored.
Guido van Rossum56ba72a2001-02-28 23:34:21 +0000182\end{funcdesc}
183
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000184\begin{funcdesc}{showwarning}{message, category, filename,
185 lineno\optional{, file}}
186Write a warning to a file. The default implementation calls
Tim Peters93ceaea2003-07-01 14:37:59 +0000187\code{formatwarning(\var{message}, \var{category}, \var{filename},
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000188\var{lineno})} and writes the resulting string to \var{file}, which
189defaults to \code{sys.stderr}. You may replace this function with an
190alternative implementation by assigning to
191\code{warnings.showwarning}.
192\end{funcdesc}
193
194\begin{funcdesc}{formatwarning}{message, category, filename, lineno}
195Format a warning the standard way. This returns a string which may
196contain embedded newlines and ends in a newline.
197\end{funcdesc}
198
199\begin{funcdesc}{filterwarnings}{action\optional{,
Fred Drake1dea7602000-12-25 06:19:08 +0000200 message\optional{, category\optional{,
Guido van Rossum77b20f02001-01-14 14:10:18 +0000201 module\optional{, lineno\optional{, append}}}}}}
202Insert an entry into the list of warnings filters. The entry is
203inserted at the front by default; if \var{append} is true, it is
204inserted at the end.
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000205This checks the types of the arguments, compiles the message and
206module regular expressions, and inserts them as a tuple in front
207of the warnings filter. Entries inserted later override entries
208inserted earlier, if both match a particular warning. Omitted
209arguments default to a value that matches everything.
210\end{funcdesc}
211
212\begin{funcdesc}{resetwarnings}{}
213Reset the warnings filter. This discards the effect of all previous
Fred Drake288927f2001-01-04 05:59:37 +0000214calls to \function{filterwarnings()}, including that of the
215\programopt{-W} command line options.
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000216\end{funcdesc}