| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | \section{\module{warnings} --- | 
					
						
							|  |  |  |          Warning control} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \declaremodule{standard}{warnings} | 
					
						
							|  |  |  | \modulesynopsis{Issue warning messages and control their disposition.} | 
					
						
							|  |  |  | \index{warnings} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  | \versionadded{2.1} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Warning messages are typically issued in situations where it is useful | 
					
						
							|  |  |  | to alert the user of some condition in a program, where that condition | 
					
						
							|  |  |  | (normally) doesn't warrant raising an exception and terminating the | 
					
						
							|  |  |  | program.  For example, one might want to issue a warning when a | 
					
						
							|  |  |  | program uses an obsolete module. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Python programmers issue warnings by calling the \function{warn()} | 
					
						
							|  |  |  | function defined in this module.  (C programmers use | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  | \cfunction{PyErr_Warn()}; see the | 
					
						
							|  |  |  | \citetitle[../api/exceptionHandling.html]{Python/C API Reference | 
					
						
							|  |  |  | Manual} for details). | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Warning messages are normally written to \code{sys.stderr}, but their | 
					
						
							|  |  |  | disposition can be changed flexibly, from ignoring all warnings to | 
					
						
							|  |  |  | turning them into exceptions.  The disposition of warnings can vary | 
					
						
							|  |  |  | based on the warning category (see below), the text of the warning | 
					
						
							|  |  |  | message, and the source location where it is issued.  Repetitions of a | 
					
						
							|  |  |  | particular warning for the same source location are typically | 
					
						
							|  |  |  | suppressed. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There are two stages in warning control: first, each time a warning is | 
					
						
							|  |  |  | issued, a determination is made whether a message should be issued or | 
					
						
							|  |  |  | not; next, if a message is to be issued, it is formatted and printed | 
					
						
							|  |  |  | using a user-settable hook. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The determination whether to issue a warning message is controlled by | 
					
						
							|  |  |  | the warning filter, which is a sequence of matching rules and actions. | 
					
						
							|  |  |  | Rules can be added to the filter by calling | 
					
						
							|  |  |  | \function{filterwarnings()} and reset to its default state by calling | 
					
						
							|  |  |  | \function{resetwarnings()}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The printing of warning messages is done by calling | 
					
						
							|  |  |  | \function{showwarning()}, which may be overidden; the default | 
					
						
							|  |  |  | implementation of this function formats the message by calling | 
					
						
							|  |  |  | \function{formatwarning()}, which is also available for use by custom | 
					
						
							|  |  |  | implementations. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-25 06:19:08 +00:00
										 |  |  | \subsection{Warning Categories \label{warning-categories}} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | There are a number of built-in exceptions that represent warning | 
					
						
							|  |  |  | categories.  This categorization is useful to be able to filter out | 
					
						
							|  |  |  | groups of warnings.  The following warnings category classes are | 
					
						
							|  |  |  | currently defined: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  | \begin{tableii}{l|l}{exception}{Class}{Description} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \lineii{Warning}{This is the base class of all warning category | 
					
						
							| 
									
										
										
										
											2001-08-14 21:51:50 +00:00
										 |  |  | classes.  It is a subclass of \exception{Exception}.} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \lineii{UserWarning}{The default category for \function{warn()}.} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \lineii{DeprecationWarning}{Base category for warnings about | 
					
						
							|  |  |  | deprecated features.} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \lineii{SyntaxWarning}{Base category for warnings about dubious | 
					
						
							|  |  |  | syntactic features.} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \lineii{RuntimeWarning}{Base category for warnings about dubious | 
					
						
							|  |  |  | runtime features.} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-14 16:40:54 +00:00
										 |  |  | \lineii{FutureWarning}{Base category for warnings about constructs | 
					
						
							|  |  |  | that will change semantically in the future.} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | \end{tableii} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | While these are technically built-in exceptions, they are documented | 
					
						
							|  |  |  | here, because conceptually they belong to the warnings mechanism. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | User code can define additional warning categories by subclassing one | 
					
						
							|  |  |  | of the standard warning categories.  A warning category must always be | 
					
						
							|  |  |  | a subclass of the \exception{Warning} class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-25 06:19:08 +00:00
										 |  |  | \subsection{The Warnings Filter \label{warning-filter}} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The warnings filter controls whether warnings are ignored, displayed, | 
					
						
							|  |  |  | or turned into errors (raising an exception). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Conceptually, the warnings filter maintains an ordered list of filter | 
					
						
							|  |  |  | specifications; any specific warning is matched against each filter | 
					
						
							|  |  |  | specification in the list in turn until a match is found; the match | 
					
						
							|  |  |  | determines the disposition of the match.  Each entry is a tuple of the | 
					
						
							|  |  |  | form (\var{action}, \var{message}, \var{category}, \var{module}, | 
					
						
							|  |  |  | \var{lineno}), where: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{itemize} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \var{action} is one of the following strings: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \begin{tableii}{l|l}{code}{Value}{Disposition} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \lineii{"error"}{turn matching warnings into exceptions} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \lineii{"ignore"}{never print matching warnings} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \lineii{"always"}{always print matching warnings} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \lineii{"default"}{print the first occurrence of matching | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  |     warnings for each location where the warning is issued} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \lineii{"module"}{print the first occurrence of matching | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  |     warnings for each module where the warning is issued} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  |     \lineii{"once"}{print only the first occurrence of matching | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  |     warnings, regardless of location} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \end{tableii} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \var{message} is a compiled regular expression that the warning | 
					
						
							|  |  |  | message must match (the match is case-insensitive) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \var{category} is a class (a subclass of \exception{Warning}) of | 
					
						
							|  |  |  |       which the warning category must be a subclass in order to match | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \var{module} is a compiled regular expression that the module | 
					
						
							|  |  |  |       name must match | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \var{lineno} is an integer that the line number where the | 
					
						
							|  |  |  |       warning occurred must match, or \code{0} to match all line | 
					
						
							|  |  |  |       numbers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \end{itemize} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Since the \exception{Warning} class is derived from the built-in | 
					
						
							|  |  |  | \exception{Exception} class, to turn a warning into an error we simply | 
					
						
							|  |  |  | raise \code{category(message)}. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  | The warnings filter is initialized by \programopt{-W} options passed | 
					
						
							|  |  |  | to the Python interpreter command line.  The interpreter saves the | 
					
						
							|  |  |  | arguments for all \programopt{-W} options without interpretation in | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | \code{sys.warnoptions}; the \module{warnings} module parses these when | 
					
						
							|  |  |  | it is first imported (invalid options are ignored, after printing a | 
					
						
							|  |  |  | message to \code{sys.stderr}). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-25 06:19:08 +00:00
										 |  |  | \subsection{Available Functions \label{warning-functions}} | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{warn}{message\optional{, category\optional{, stacklevel}}} | 
					
						
							|  |  |  | Issue a warning, or maybe ignore it or raise an exception.  The | 
					
						
							|  |  |  | \var{category} argument, if given, must be a warning category class | 
					
						
							| 
									
										
										
										
											2002-03-21 10:38:40 +00:00
										 |  |  | (see above); it defaults to \exception{UserWarning}.  Alternatively | 
					
						
							|  |  |  | \var{message} can be a \exception{Warning} instance, in which case | 
					
						
							| 
									
										
										
										
											2002-03-21 12:58:54 +00:00
										 |  |  | \var{category} will be ignored and \code{message.__class__} will be used. | 
					
						
							| 
									
										
										
										
											2002-03-21 10:38:40 +00:00
										 |  |  | In this case the message text will be \code{str(message)}. This function | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | raises an exception if the particular warning issued is changed | 
					
						
							|  |  |  | into an error by the warnings filter see above.  The \var{stacklevel} | 
					
						
							|  |  |  | argument can be used by wrapper functions written in Python, like | 
					
						
							|  |  |  | this: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | def deprecation(message): | 
					
						
							| 
									
										
										
										
											2002-03-12 19:49:31 +00:00
										 |  |  |     warnings.warn(message, DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This makes the warning refer to \function{deprecation()}'s caller, | 
					
						
							|  |  |  | rather than to the source of \function{deprecation()} itself (since | 
					
						
							|  |  |  | the latter would defeat the purpose of the warning message). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-28 23:34:21 +00:00
										 |  |  | \begin{funcdesc}{warn_explicit}{message, category, filename, | 
					
						
							|  |  |  |  lineno\optional{, module\optional{, registry}}} | 
					
						
							|  |  |  | This is a low-level interface to the functionality of | 
					
						
							|  |  |  | \function{warn()}, passing in explicitly the message, category, | 
					
						
							|  |  |  | filename and line number, and optionally the module name and the | 
					
						
							|  |  |  | registry (which should be the \code{__warningregistry__} dictionary of | 
					
						
							|  |  |  | the module).  The module name defaults to the filename with \code{.py} | 
					
						
							|  |  |  | stripped; if no registry is passed, the warning is never suppressed. | 
					
						
							| 
									
										
										
										
											2002-03-21 10:38:40 +00:00
										 |  |  | \var{message} must be a string and \var{category} a subclass of | 
					
						
							|  |  |  | \exception{Warning} or \var{message} may be a \exception{Warning} instance, | 
					
						
							|  |  |  | in which case \var{category} will be ignored. | 
					
						
							| 
									
										
										
										
											2001-02-28 23:34:21 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | \begin{funcdesc}{showwarning}{message, category, filename, | 
					
						
							|  |  |  | 			     lineno\optional{, file}} | 
					
						
							|  |  |  | Write a warning to a file.  The default implementation calls | 
					
						
							|  |  |  | \code{showwarning(\var{message}, \var{category}, \var{filename}, | 
					
						
							|  |  |  | \var{lineno})} and writes the resulting string to \var{file}, which | 
					
						
							|  |  |  | defaults to \code{sys.stderr}.  You may replace this function with an | 
					
						
							|  |  |  | alternative implementation by assigning to | 
					
						
							|  |  |  | \code{warnings.showwarning}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{formatwarning}{message, category, filename, lineno} | 
					
						
							|  |  |  | Format a warning the standard way.  This returns a string  which may | 
					
						
							|  |  |  | contain embedded newlines and ends in a newline. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{filterwarnings}{action\optional{, | 
					
						
							| 
									
										
										
										
											2000-12-25 06:19:08 +00:00
										 |  |  |                  message\optional{, category\optional{, | 
					
						
							| 
									
										
										
										
											2001-01-14 14:10:18 +00:00
										 |  |  |                  module\optional{, lineno\optional{, append}}}}}} | 
					
						
							|  |  |  | Insert an entry into the list of warnings filters.  The entry is | 
					
						
							|  |  |  | inserted at the front by default; if \var{append} is true, it is | 
					
						
							|  |  |  | inserted at the end. | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | This checks the types of the arguments, compiles the message and | 
					
						
							|  |  |  | module regular expressions, and inserts them as a tuple in front | 
					
						
							|  |  |  | of the warnings filter.  Entries inserted later override entries | 
					
						
							|  |  |  | inserted earlier, if both match a particular warning.  Omitted | 
					
						
							|  |  |  | arguments default to a value that matches everything. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{resetwarnings}{} | 
					
						
							|  |  |  | Reset the warnings filter.  This discards the effect of all previous | 
					
						
							| 
									
										
										
										
											2001-01-04 05:59:37 +00:00
										 |  |  | calls to \function{filterwarnings()}, including that of the | 
					
						
							|  |  |  | \programopt{-W} command line options. | 
					
						
							| 
									
										
										
										
											2000-12-19 06:01:23 +00:00
										 |  |  | \end{funcdesc} |