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