PEP 230 – Warning Framework | peps.python.org
PEP 230 – Warning Framework PEP 230 – Warning Framework Author : Guido van Rossum <guido at python.org> Status : Final Type : Standards Track Created : 28-Nov-2000 Python-Version : 2.1 Post-History : 05-Nov-2000 Table of Contents Abstract Motivation APIs For Issuing Warnings Warnings Categories The Warnings Filter Warnings Output And Formatting Hooks API For Manipulating Warning Filters Command Line Syntax Open Issues Rejected Concerns Implementation Abstract This PEP proposes a C and Python level API, as well as command line flags, to issue warning messages and control what happens to them.
This is mostly based on GvR’s proposal posted to python-dev on 05-Nov-2000, with some ideas (such as using classes to categorize warnings) merged in from Paul Prescod’s counter-proposal posted on the same date. Also, an attempt to implement the proposal caused several small tweaks. Motivation With Python 3000 looming, it is necessary to start issuing warnings about the use of obsolete or deprecated features, in addition to errors.
There are also lots of other reasons to be able to issue warnings, both from C and from Python code, both at compile time and at run time. Warnings aren’t fatal, and thus it’s possible that a program triggers the same warning many times during a single execution. It would be annoying if a program emitted an endless stream of identical warnings. Therefore, a mechanism is needed that suppresses multiple identical warnings.
It is also desirable to have user control over which warnings are printed. While in general it is useful to see all warnings all the time, there may be times where it is impractical to fix the code right away in a production program. In this case, there should be a way to suppress warnings. It is also useful to be able to suppress specific warnings during program development, e.g. when a warning is generated by a piece of 3rd party code that cannot be fixed right away, or when there is no way to fix the code (possibly a warning message is generated for a perfectly fine piece of code).
It would be unwise to offer to suppress all warnings in such cases: the developer would miss warnings about the rest of the code. On the other hand, there are also situations conceivable where some or all warnings are better treated as errors. For example, it may be a local coding standard that a particular deprecated feature should not be used. In order to enforce this, it is useful to be able to turn the warning about this particular feature into an error, raising an exception (without necessarily turning all warnings into errors).
Therefore, I propose to introduce a flexible “warning filter” which can filter out warnings or change them into exceptions, based on: Where in the code they are generated (per package, module, or function) The warning category (warning categories are discussed below) A specific warning message The warning filter must be controllable both from the command line and from Python code. APIs For Issuing Warnings To issue a warning from Python: import warnings warnings . warn ( message [, category [, stacklevel ]]) The category argument, if given, must be a warning category class (see below); it defaults to warnings.UserWarning.
This may raise an exception if the particular warning issued is changed into an error by the warnings filter. The stacklevel can be used by wrapper functions written in Python, like this: def deprecation ( message ): warn ( message , DeprecationWarning , level = 2 ) This makes the warning refer to the deprecation()’s caller, rather than to the source of deprecation() itself (since the latter would defeat the purpose of the warning message).
To issue a warning from C: int PyErr_Warn ( PyObject * category , char * message ); Return 0 normally, 1 if an exception is raised (either because the warning was transformed into an exception, or because of a malfunction in the implementation, such as running out of memory). The category argument must be a warning category class (see below) or NULL , in which case it defaults to PyExc_RuntimeWarning . When PyErr_Warn() function returns 1, the caller should do normal exception handling.
The current C implementation of PyErr_Warn() imports the warnings module (implemented in Python) and calls its warn() function. This minimizes the amount of C code that needs to be added to implement the warning feature. [XXX Open Issue: what about issuing warnings during lexing or parsing, which don’t have the exception machinery available?] Warnings Categories 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: Warning – this is the base class of all warning category classes and it itself a subclass of Exception UserWarning – the default category for warnings.warn() DeprecationWarning – base category for warnings about deprecated features SyntaxWarning – base category for warnings about dubious syntactic features RuntimeWarning – base category for warnings about dubious runtime features [XXX: Other warning categories may be proposed during the review period for this PEP.] These standard warning categories are available from C as PyExc_Warning , PyExc_UserWarning , etc.
From Python, they are available in the __builtin__ module, so no import is necessary. 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 Warning class. The Warnings Filter The warnings filter control whether warnings are ignored, displayed, or turned into errors (raising an exception). There are three sides to the warnings filter: The data structures used to efficiently determine the disposition of a particular warnings.warn() or PyErr_Warn() call.
The API to control the filter from Python source code. The command line switches to control the filter. The warnings filter works in several stages. It is optimized for the (expected to be common) case where the same warning is issued from the same place in the code over and over. First, the warning filter collects the module and line number where the warning is issued; this information is readily available through sys._getframe() .
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 as follows: ( category , message , module , lineno , action ) category is a class (a subclass of warnings.Warning ) of which the warning category must be a subclass in order to match message is a compiled regular expression that the warning message must match (the match is case-insensitive) module is a compiled regular expression that the module name must match lineno is an integer that the line number where the warning occurred must match, or 0 to match all line numbers action is one of the following strings: “error” – turn matching warnings into exceptions “ignore” – never print matching warnings “always” – always print matching warnings “default” – print the first occurrence of matching warnings for each location where the warning is issued “module” – print the first occurrence of matching warnings for each module where the warning is issued “once” – print only the first occurrence of matching warnings Since the Warning class is derived from the built-in Exception class, to turn a warning into an error we simply raise category(message) .
Warnings Output And Formatting Hooks When the warnings filter decides to issue a warning (but not when it decides to raise an exception), it passes the information about the function warnings.showwarning(message, category, filename, lineno) . The default implementation of this function writes the warnin
The rest of this page is on the live site.