warnings — 警告控制

源代码: Lib/warnings.py


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 warn() function defined in this module. (C programmers use PyErr_WarnEx() ;见 异常处理 了解细节)。

Warning messages are normally written to 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 警告类别 , 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 警告过滤 , which is a sequence of matching rules and actions. Rules can be added to the filter by calling filterwarnings() and reset to its default state by calling resetwarnings() .

The printing of warning messages is done by calling showwarning() , which may be overridden; the default implementation of this function formats the message by calling formatwarning() , which is also available for use by custom implementations.

另请参阅

logging.captureWarnings() allows you to handle all warnings with the standard logging infrastructure.

警告类别

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.

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 Warning 类。

The following warnings category classes are currently defined:

描述

Warning

This is the base class of all warning category classes. It is a subclass of Exception .

UserWarning

The default category for warn() .

DeprecationWarning

Base category for warnings about deprecated features when those warnings are intended for other Python developers (ignored by default, unless triggered by code in __main__ ).

SyntaxWarning

Base category for warnings about dubious syntactic features.

RuntimeWarning

Base category for warnings about dubious runtime features.

FutureWarning

Base category for warnings about deprecated features when those warnings are intended for end users of applications that are written in Python.

PendingDeprecationWarning

Base category for warnings about features that will be deprecated in the future (ignored by default).

ImportWarning

Base category for warnings triggered during the process of importing a module (ignored by default).

UnicodeWarning

Base category for warnings related to Unicode.

BytesWarning

Base category for warnings related to bytes and bytearray .

ResourceWarning

Base category for warnings related to resource usage (ignored by default).

3.7 版改变: Previously DeprecationWarning and FutureWarning were distinguished based on whether a feature was being removed entirely or changing its behaviour. They are now distinguished based on their intended audience and the way they’re handled by the default warnings filters.