System learning Python - warning information control module warnings: warning filter - [execution process of warning filter]

Category Catalog: General Catalog of "Systematic Learning Python"


-WWarning filters are initialized by command line options and PYTHONWARNINGSenvironment variables passed to the Python interpreter . The interpreter sys.warningoptionssaves all given arguments in , but does not interpret them; warningsthe module parses these arguments when it is first imported (invalid options are ignored and sys.stderra message is printed first).

Each warning filter is formatted as a colon-separated sequence of fields:

action:message:category:module:line

The meaning of these fields is explained in the article " System Learning Python - Warning Information Control Module warnings: Warning Filter - [Basic Knowledge] ". When multiple filters are listed on a line (such as PYTHONWARNINGS), the filters are separated by commas, and the later ones take precedence (because they are applied from left to right, the most recently applied filter takes precedence).

Common warning filters apply to all warnings, warnings of specific categories, and warnings raised by specific modules and packages. Here are some examples:

default                      # Show all warnings (even those ignored by default)
ignore                       # Ignore all warnings
error                        # Convert all warnings to errors
error::ResourceWarning       # Treat ResourceWarning messages as errors
default::DeprecationWarning  # Show DeprecationWarning messages
ignore,default:::mymodule    # Only report warnings triggered by "mymodule"
error:::mymodule[.*]         # Convert warnings to errors in "mymodule"
                             # and any subpackages of "mymodule"

Guess you like

Origin blog.csdn.net/hy592070616/article/details/135465603