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