在Python中,如何像捕获异常一样捕获警告?



我在python代码中使用的第三方库(用C编写)正在发出警告。我希望能够使用try except语法来正确处理这些警告。有办法做到这一点吗?

要将警告作为错误处理,只需使用以下命令:

import warnings
warnings.filterwarnings("error")

在此之后,您将能够捕获与错误相同的警告,例如:

try:
    some_heavy_calculations()
except RuntimeWarning:
    breakpoint()

你也可以通过运行命令重置警告行为:

warnings.resetwarnings()

注:添加这个答案,因为评论中最好的答案有拼写错误:filterwarnigns而不是filterwarnings

引用python手册(27.6.4。测试警告):

import warnings
def fxn():
    warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
    # Cause all warnings to always be triggered.
    warnings.simplefilter("always")
    # Trigger a warning.
    fxn()
    # Verify some things
    assert len(w) == 1
    assert issubclass(w[-1].category, DeprecationWarning)
    assert "deprecated" in str(w[-1].message)

如果您只是希望脚本在警告时失败,您可以使用-W参数调用python:

python -W error foobar.py

这里有一个变化,使如何仅使用自定义警告更清楚。

import warnings
with warnings.catch_warnings(record=True) as w:
    # Cause all warnings to always be triggered.
    warnings.simplefilter("always")
    # Call some code that triggers a custom warning.
    functionThatRaisesWarning()
    # ignore any non-custom warnings that may be in the list
    w = filter(lambda i: issubclass(i.category, UserWarning), w)
    if len(w):
        # do something with the first warning
        email_admins(w[0].message)

扩展nieka的答案,但使用catch_warnings上下文管理器,在上下文退出后将警告行为重置为默认值:

import warnings
with warnings.catch_warnings():
     warnings.simplefilter("error")
     # Code in this block will raise exception for a warning
# Code in this block will have default warning behaviour

捕捉所有警告可能会有问题。您可以捕获特定的警告。例如,我需要捕捉一个Pillow警告:

import warnings
warnings.filterwarnings("error", category=Image.DecompressionBombWarning)
def process_images():
  try:
    some_process()
  except Image.DecompressionBombWarning as e:
    print(e)

在某些情况下,需要使用ctypes将警告转换为错误。例如:

str(b'test')  # no error
import warnings
warnings.simplefilter('error', BytesWarning)
str(b'test')  # still no error
import ctypes
ctypes.c_int.in_dll(ctypes.pythonapi, 'Py_BytesWarningFlag').value = 2
str(b'test')  # this raises an error

为了完整起见,您还可以导出一个env变量:

PYTHONWARNINGS=error /usr/bin/run_my_python_utility

最新更新