| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`warnings` --- Warning control | 
 | 2 | =================================== | 
 | 3 |  | 
 | 4 | .. index:: single: warnings | 
 | 5 |  | 
 | 6 | .. module:: warnings | 
 | 7 |    :synopsis: Issue warning messages and control their disposition. | 
 | 8 |  | 
 | 9 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | Warning messages are typically issued in situations where it is useful to alert | 
 | 11 | the user of some condition in a program, where that condition (normally) doesn't | 
 | 12 | warrant raising an exception and terminating the program.  For example, one | 
 | 13 | might want to issue a warning when a program uses an obsolete module. | 
 | 14 |  | 
 | 15 | Python programmers issue warnings by calling the :func:`warn` function defined | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 16 | in this module.  (C programmers use :c:func:`PyErr_WarnEx`; see | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | :ref:`exceptionhandling` for details). | 
 | 18 |  | 
 | 19 | Warning messages are normally written to ``sys.stderr``, but their disposition | 
 | 20 | can be changed flexibly, from ignoring all warnings to turning them into | 
 | 21 | exceptions.  The disposition of warnings can vary based on the warning category | 
 | 22 | (see below), the text of the warning message, and the source location where it | 
 | 23 | is issued.  Repetitions of a particular warning for the same source location are | 
 | 24 | typically suppressed. | 
 | 25 |  | 
 | 26 | There are two stages in warning control: first, each time a warning is issued, a | 
 | 27 | determination is made whether a message should be issued or not; next, if a | 
 | 28 | message is to be issued, it is formatted and printed using a user-settable hook. | 
 | 29 |  | 
 | 30 | The determination whether to issue a warning message is controlled by the | 
 | 31 | warning filter, which is a sequence of matching rules and actions. Rules can be | 
 | 32 | added to the filter by calling :func:`filterwarnings` and reset to its default | 
 | 33 | state by calling :func:`resetwarnings`. | 
 | 34 |  | 
 | 35 | The printing of warning messages is done by calling :func:`showwarning`, which | 
 | 36 | may be overridden; the default implementation of this function formats the | 
 | 37 | message by calling :func:`formatwarning`, which is also available for use by | 
 | 38 | custom implementations. | 
 | 39 |  | 
 | 40 |  | 
 | 41 | .. _warning-categories: | 
 | 42 |  | 
 | 43 | Warning Categories | 
 | 44 | ------------------ | 
 | 45 |  | 
 | 46 | There are a number of built-in exceptions that represent warning categories. | 
 | 47 | This categorization is useful to be able to filter out groups of warnings.  The | 
 | 48 | following warnings category classes are currently defined: | 
 | 49 |  | 
 | 50 | +----------------------------------+-----------------------------------------------+ | 
 | 51 | | Class                            | Description                                   | | 
 | 52 | +==================================+===============================================+ | 
 | 53 | | :exc:`Warning`                   | This is the base class of all warning         | | 
 | 54 | |                                  | category classes.  It is a subclass of        | | 
 | 55 | |                                  | :exc:`Exception`.                             | | 
 | 56 | +----------------------------------+-----------------------------------------------+ | 
 | 57 | | :exc:`UserWarning`               | The default category for :func:`warn`.        | | 
 | 58 | +----------------------------------+-----------------------------------------------+ | 
 | 59 | | :exc:`DeprecationWarning`        | Base category for warnings about deprecated   | | 
| Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 60 | |                                  | features (ignored by default).                | | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | +----------------------------------+-----------------------------------------------+ | 
 | 62 | | :exc:`SyntaxWarning`             | Base category for warnings about dubious      | | 
 | 63 | |                                  | syntactic features.                           | | 
 | 64 | +----------------------------------+-----------------------------------------------+ | 
 | 65 | | :exc:`RuntimeWarning`            | Base category for warnings about dubious      | | 
 | 66 | |                                  | runtime features.                             | | 
 | 67 | +----------------------------------+-----------------------------------------------+ | 
 | 68 | | :exc:`FutureWarning`             | Base category for warnings about constructs   | | 
 | 69 | |                                  | that will change semantically in the future.  | | 
 | 70 | +----------------------------------+-----------------------------------------------+ | 
 | 71 | | :exc:`PendingDeprecationWarning` | Base category for warnings about features     | | 
 | 72 | |                                  | that will be deprecated in the future         | | 
 | 73 | |                                  | (ignored by default).                         | | 
 | 74 | +----------------------------------+-----------------------------------------------+ | 
 | 75 | | :exc:`ImportWarning`             | Base category for warnings triggered during   | | 
 | 76 | |                                  | the process of importing a module (ignored by | | 
 | 77 | |                                  | default).                                     | | 
 | 78 | +----------------------------------+-----------------------------------------------+ | 
 | 79 | | :exc:`UnicodeWarning`            | Base category for warnings related to         | | 
 | 80 | |                                  | Unicode.                                      | | 
 | 81 | +----------------------------------+-----------------------------------------------+ | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 82 | | :exc:`BytesWarning`              | Base category for warnings related to         | | 
 | 83 | |                                  | :class:`bytes` and :class:`buffer`.           | | 
 | 84 | +----------------------------------+-----------------------------------------------+ | 
| Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 85 | | :exc:`ResourceWarning`           | Base category for warnings related to         | | 
 | 86 | |                                  | resource usage.                               | | 
 | 87 | +----------------------------------+-----------------------------------------------+ | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 88 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 |  | 
 | 90 | While these are technically built-in exceptions, they are documented here, | 
 | 91 | because conceptually they belong to the warnings mechanism. | 
 | 92 |  | 
 | 93 | User code can define additional warning categories by subclassing one of the | 
 | 94 | standard warning categories.  A warning category must always be a subclass of | 
 | 95 | the :exc:`Warning` class. | 
 | 96 |  | 
 | 97 |  | 
 | 98 | .. _warning-filter: | 
 | 99 |  | 
 | 100 | The Warnings Filter | 
 | 101 | ------------------- | 
 | 102 |  | 
 | 103 | The warnings filter controls whether warnings are ignored, displayed, or turned | 
 | 104 | into errors (raising an exception). | 
 | 105 |  | 
 | 106 | Conceptually, the warnings filter maintains an ordered list of filter | 
 | 107 | specifications; any specific warning is matched against each filter | 
 | 108 | specification in the list in turn until a match is found; the match determines | 
 | 109 | the disposition of the match.  Each entry is a tuple of the form (*action*, | 
 | 110 | *message*, *category*, *module*, *lineno*), where: | 
 | 111 |  | 
 | 112 | * *action* is one of the following strings: | 
 | 113 |  | 
 | 114 |   +---------------+----------------------------------------------+ | 
 | 115 |   | Value         | Disposition                                  | | 
 | 116 |   +===============+==============================================+ | 
 | 117 |   | ``"error"``   | turn matching warnings into exceptions       | | 
 | 118 |   +---------------+----------------------------------------------+ | 
 | 119 |   | ``"ignore"``  | never print matching warnings                | | 
 | 120 |   +---------------+----------------------------------------------+ | 
 | 121 |   | ``"always"``  | always print matching warnings               | | 
 | 122 |   +---------------+----------------------------------------------+ | 
 | 123 |   | ``"default"`` | print the first occurrence of matching       | | 
 | 124 |   |               | warnings for each location where the warning | | 
 | 125 |   |               | is issued                                    | | 
 | 126 |   +---------------+----------------------------------------------+ | 
 | 127 |   | ``"module"``  | print the first occurrence of matching       | | 
 | 128 |   |               | warnings for each module where the warning   | | 
 | 129 |   |               | is issued                                    | | 
 | 130 |   +---------------+----------------------------------------------+ | 
 | 131 |   | ``"once"``    | print only the first occurrence of matching  | | 
 | 132 |   |               | warnings, regardless of location             | | 
 | 133 |   +---------------+----------------------------------------------+ | 
 | 134 |  | 
 | 135 | * *message* is a string containing a regular expression that the warning message | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 136 |   must match (the match is compiled to always be case-insensitive). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 |  | 
 | 138 | * *category* is a class (a subclass of :exc:`Warning`) of which the warning | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 139 |   category must be a subclass in order to match. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 |  | 
 | 141 | * *module* is a string containing a regular expression that the module name must | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 142 |   match (the match is compiled to be case-sensitive). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 |  | 
 | 144 | * *lineno* is an integer that the line number where the warning occurred must | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 145 |   match, or ``0`` to match all line numbers. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 |  | 
 | 147 | Since the :exc:`Warning` class is derived from the built-in :exc:`Exception` | 
 | 148 | class, to turn a warning into an error we simply raise ``category(message)``. | 
 | 149 |  | 
 | 150 | The warnings filter is initialized by :option:`-W` options passed to the Python | 
 | 151 | interpreter command line.  The interpreter saves the arguments for all | 
 | 152 | :option:`-W` options without interpretation in ``sys.warnoptions``; the | 
 | 153 | :mod:`warnings` module parses these when it is first imported (invalid options | 
 | 154 | are ignored, after printing a message to ``sys.stderr``). | 
 | 155 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 |  | 
