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

Skip to content

Commit 5db5ba1

Browse files
committed
Document the warnings module.
1 parent dea764d commit 5db5ba1

3 files changed

Lines changed: 193 additions & 0 deletions

File tree

Doc/Makefile.deps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ LIBFILES= $(MANSTYLES) $(COMMONTEX) \
5858
../lib/libshelve.tex \
5959
../lib/libcopy.tex \
6060
../lib/libmarshal.tex \
61+
../lib/libwarnings.tex \
6162
../lib/libimp.tex \
6263
../lib/libparser.tex \
6364
../lib/libbltin.tex \

Doc/lib/lib.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ \chapter*{Front Matter\label{front}}
8585
\input{libshelve}
8686
\input{libcopy}
8787
\input{libmarshal}
88+
\input{libwarnings}
8889
\input{libimp}
8990
%\input{libni}
9091
\input{libcode}

Doc/lib/libwarnings.tex

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
\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+
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+
\code{PyErr_Warn()}).
19+
20+
Warning messages are normally written to \code{sys.stderr}, but their
21+
disposition can be changed flexibly, from ignoring all warnings to
22+
turning them into exceptions. The disposition of warnings can vary
23+
based on the warning category (see below), the text of the warning
24+
message, and the source location where it is issued. Repetitions of a
25+
particular warning for the same source location are typically
26+
suppressed.
27+
28+
There are two stages in warning control: first, each time a warning is
29+
issued, a determination is made whether a message should be issued or
30+
not; next, if a message is to be issued, it is formatted and printed
31+
using a user-settable hook.
32+
33+
The determination whether to issue a warning message is controlled by
34+
the warning filter, which is a sequence of matching rules and actions.
35+
Rules can be added to the filter by calling
36+
\function{filterwarnings()} and reset to its default state by calling
37+
\function{resetwarnings()}.
38+
39+
The printing of warning messages is done by calling
40+
\function{showwarning()}, which may be overidden; the default
41+
implementation of this function formats the message by calling
42+
\function{formatwarning()}, which is also available for use by custom
43+
implementations.
44+
45+
46+
\subsection{Warning Categories}
47+
48+
There are a number of built-in exceptions that represent warning
49+
categories. This categorization is useful to be able to filter out
50+
groups of warnings. The following warnings category classes are
51+
currently defined:
52+
53+
\begin{tableii}{l|l}{code}{Class}{Description}
54+
55+
\lineii{Warning}{This is the base class of all warning category
56+
classes. 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
61+
deprecated features.}
62+
63+
\lineii{SyntaxWarning}{Base category for warnings about dubious
64+
syntactic features.}
65+
66+
\lineii{RuntimeWarning}{Base category for warnings about dubious
67+
runtime features.}
68+
69+
\end{tableii}
70+
71+
While these are technically built-in exceptions, they are documented
72+
here, because conceptually they belong to the warnings mechanism.
73+
74+
User code can define additional warning categories by subclassing one
75+
of the standard warning categories. A warning category must always be
76+
a subclass of the \exception{Warning} class.
77+
78+
79+
\subsection{The Warnings Filter}
80+
81+
The warnings filter controls whether warnings are ignored, displayed,
82+
or turned into errors (raising an exception).
83+
84+
Conceptually, the warnings filter maintains an ordered list of filter
85+
specifications; any specific warning is matched against each filter
86+
specification in the list in turn until a match is found; the match
87+
determines the disposition of the match. Each entry is a tuple of the
88+
form (\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
115+
message 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+
129+
Since the \exception{Warning} class is derived from the built-in
130+
\exception{Exception} class, to turn a warning into an error we simply
131+
raise \code{category(message)}.
132+
133+
The warnings filter is initialized by \samp{-W} options passed to the
134+
Python interpreter command line. The interpreter saves the arguments
135+
for all \samp{-W} options without interpretation in
136+
\code{sys.warnoptions}; the \module{warnings} module parses these when
137+
it is first imported (invalid options are ignored, after printing a
138+
message to \code{sys.stderr}).
139+
140+
141+
\subsection{Available Functions}
142+
143+
\begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}}
144+
Issue 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
147+
raises an exception if the particular warning issued is changed
148+
into an error by the warnings filter see above. The \var{stacklevel}
149+
argument can be used by wrapper functions written in Python, like
150+
this:
151+
152+
\begin{verbatim}
153+
def deprecation(message):
154+
warnings.warn(message, DeprecationWarning, level=2)
155+
\end{verbatim}
156+
157+
This makes the warning refer to \function{deprecation()}'s caller,
158+
rather than to the source of \function{deprecation()} itself (since
159+
the latter would defeat the purpose of the warning message).
160+
\end{funcdesc}
161+
162+
\begin{funcdesc}{showwarning}{message, category, filename,
163+
lineno\optional{, file}}
164+
Write 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
167+
defaults to \code{sys.stderr}. You may replace this function with an
168+
alternative implementation by assigning to
169+
\code{warnings.showwarning}.
170+
\end{funcdesc}
171+
172+
\begin{funcdesc}{formatwarning}{message, category, filename, lineno}
173+
Format a warning the standard way. This returns a string which may
174+
contain embedded newlines and ends in a newline.
175+
\end{funcdesc}
176+
177+
\begin{funcdesc}{filterwarnings}{action\optional{,
178+
message\optional{, category\optional{, module\optional{, lineno}}}}}
179+
Insert an entry into the list of warnings filters (at the front).
180+
This checks the types of the arguments, compiles the message and
181+
module regular expressions, and inserts them as a tuple in front
182+
of the warnings filter. Entries inserted later override entries
183+
inserted earlier, if both match a particular warning. Omitted
184+
arguments default to a value that matches everything.
185+
\end{funcdesc}
186+
187+
\begin{funcdesc}{resetwarnings}{}
188+
Reset the warnings filter. This discards the effect of all previous
189+
calls to \function{filterwarnings()}, including that of the \samp{-W}
190+
command line options.
191+
\end{funcdesc}

0 commit comments

Comments
 (0)