blob: 218c86024811026adeb353b43a875dad5bbe570e [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.}
6
7\index{warnings}
8
9
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
18\code{PyErr_Warn()}).
19
20Warning messages are normally written to \code{sys.stderr}, but their
21disposition can be changed flexibly, from ignoring all warnings to
22turning them into exceptions. The disposition of warnings can vary
23based on the warning category (see below), the text of the warning
24message, and the source location where it is issued. Repetitions of a
25particular warning for the same source location are typically
26suppressed.
27
28There are two stages in warning control: first, each time a warning is
29issued, a determination is made whether a message should be issued or
30not; next, if a message is to be issued, it is formatted and printed
31using a user-settable hook.
32
33The determination whether to issue a warning message is controlled by
34the warning filter, which is a sequence of matching rules and actions.
35Rules can be added to the filter by calling
36\function{filterwarnings()} and reset to its default state by calling
37\function{resetwarnings()}.
38
39The printing of warning messages is done by calling
40\function{showwarning()}, which may be overidden; the default
41implementation of this function formats the message by calling
42\function{formatwarning()}, which is also available for use by custom
43implementations.
44
45
Fred Drake1dea7602000-12-25 06:19:08 +000046\subsection{Warning Categories \label{warning-categories}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +000047
48There are a number of built-in exceptions that represent warning
49categories. This categorization is useful to be able to filter out
50groups of warnings. The following warnings category classes are
51currently defined:
52
53\begin{tableii}{l|l}{code}{Class}{Description}
54
55\lineii{Warning}{This is the base class of all warning category
56classes. It itself a subclass of Exception.}
57
58\lineii{UserWarning}{The default category for \function{warn()}.}
59
60\lineii{DeprecationWarning}{Base category for warnings about
61deprecated features.}
62
63\lineii{SyntaxWarning}{Base category for warnings about dubious
64syntactic features.}
65
66\lineii{RuntimeWarning}{Base category for warnings about dubious
67runtime features.}
68
69\end{tableii}
70
71While these are technically built-in exceptions, they are documented
72here, because conceptually they belong to the warnings mechanism.
73
74User code can define additional warning categories by subclassing one
75of the standard warning categories. A warning category must always be
76a subclass of the \exception{Warning} class.
77
78
Fred Drake1dea7602000-12-25 06:19:08 +000079\subsection{The Warnings Filter \label{warning-filter}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +000080
81The warnings filter controls whether warnings are ignored, displayed,
82or turned into errors (raising an exception).
83
84Conceptually, the warnings filter maintains an ordered list of filter
85specifications; any specific warning is matched against each filter
86specification in the list in turn until a match is found; the match
87determines the disposition of the match. Each entry is a tuple of the
88form (\var{action}, \var{message}, \var{category}, \var{module},
89\var{lineno}), where:
90
91\begin{itemize}
92
93\item \var{action} is one of the following strings:
94
95 \begin{tableii}{l|l}{code}{value}{disposition}
96
97 \lineii{\code{"error"}}{turn matching warnings into exceptions}
98
99 \lineii{\code{"ignore"}}{never print matching warnings}
100
101 \lineii{\code{"always"}}{always print matching warnings}
102
103 \lineii{\code{"default"}}{print the first occurrence of matching
104 warnings for each location where the warning is issued}
105
106 \lineii{\code{"module"}}{print the first occurrence of matching
107 warnings for each module where the warning is issued}
108
109 \lineii{\code{"once"}}{print only the first occurrence of matching
110 warnings, regardless of location}
111
112 \end{tableii}
113
114\item \var{message} is a compiled regular expression that the warning
115message must match (the match is case-insensitive)
116
117\item \var{category} is a class (a subclass of \exception{Warning}) of
118 which the warning category must be a subclass in order to match
119
120\item \var{module} is a compiled regular expression that the module
121 name must match
122
123\item \var{lineno} is an integer that the line number where the
124 warning occurred must match, or \code{0} to match all line
125 numbers
126
127\end{itemize}
128
129Since the \exception{Warning} class is derived from the built-in
130\exception{Exception} class, to turn a warning into an error we simply
131raise \code{category(message)}.
132
133The warnings filter is initialized by \samp{-W} options passed to the
134Python interpreter command line. The interpreter saves the arguments
135for all \samp{-W} options without interpretation in
136\code{sys.warnoptions}; the \module{warnings} module parses these when
137it is first imported (invalid options are ignored, after printing a
138message to \code{sys.stderr}).
139
140
Fred Drake1dea7602000-12-25 06:19:08 +0000141\subsection{Available Functions \label{warning-functions}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000142
143\begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}}
144Issue a warning, or maybe ignore it or raise an exception. The
145\var{category} argument, if given, must be a warning category class
146(see above); it defaults to \exception{UserWarning}. This function
147raises an exception if the particular warning issued is changed
148into an error by the warnings filter see above. The \var{stacklevel}
149argument can be used by wrapper functions written in Python, like
150this:
151
152\begin{verbatim}
153def deprecation(message):
154 warnings.warn(message, DeprecationWarning, level=2)
155\end{verbatim}
156
157This makes the warning refer to \function{deprecation()}'s caller,
158rather than to the source of \function{deprecation()} itself (since
159the latter would defeat the purpose of the warning message).
160\end{funcdesc}
161
162\begin{funcdesc}{showwarning}{message, category, filename,
163 lineno\optional{, file}}
164Write a warning to a file. The default implementation calls
165\code{showwarning(\var{message}, \var{category}, \var{filename},
166\var{lineno})} and writes the resulting string to \var{file}, which
167defaults to \code{sys.stderr}. You may replace this function with an
168alternative implementation by assigning to
169\code{warnings.showwarning}.
170\end{funcdesc}
171
172\begin{funcdesc}{formatwarning}{message, category, filename, lineno}
173Format a warning the standard way. This returns a string which may
174contain embedded newlines and ends in a newline.
175\end{funcdesc}
176
177\begin{funcdesc}{filterwarnings}{action\optional{,
Fred Drake1dea7602000-12-25 06:19:08 +0000178 message\optional{, category\optional{,
179 module\optional{, lineno}}}}}
Guido van Rossum5db5ba12000-12-19 06:01:23 +0000180Insert an entry into the list of warnings filters (at the front).
181This checks the types of the arguments, compiles the message and
182module regular expressions, and inserts them as a tuple in front
183of the warnings filter. Entries inserted later override entries
184inserted earlier, if both match a particular warning. Omitted
185arguments default to a value that matches everything.
186\end{funcdesc}
187
188\begin{funcdesc}{resetwarnings}{}
189Reset the warnings filter. This discards the effect of all previous
190calls to \function{filterwarnings()}, including that of the \samp{-W}
191command line options.
192\end{funcdesc}