| Georg Brandl | 2062937 | 2010-10-24 15:16:02 +0000 | [diff] [blame] | 157 | Default Warning Filters | 
 | 158 | ~~~~~~~~~~~~~~~~~~~~~~~ | 
 | 159 |  | 
 | 160 | By default, Python installs several warning filters, which can be overridden by | 
 | 161 | the command-line options passed to :option:`-W` and calls to | 
 | 162 | :func:`filterwarnings`. | 
 | 163 |  | 
 | 164 | * :exc:`DeprecationWarning` and :exc:`PendingDeprecationWarning`, and | 
 | 165 |   :exc:`ImportWarning` are ignored. | 
 | 166 |  | 
 | 167 | * :exc:`BytesWarning` is ignored unless the :option:`-b` option is given once or | 
 | 168 |   twice; in this case this warning is either printed (``-b``) or turned into an | 
| Georg Brandl | 1920890 | 2010-10-26 06:59:23 +0000 | [diff] [blame] | 169 |   exception (``-bb``). | 
| Georg Brandl | 2062937 | 2010-10-24 15:16:02 +0000 | [diff] [blame] | 170 |  | 
 | 171 | * :exc:`ResourceWarning` is ignored unless Python was built in debug mode. | 
 | 172 |  | 
 | 173 | .. versionchanged:: 3.2 | 
 | 174 |    :exc:`DeprecationWarning` is now ignored by default in addition to | 
 | 175 |    :exc:`PendingDeprecationWarning`. | 
 | 176 |  | 
 | 177 |  | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 178 | .. _warning-suppress: | 
 | 179 |  | 
 | 180 | Temporarily Suppressing Warnings | 
 | 181 | -------------------------------- | 
 | 182 |  | 
| Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 183 | If you are using code that you know will raise a warning, such as a deprecated | 
 | 184 | function, but do not want to see the warning, then it is possible to suppress | 
 | 185 | the warning using the :class:`catch_warnings` context manager:: | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 186 |  | 
 | 187 |     import warnings | 
 | 188 |  | 
 | 189 |     def fxn(): | 
 | 190 |         warnings.warn("deprecated", DeprecationWarning) | 
 | 191 |  | 
 | 192 |     with warnings.catch_warnings(): | 
 | 193 |         warnings.simplefilter("ignore") | 
 | 194 |         fxn() | 
 | 195 |  | 
 | 196 | While within the context manager all warnings will simply be ignored. This | 
 | 197 | allows you to use known-deprecated code without having to see the warning while | 
 | 198 | not suppressing the warning for other code that might not be aware of its use | 
| Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 199 | of deprecated code.  Note: this can only be guaranteed in a single-threaded | 
 | 200 | application. If two or more threads use the :class:`catch_warnings` context | 
 | 201 | manager at the same time, the behavior is undefined. | 
 | 202 |  | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 203 |  | 
 | 204 |  | 
 | 205 | .. _warning-testing: | 
 | 206 |  | 
 | 207 | Testing Warnings | 
 | 208 | ---------------- | 
 | 209 |  | 
 | 210 | To test warnings raised by code, use the :class:`catch_warnings` context | 
 | 211 | manager. With it you can temporarily mutate the warnings filter to facilitate | 
 | 212 | your testing. For instance, do the following to capture all raised warnings to | 
 | 213 | check:: | 
 | 214 |  | 
 | 215 |     import warnings | 
 | 216 |  | 
 | 217 |     def fxn(): | 
 | 218 |         warnings.warn("deprecated", DeprecationWarning) | 
 | 219 |  | 
 | 220 |     with warnings.catch_warnings(record=True) as w: | 
 | 221 |         # Cause all warnings to always be triggered. | 
 | 222 |         warnings.simplefilter("always") | 
 | 223 |         # Trigger a warning. | 
 | 224 |         fxn() | 
 | 225 |         # Verify some things | 
 | 226 |         assert len(w) == 1 | 
| Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 227 |         assert issubclass(w[-1].category, DeprecationWarning) | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 228 |         assert "deprecated" in str(w[-1].message) | 
 | 229 |  | 
 | 230 | One can also cause all warnings to be exceptions by using ``error`` instead of | 
 | 231 | ``always``. One thing to be aware of is that if a warning has already been | 
 | 232 | raised because of a ``once``/``default`` rule, then no matter what filters are | 
 | 233 | set the warning will not be seen again unless the warnings registry related to | 
 | 234 | the warning has been cleared. | 
 | 235 |  | 
 | 236 | Once the context manager exits, the warnings filter is restored to its state | 
 | 237 | when the context was entered. This prevents tests from changing the warnings | 
 | 238 | filter in unexpected ways between tests and leading to indeterminate test | 
| Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 239 | results. The :func:`showwarning` function in the module is also restored to | 
| Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 240 | its original value.  Note: this can only be guaranteed in a single-threaded | 
 | 241 | application. If two or more threads use the :class:`catch_warnings` context | 
 | 242 | manager at the same time, the behavior is undefined. | 
| Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 243 |  | 
 | 244 | When testing multiple operations that raise the same kind of warning, it | 
 | 245 | is important to test them in a manner that confirms each operation is raising | 
 | 246 | a new warning (e.g. set warnings to be raised as exceptions and check the | 
 | 247 | operations raise exceptions, check that the length of the warning list | 
 | 248 | continues to increase after each operation, or else delete the previous | 
 | 249 | entries from the warnings list before each new operation). | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 250 |  | 
 | 251 |  | 
| Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 252 | Updating Code For New Versions of Python | 
 | 253 | ---------------------------------------- | 
 | 254 |  | 
 | 255 | Warnings that are only of interest to the developer are ignored by default. As | 
 | 256 | such you should make sure to test your code with typically ignored warnings | 
 | 257 | made visible. You can do this from the command-line by passing :option:`-Wd` | 
 | 258 | to the interpreter (this is shorthand for :option:`-W default`).  This enables | 
 | 259 | default handling for all warnings, including those that are ignored by default. | 
 | 260 | To change what action is taken for encountered warnings you simply change what | 
 | 261 | argument is passed to :option:`-W`, e.g. :option:`-W error`. See the | 
 | 262 | :option:`-W` flag for more details on what is possible. | 
 | 263 |  | 
 | 264 | To programmatically do the same as :option:`-Wd`, use:: | 
 | 265 |  | 
 | 266 |   warnings.simplefilter('default') | 
 | 267 |  | 
 | 268 | Make sure to execute this code as soon as possible. This prevents the | 
 | 269 | registering of what warnings have been raised from unexpectedly influencing how | 
 | 270 | future warnings are treated. | 
 | 271 |  | 
 | 272 | Having certain warnings ignored by default is done to prevent a user from | 
 | 273 | seeing warnings that are only of interest to the developer. As you do not | 
 | 274 | necessarily have control over what interpreter a user uses to run their code, | 
 | 275 | it is possible that a new version of Python will be released between your | 
 | 276 | release cycles.  The new interpreter release could trigger new warnings in your | 
 | 277 | code that were not there in an older interpreter, e.g. | 
 | 278 | :exc:`DeprecationWarning` for a module that you are using. While you as a | 
 | 279 | developer want to be notified that your code is using a deprecated module, to a | 
 | 280 | user this information is essentially noise and provides no benefit to them. | 
 | 281 |  | 
 | 282 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 283 | .. _warning-functions: | 
 | 284 |  | 
 | 285 | Available Functions | 
 | 286 | ------------------- | 
 | 287 |  | 
 | 288 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 289 | .. function:: warn(message, category=None, stacklevel=1) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 |  | 
 | 291 |    Issue a warning, or maybe ignore it or raise an exception.  The *category* | 
 | 292 |    argument, if given, must be a warning category class (see above); it defaults to | 
 | 293 |    :exc:`UserWarning`.  Alternatively *message* can be a :exc:`Warning` instance, | 
 | 294 |    in which case *category* will be ignored and ``message.__class__`` will be used. | 
 | 295 |    In this case the message text will be ``str(message)``. This function raises an | 
 | 296 |    exception if the particular warning issued is changed into an error by the | 
 | 297 |    warnings filter see above.  The *stacklevel* argument can be used by wrapper | 
 | 298 |    functions written in Python, like this:: | 
 | 299 |  | 
 | 300 |       def deprecation(message): | 
 | 301 |           warnings.warn(message, DeprecationWarning, stacklevel=2) | 
 | 302 |  | 
 | 303 |    This makes the warning refer to :func:`deprecation`'s caller, rather than to the | 
 | 304 |    source of :func:`deprecation` itself (since the latter would defeat the purpose | 
 | 305 |    of the warning message). | 
 | 306 |  | 
 | 307 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 308 | .. function:: warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 |  | 
 | 310 |    This is a low-level interface to the functionality of :func:`warn`, passing in | 
 | 311 |    explicitly the message, category, filename and line number, and optionally the | 
 | 312 |    module name and the registry (which should be the ``__warningregistry__`` | 
 | 313 |    dictionary of the module).  The module name defaults to the filename with | 
 | 314 |    ``.py`` stripped; if no registry is passed, the warning is never suppressed. | 
 | 315 |    *message* must be a string and *category* a subclass of :exc:`Warning` or | 
 | 316 |    *message* may be a :exc:`Warning` instance, in which case *category* will be | 
 | 317 |    ignored. | 
 | 318 |  | 
 | 319 |    *module_globals*, if supplied, should be the global namespace in use by the code | 
 | 320 |    for which the warning is issued.  (This argument is used to support displaying | 
| Christian Heimes | 3279b5d | 2007-12-09 15:58:13 +0000 | [diff] [blame] | 321 |    source for modules found in zipfiles or other non-filesystem import | 
 | 322 |    sources). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 323 |  | 
 | 324 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 325 | .. function:: showwarning(message, category, filename, lineno, file=None, line=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 |  | 
 | 327 |    Write a warning to a file.  The default implementation calls | 
| Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 328 |    ``formatwarning(message, category, filename, lineno, line)`` and writes the | 
 | 329 |    resulting string to *file*, which defaults to ``sys.stderr``.  You may replace | 
 | 330 |    this function with an alternative implementation by assigning to | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 |    ``warnings.showwarning``. | 
| Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 332 |    *line* is a line of source code to be included in the warning | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 333 |    message; if *line* is not supplied, :func:`showwarning` will | 
| Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 334 |    try to read the line specified by *filename* and *lineno*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 |  | 
 | 336 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 337 | .. function:: formatwarning(message, category, filename, lineno, line=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 |  | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 339 |    Format a warning the standard way.  This returns a string which may contain | 
 | 340 |    embedded newlines and ends in a newline.  *line* is a line of source code to | 
 | 341 |    be included in the warning message; if *line* is not supplied, | 
 | 342 |    :func:`formatwarning` will try to read the line specified by *filename* and | 
 | 343 |    *lineno*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 |  | 
 | 345 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 346 | .. function:: filterwarnings(action, message='', category=Warning, module='', lineno=0, append=False) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 347 |  | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 348 |    Insert an entry into the list of :ref:`warnings filter specifications | 
 | 349 |    <warning-filter>`.  The entry is inserted at the front by default; if | 
 | 350 |    *append* is true, it is inserted at the end.  This checks the types of the | 
 | 351 |    arguments, compiles the *message* and *module* regular expressions, and | 
 | 352 |    inserts them as a tuple in the list of warnings filters.  Entries closer to | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 |    the front of the list override entries later in the list, if both match a | 
 | 354 |    particular warning.  Omitted arguments default to a value that matches | 
 | 355 |    everything. | 
 | 356 |  | 
 | 357 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 358 | .. function:: simplefilter(action, category=Warning, lineno=0, append=False) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 |  | 
| Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 360 |    Insert a simple entry into the list of :ref:`warnings filter specifications | 
 | 361 |    <warning-filter>`.  The meaning of the function parameters is as for | 
 | 362 |    :func:`filterwarnings`, but regular expressions are not needed as the filter | 
 | 363 |    inserted always matches any message in any module as long as the category and | 
 | 364 |    line number match. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 |  | 
 | 366 |  | 
 | 367 | .. function:: resetwarnings() | 
 | 368 |  | 
 | 369 |    Reset the warnings filter.  This discards the effect of all previous calls to | 
 | 370 |    :func:`filterwarnings`, including that of the :option:`-W` command line options | 
 | 371 |    and calls to :func:`simplefilter`. | 
 | 372 |  | 
| Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 373 |  | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 374 | Available Context Managers | 
 | 375 | -------------------------- | 
| Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 376 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 377 | .. class:: catch_warnings(\*, record=False, module=None) | 
| Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 378 |  | 
| Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 379 |     A context manager that copies and, upon exit, restores the warnings filter | 
 | 380 |     and the :func:`showwarning` function. | 
 | 381 |     If the *record* argument is :const:`False` (the default) the context manager | 
 | 382 |     returns :class:`None` on entry. If *record* is :const:`True`, a list is | 
 | 383 |     returned that is progressively populated with objects as seen by a custom | 
 | 384 |     :func:`showwarning` function (which also suppresses output to ``sys.stdout``). | 
 | 385 |     Each object in the list has attributes with the same names as the arguments to | 
 | 386 |     :func:`showwarning`. | 
| Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 387 |  | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 388 |     The *module* argument takes a module that will be used instead of the | 
 | 389 |     module returned when you import :mod:`warnings` whose filter will be | 
| Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 390 |     protected. This argument exists primarily for testing the :mod:`warnings` | 
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 391 |     module itself. | 
| Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 392 |  | 
 | 393 |     .. note:: | 
 | 394 |  | 
 | 395 |         The :class:`catch_warnings` manager works by replacing and | 
 | 396 |         then later restoring the module's | 
 | 397 |         :func:`showwarning` function and internal list of filter | 
 | 398 |         specifications.  This means the context manager is modifying | 
 | 399 |         global state and therefore is not thread-safe. |