| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="warning-functions.html" /> |
| 13 | <link rel="prev" href="warning-categories.html" /> |
| 14 | <link rel="parent" href="module-warnings.html" /> |
| 15 | <link rel="next" href="warning-functions.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.20.2 The Warnings Filter </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.20.1 Warning Categories" |
| 25 | href="warning-categories.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3.20 warnings " |
| 28 | href="module-warnings.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.20.3 Available Functions" |
| 31 | href="warning-functions.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="warning-categories.html">3.20.1 Warning Categories</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-warnings.html">3.20 warnings </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="warning-functions.html">3.20.3 Available Functions</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0052020000000000000000"></A><A NAME="warning-filter"></A> |
| 56 | <BR> |
| 57 | 3.20.2 The Warnings Filter |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | The warnings filter controls whether warnings are ignored, displayed, |
| 62 | or turned into errors (raising an exception). |
| 63 | |
| 64 | <P> |
| 65 | Conceptually, the warnings filter maintains an ordered list of filter |
| 66 | specifications; any specific warning is matched against each filter |
| 67 | specification in the list in turn until a match is found; the match |
| 68 | determines the disposition of the match. Each entry is a tuple of the |
| 69 | form (<var>action</var>, <var>message</var>, <var>category</var>, <var>module</var>, |
| 70 | <var>lineno</var>), where: |
| 71 | |
| 72 | <P> |
| 73 | |
| 74 | <UL> |
| 75 | <LI><var>action</var> is one of the following strings: |
| 76 | |
| 77 | <P> |
| 78 | <div class="center"><table class="realtable"> |
| 79 | <thead> |
| 80 | <tr> |
| 81 | <th class="left" >Value</th> |
| 82 | <th class="left" >Disposition</th> |
| 83 | </tr> |
| 84 | </thead> |
| 85 | <tbody> |
| 86 | <P> |
| 87 | |
| 88 | <tr><td class="left" valign="baseline"><code>"error"</code></td> |
| 89 | <td class="left" >turn matching warnings into exceptions</td></tr><P> |
| 90 | |
| 91 | <tr><td class="left" valign="baseline"><code>"ignore"</code></td> |
| 92 | <td class="left" >never print matching warnings</td></tr><P> |
| 93 | |
| 94 | <tr><td class="left" valign="baseline"><code>"always"</code></td> |
| 95 | <td class="left" >always print matching warnings</td></tr><P> |
| 96 | |
| 97 | <tr><td class="left" valign="baseline"><code>"default"</code></td> |
| 98 | <td class="left" >print the first occurrence of matching |
| 99 | warnings for each location where the warning is issued</td></tr><P> |
| 100 | |
| 101 | <tr><td class="left" valign="baseline"><code>"module"</code></td> |
| 102 | <td class="left" >print the first occurrence of matching |
| 103 | warnings for each module where the warning is issued</td></tr><P> |
| 104 | |
| 105 | <tr><td class="left" valign="baseline"><code>"once"</code></td> |
| 106 | <td class="left" >print only the first occurrence of matching |
| 107 | warnings, regardless of location</td></tr><P> |
| 108 | |
| 109 | </tbody> |
| 110 | </table></div> |
| 111 | |
| 112 | <P> |
| 113 | </LI> |
| 114 | <LI><var>message</var> is a string containing a regular expression that |
| 115 | the warning message must match (the match is compiled to always be |
| 116 | case-insensitive) |
| 117 | |
| 118 | <P> |
| 119 | </LI> |
| 120 | <LI><var>category</var> is a class (a subclass of <tt class="exception">Warning</tt>) of |
| 121 | which the warning category must be a subclass in order to match |
| 122 | |
| 123 | <P> |
| 124 | </LI> |
| 125 | <LI><var>module</var> is a string containing a regular expression that the module |
| 126 | name must match (the match is compiled to be case-sensitive) |
| 127 | |
| 128 | <P> |
| 129 | </LI> |
| 130 | <LI><var>lineno</var> is an integer that the line number where the |
| 131 | warning occurred must match, or <code>0</code> to match all line |
| 132 | numbers |
| 133 | |
| 134 | <P> |
| 135 | </LI> |
| 136 | </UL> |
| 137 | |
| 138 | <P> |
| 139 | Since the <tt class="exception">Warning</tt> class is derived from the built-in |
| 140 | <tt class="exception">Exception</tt> class, to turn a warning into an error we simply |
| 141 | raise <code>category(message)</code>. |
| 142 | |
| 143 | <P> |
| 144 | The warnings filter is initialized by <b class="programopt">-W</b> options passed |
| 145 | to the Python interpreter command line. The interpreter saves the |
| 146 | arguments for all <b class="programopt">-W</b> options without interpretation in |
| 147 | <code>sys.warnoptions</code>; the <tt class="module">warnings</tt> module parses these when |
| 148 | it is first imported (invalid options are ignored, after printing a |
| 149 | message to <code>sys.stderr</code>). |
| 150 | |
| 151 | <P> |
| 152 | |
| 153 | <DIV CLASS="navigation"> |
| 154 | <div class='online-navigation'> |
| 155 | <p></p><hr /> |
| 156 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 157 | <tr> |
| 158 | <td class='online-navigation'><a rel="prev" title="3.20.1 Warning Categories" |
| 159 | href="warning-categories.html"><img src='../icons/previous.png' |
| 160 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 161 | <td class='online-navigation'><a rel="parent" title="3.20 warnings " |
| 162 | href="module-warnings.html"><img src='../icons/up.png' |
| 163 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 164 | <td class='online-navigation'><a rel="next" title="3.20.3 Available Functions" |
| 165 | href="warning-functions.html"><img src='../icons/next.png' |
| 166 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 167 | <td align="center" width="100%">Python Library Reference</td> |
| 168 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 169 | href="contents.html"><img src='../icons/contents.png' |
| 170 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 171 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 172 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 173 | <td class='online-navigation'><a rel="index" title="Index" |
| 174 | href="genindex.html"><img src='../icons/index.png' |
| 175 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 176 | </tr></table> |
| 177 | <div class='online-navigation'> |
| 178 | <b class="navlabel">Previous:</b> |
| 179 | <a class="sectref" rel="prev" href="warning-categories.html">3.20.1 Warning Categories</A> |
| 180 | <b class="navlabel">Up:</b> |
| 181 | <a class="sectref" rel="parent" href="module-warnings.html">3.20 warnings </A> |
| 182 | <b class="navlabel">Next:</b> |
| 183 | <a class="sectref" rel="next" href="warning-functions.html">3.20.3 Available Functions</A> |
| 184 | </div> |
| 185 | </div> |
| 186 | <hr /> |
| 187 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 188 | </DIV> |
| 189 | <!--End of Navigation Panel--> |
| 190 | <ADDRESS> |
| 191 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 192 | </ADDRESS> |
| 193 | </BODY> |
| 194 | </HTML> |