<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"warning-functions.html" />
<link rel=
"prev" href=
"warning-categories.html" />
<link rel=
"parent" href=
"module-warnings.html" />
<link rel=
"next" href=
"warning-functions.html" />
<meta name='aesop' content='information'
/>
<title>3.20.2 The Warnings Filter
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.20.1 Warning Categories"
href=
"warning-categories.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3.20 warnings "
href=
"module-warnings.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.20.3 Available Functions"
href=
"warning-functions.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"warning-categories.html">3.20.1 Warning Categories
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-warnings.html">3.20 warnings
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"warning-functions.html">3.20.3 Available Functions
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION0052020000000000000000"></A><A NAME=
"warning-filter"></A>
3.20.2 The Warnings Filter
The warnings filter controls whether warnings are ignored, displayed,
or turned into errors (raising an exception).
Conceptually, the warnings filter maintains an ordered list of filter
specifications; any specific warning is matched against each filter
specification in the list in turn until a match is found; the match
determines the disposition of the match. Each entry is a tuple of the
form (
<var>action
</var>,
<var>message
</var>,
<var>category
</var>,
<var>module
</var>,
<var>lineno
</var>), where:
<LI><var>action
</var> is one of the following strings:
<div class=
"center"><table class=
"realtable">
<th class=
"left" >Value
</th>
<th class=
"left" >Disposition
</th>
<tr><td class=
"left" valign=
"baseline"><code>"error"</code></td>
<td class=
"left" >turn matching warnings into exceptions
</td></tr><P>
<tr><td class=
"left" valign=
"baseline"><code>"ignore"</code></td>
<td class=
"left" >never print matching warnings
</td></tr><P>
<tr><td class=
"left" valign=
"baseline"><code>"always"</code></td>
<td class=
"left" >always print matching warnings
</td></tr><P>
<tr><td class=
"left" valign=
"baseline"><code>"default"</code></td>
<td class=
"left" >print the first occurrence of matching
warnings for each location where the warning is issued
</td></tr><P>
<tr><td class=
"left" valign=
"baseline"><code>"module"</code></td>
<td class=
"left" >print the first occurrence of matching
warnings for each module where the warning is issued
</td></tr><P>
<tr><td class=
"left" valign=
"baseline"><code>"once"</code></td>
<td class=
"left" >print only the first occurrence of matching
warnings, regardless of location
</td></tr><P>
<LI><var>message
</var> is a string containing a regular expression that
the warning message must match (the match is compiled to always be
<LI><var>category
</var> is a class (a subclass of
<tt class=
"exception">Warning
</tt>) of
which the warning category must be a subclass in order to match
<LI><var>module
</var> is a string containing a regular expression that the module
name must match (the match is compiled to be case-sensitive)
<LI><var>lineno
</var> is an integer that the line number where the
warning occurred must match, or
<code>0</code> to match all line
Since the
<tt class=
"exception">Warning
</tt> class is derived from the built-in
<tt class=
"exception">Exception
</tt> class, to turn a warning into an error we simply
raise
<code>category(message)
</code>.
The warnings filter is initialized by
<b class=
"programopt">-W
</b> options passed
to the Python interpreter command line. The interpreter saves the
arguments for all
<b class=
"programopt">-W
</b> options without interpretation in
<code>sys.warnoptions
</code>; the
<tt class=
"module">warnings
</tt> module parses these when
it is first imported (invalid options are ignored, after printing a
message to
<code>sys.stderr
</code>).
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.20.1 Warning Categories"
href=
"warning-categories.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3.20 warnings "
href=
"module-warnings.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.20.3 Available Functions"
href=
"warning-functions.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"warning-categories.html">3.20.1 Warning Categories
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-warnings.html">3.20 warnings
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"warning-functions.html">3.20.3 Available Functions
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.