Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 1 | \section{\module{warnings} --- |
| 2 | Warning control} |
| 3 | |
| 4 | \declaremodule{standard}{warnings} |
| 5 | \modulesynopsis{Issue warning messages and control their disposition.} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 6 | \index{warnings} |
| 7 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 8 | \versionadded{2.1} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 9 | |
| 10 | Warning messages are typically issued in situations where it is useful |
| 11 | to alert the user of some condition in a program, where that condition |
| 12 | (normally) doesn't warrant raising an exception and terminating the |
| 13 | program. For example, one might want to issue a warning when a |
| 14 | program uses an obsolete module. |
| 15 | |
| 16 | Python programmers issue warnings by calling the \function{warn()} |
| 17 | function defined in this module. (C programmers use |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 18 | \cfunction{PyErr_Warn()}; see the |
| 19 | \citetitle[../api/exceptionHandling.html]{Python/C API Reference |
| 20 | Manual} for details). |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 21 | |
| 22 | Warning messages are normally written to \code{sys.stderr}, but their |
| 23 | disposition can be changed flexibly, from ignoring all warnings to |
| 24 | turning them into exceptions. The disposition of warnings can vary |
| 25 | based on the warning category (see below), the text of the warning |
| 26 | message, and the source location where it is issued. Repetitions of a |
| 27 | particular warning for the same source location are typically |
| 28 | suppressed. |
| 29 | |
| 30 | There are two stages in warning control: first, each time a warning is |
| 31 | issued, a determination is made whether a message should be issued or |
| 32 | not; next, if a message is to be issued, it is formatted and printed |
| 33 | using a user-settable hook. |
| 34 | |
| 35 | The determination whether to issue a warning message is controlled by |
| 36 | the warning filter, which is a sequence of matching rules and actions. |
| 37 | Rules can be added to the filter by calling |
| 38 | \function{filterwarnings()} and reset to its default state by calling |
| 39 | \function{resetwarnings()}. |
| 40 | |
| 41 | The printing of warning messages is done by calling |
Raymond Hettinger | 6880431 | 2005-01-01 00:28:46 +0000 | [diff] [blame] | 42 | \function{showwarning()}, which may be overridden; the default |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 43 | implementation of this function formats the message by calling |
| 44 | \function{formatwarning()}, which is also available for use by custom |
| 45 | implementations. |
| 46 | |
| 47 | |
Fred Drake | 1dea760 | 2000-12-25 06:19:08 +0000 | [diff] [blame] | 48 | \subsection{Warning Categories \label{warning-categories}} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 49 | |
| 50 | There are a number of built-in exceptions that represent warning |
| 51 | categories. This categorization is useful to be able to filter out |
| 52 | groups of warnings. The following warnings category classes are |
| 53 | currently defined: |
| 54 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 55 | \begin{tableii}{l|l}{exception}{Class}{Description} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 56 | |
| 57 | \lineii{Warning}{This is the base class of all warning category |
Fred Drake | ce575ba | 2001-08-14 21:51:50 +0000 | [diff] [blame] | 58 | classes. It is a subclass of \exception{Exception}.} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 59 | |
| 60 | \lineii{UserWarning}{The default category for \function{warn()}.} |
| 61 | |
| 62 | \lineii{DeprecationWarning}{Base category for warnings about |
| 63 | deprecated features.} |
| 64 | |
| 65 | \lineii{SyntaxWarning}{Base category for warnings about dubious |
| 66 | syntactic features.} |
| 67 | |
| 68 | \lineii{RuntimeWarning}{Base category for warnings about dubious |
| 69 | runtime features.} |
| 70 | |
Barry Warsaw | b8c20a7 | 2002-08-14 16:40:54 +0000 | [diff] [blame] | 71 | \lineii{FutureWarning}{Base category for warnings about constructs |
| 72 | that will change semantically in the future.} |
| 73 | |
Nick Coghlan | 2bfe3a9 | 2006-07-06 13:41:34 +0000 | [diff] [blame] | 74 | \lineii{PendingDeprecationWarning}{Base category for warnings about |
| 75 | features that will be deprecated in the future (ignored by default).} |
| 76 | |
| 77 | \lineii{ImportWarning}{Base category for warnings triggered during the |
| 78 | process of importing a module (ignored by default).} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 79 | \end{tableii} |
| 80 | |
| 81 | While these are technically built-in exceptions, they are documented |
| 82 | here, because conceptually they belong to the warnings mechanism. |
| 83 | |
| 84 | User code can define additional warning categories by subclassing one |
| 85 | of the standard warning categories. A warning category must always be |
| 86 | a subclass of the \exception{Warning} class. |
| 87 | |
| 88 | |
Fred Drake | 1dea760 | 2000-12-25 06:19:08 +0000 | [diff] [blame] | 89 | \subsection{The Warnings Filter \label{warning-filter}} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 90 | |
| 91 | The warnings filter controls whether warnings are ignored, displayed, |
| 92 | or turned into errors (raising an exception). |
| 93 | |
| 94 | Conceptually, the warnings filter maintains an ordered list of filter |
| 95 | specifications; any specific warning is matched against each filter |
| 96 | specification in the list in turn until a match is found; the match |
| 97 | determines the disposition of the match. Each entry is a tuple of the |
| 98 | form (\var{action}, \var{message}, \var{category}, \var{module}, |
| 99 | \var{lineno}), where: |
| 100 | |
| 101 | \begin{itemize} |
| 102 | |
| 103 | \item \var{action} is one of the following strings: |
| 104 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 105 | \begin{tableii}{l|l}{code}{Value}{Disposition} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 106 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 107 | \lineii{"error"}{turn matching warnings into exceptions} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 108 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 109 | \lineii{"ignore"}{never print matching warnings} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 110 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 111 | \lineii{"always"}{always print matching warnings} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 112 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 113 | \lineii{"default"}{print the first occurrence of matching |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 114 | warnings for each location where the warning is issued} |
| 115 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 116 | \lineii{"module"}{print the first occurrence of matching |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 117 | warnings for each module where the warning is issued} |
| 118 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 119 | \lineii{"once"}{print only the first occurrence of matching |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 120 | warnings, regardless of location} |
| 121 | |
| 122 | \end{tableii} |
| 123 | |
Andrew M. Kuchling | ba37524 | 2003-02-06 14:38:45 +0000 | [diff] [blame] | 124 | \item \var{message} is a string containing a regular expression that |
| 125 | the warning message must match (the match is compiled to always be |
| 126 | case-insensitive) |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 127 | |
| 128 | \item \var{category} is a class (a subclass of \exception{Warning}) of |
| 129 | which the warning category must be a subclass in order to match |
| 130 | |
Andrew M. Kuchling | ba37524 | 2003-02-06 14:38:45 +0000 | [diff] [blame] | 131 | \item \var{module} is a string containing a regular expression that the module |
| 132 | name must match (the match is compiled to be case-sensitive) |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 133 | |
| 134 | \item \var{lineno} is an integer that the line number where the |
| 135 | warning occurred must match, or \code{0} to match all line |
| 136 | numbers |
| 137 | |
| 138 | \end{itemize} |
| 139 | |
| 140 | Since the \exception{Warning} class is derived from the built-in |
| 141 | \exception{Exception} class, to turn a warning into an error we simply |
| 142 | raise \code{category(message)}. |
| 143 | |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 144 | The warnings filter is initialized by \programopt{-W} options passed |
| 145 | to the Python interpreter command line. The interpreter saves the |
| 146 | arguments for all \programopt{-W} options without interpretation in |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 147 | \code{sys.warnoptions}; the \module{warnings} module parses these when |
| 148 | it is first imported (invalid options are ignored, after printing a |
| 149 | message to \code{sys.stderr}). |
| 150 | |
Nick Coghlan | 2bfe3a9 | 2006-07-06 13:41:34 +0000 | [diff] [blame] | 151 | The warnings that are ignored by default may be enabled by passing |
| 152 | \programopt{-Wd} to the interpreter. This enables default handling |
| 153 | for all warnings, including those that are normally ignored by |
| 154 | default. This is particular useful for enabling ImportWarning when |
| 155 | debugging problems importing a developed package. ImportWarning can |
| 156 | also be enabled explicitly in Python code using: |
| 157 | |
| 158 | \begin{verbatim} |
| 159 | warnings.simplefilter('default', ImportWarning) |
| 160 | \end{verbatim} |
| 161 | |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 162 | |
Fred Drake | 1dea760 | 2000-12-25 06:19:08 +0000 | [diff] [blame] | 163 | \subsection{Available Functions \label{warning-functions}} |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 164 | |
| 165 | \begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}} |
| 166 | Issue a warning, or maybe ignore it or raise an exception. The |
| 167 | \var{category} argument, if given, must be a warning category class |
Walter Dörwald | b25c2b0 | 2002-03-21 10:38:40 +0000 | [diff] [blame] | 168 | (see above); it defaults to \exception{UserWarning}. Alternatively |
| 169 | \var{message} can be a \exception{Warning} instance, in which case |
Neal Norwitz | e22d3df | 2002-03-21 12:58:54 +0000 | [diff] [blame] | 170 | \var{category} will be ignored and \code{message.__class__} will be used. |
Walter Dörwald | b25c2b0 | 2002-03-21 10:38:40 +0000 | [diff] [blame] | 171 | In this case the message text will be \code{str(message)}. This function |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 172 | raises an exception if the particular warning issued is changed |
| 173 | into an error by the warnings filter see above. The \var{stacklevel} |
| 174 | argument can be used by wrapper functions written in Python, like |
| 175 | this: |
| 176 | |
| 177 | \begin{verbatim} |
| 178 | def deprecation(message): |
Fred Drake | f981617 | 2002-03-12 19:49:31 +0000 | [diff] [blame] | 179 | warnings.warn(message, DeprecationWarning, stacklevel=2) |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 180 | \end{verbatim} |
| 181 | |
| 182 | This makes the warning refer to \function{deprecation()}'s caller, |
| 183 | rather than to the source of \function{deprecation()} itself (since |
| 184 | the latter would defeat the purpose of the warning message). |
| 185 | \end{funcdesc} |
| 186 | |
Guido van Rossum | 56ba72a | 2001-02-28 23:34:21 +0000 | [diff] [blame] | 187 | \begin{funcdesc}{warn_explicit}{message, category, filename, |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 188 | lineno\optional{, module\optional{, registry\optional{, |
| 189 | module_globals}}}} |
Guido van Rossum | 56ba72a | 2001-02-28 23:34:21 +0000 | [diff] [blame] | 190 | This is a low-level interface to the functionality of |
| 191 | \function{warn()}, passing in explicitly the message, category, |
| 192 | filename and line number, and optionally the module name and the |
| 193 | registry (which should be the \code{__warningregistry__} dictionary of |
| 194 | the module). The module name defaults to the filename with \code{.py} |
| 195 | stripped; if no registry is passed, the warning is never suppressed. |
Walter Dörwald | b25c2b0 | 2002-03-21 10:38:40 +0000 | [diff] [blame] | 196 | \var{message} must be a string and \var{category} a subclass of |
| 197 | \exception{Warning} or \var{message} may be a \exception{Warning} instance, |
| 198 | in which case \var{category} will be ignored. |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 199 | |
| 200 | \var{module_globals}, if supplied, should be the global namespace in use |
| 201 | by the code for which the warning is issued. (This argument is used to |
| 202 | support displaying source for modules found in zipfiles or other |
| 203 | non-filesystem import sources, and was added in Python 2.5.) |
Guido van Rossum | 56ba72a | 2001-02-28 23:34:21 +0000 | [diff] [blame] | 204 | \end{funcdesc} |
| 205 | |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 206 | \begin{funcdesc}{showwarning}{message, category, filename, |
| 207 | lineno\optional{, file}} |
| 208 | Write a warning to a file. The default implementation calls |
Tim Peters | 93ceaea | 2003-07-01 14:37:59 +0000 | [diff] [blame] | 209 | \code{formatwarning(\var{message}, \var{category}, \var{filename}, |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 210 | \var{lineno})} and writes the resulting string to \var{file}, which |
| 211 | defaults to \code{sys.stderr}. You may replace this function with an |
| 212 | alternative implementation by assigning to |
| 213 | \code{warnings.showwarning}. |
| 214 | \end{funcdesc} |
| 215 | |
| 216 | \begin{funcdesc}{formatwarning}{message, category, filename, lineno} |
| 217 | Format a warning the standard way. This returns a string which may |
| 218 | contain embedded newlines and ends in a newline. |
| 219 | \end{funcdesc} |
| 220 | |
| 221 | \begin{funcdesc}{filterwarnings}{action\optional{, |
Fred Drake | 1dea760 | 2000-12-25 06:19:08 +0000 | [diff] [blame] | 222 | message\optional{, category\optional{, |
Guido van Rossum | 77b20f0 | 2001-01-14 14:10:18 +0000 | [diff] [blame] | 223 | module\optional{, lineno\optional{, append}}}}}} |
| 224 | Insert an entry into the list of warnings filters. The entry is |
| 225 | inserted at the front by default; if \var{append} is true, it is |
| 226 | inserted at the end. |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 227 | This checks the types of the arguments, compiles the message and |
Nick Coghlan | 2bfe3a9 | 2006-07-06 13:41:34 +0000 | [diff] [blame] | 228 | module regular expressions, and inserts them as a tuple in the |
| 229 | list of warnings filters. Entries closer to the front of the list |
| 230 | override entries later in the list, if both match a particular |
| 231 | warning. Omitted arguments default to a value that matches |
| 232 | everything. |
| 233 | \end{funcdesc} |
| 234 | |
| 235 | \begin{funcdesc}{simplefilter}{action\optional{, |
| 236 | category\optional{, |
| 237 | lineno\optional{, append}}}} |
| 238 | Insert a simple entry into the list of warnings filters. The meaning |
| 239 | of the function parameters is as for \function{filterwarnings()}, but |
| 240 | regular expressions are not needed as the filter inserted always |
| 241 | matches any message in any module as long as the category and line |
| 242 | number match. |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 243 | \end{funcdesc} |
| 244 | |
| 245 | \begin{funcdesc}{resetwarnings}{} |
| 246 | Reset the warnings filter. This discards the effect of all previous |
Fred Drake | 288927f | 2001-01-04 05:59:37 +0000 | [diff] [blame] | 247 | calls to \function{filterwarnings()}, including that of the |
Nick Coghlan | 2bfe3a9 | 2006-07-06 13:41:34 +0000 | [diff] [blame] | 248 | \programopt{-W} command line options and calls to |
| 249 | \function{simplefilter()}. |
Guido van Rossum | 5db5ba1 | 2000-12-19 06:01:23 +0000 | [diff] [blame] | 250 | \end{funcdesc} |