14.11 输出警告信息

问题

你希望自己的程序能生成警告信息(比如废弃特性或使用问题)。

解决方案

要输出一个警告消息,可使用 warning.warn() 函数。例如:

  1. import warnings
  2.  
  3. def func(x, y, logfile=None, debug=False):
  4. if logfile is not None:
  5. warnings.warn('logfile argument deprecated', DeprecationWarning)
  6. ...

warn() 的参数是一个警告消息和一个警告类,警告类有如下几种:UserWarning, DeprecationWarning,SyntaxWarning, RuntimeWarning, ResourceWarning, 或 FutureWarning.

对警告的处理取决于你如何运行解释器以及一些其他配置。例如,如果你使用 -W all 选项去运行Python,你会得到如下的输出:

  1. bash % python3 -W all example.py
  2. example.py:5: DeprecationWarning: logfile argument is deprecated
  3. warnings.warn('logfile argument is deprecated', DeprecationWarning)

通常来讲,警告会输出到标准错误上。如果你想讲警告转换为异常,可以使用 -W error 选项:

  1. bash % python3 -W error example.py
  2. Traceback (most recent call last):
  3. File "example.py", line 10, in <module>
  4. func(2, 3, logfile='log.txt')
  5. File "example.py", line 5, in func
  6. warnings.warn('logfile argument is deprecated', DeprecationWarning)
  7. DeprecationWarning: logfile argument is deprecated
  8. bash %

讨论

在你维护软件,提示用户某些信息,但是又不需要将其上升为异常级别,那么输出警告信息就会很有用了。例如,假设你准备修改某个函数库或框架的功能,你可以先为你要更改的部分输出警告信息,同时向后兼容一段时间。你还可以警告用户一些对代码有问题的使用方式。

作为另外一个内置函数库的警告使用例子,下面演示了一个没有关闭文件就销毁它时产生的警告消息:

  1. >>> import warnings
  2. >>> warnings.simplefilter('always')
  3. >>> f = open('/etc/passwd')
  4. >>> del f
  5. __main__:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/passwd'
  6. mode='r' encoding='UTF-8'>
  7. >>>

默认情况下,并不是所有警告消息都会出现。-W 选项能控制警告消息的输出。-W all 会输出所有警告消息,-W ignore 忽略掉所有警告,-W error 将警告转换成异常。另外一种选择,你还可以使用 warnings.simplefilter() 函数控制输出。always 参数会让所有警告消息出现,`ignore 忽略调所有的警告,error 将警告转换成异常。

对于简单的生成警告消息的情况这些已经足够了。warnings 模块对过滤和警告消息处理提供了大量的更高级的配置选项。更多信息请参考 Python文档

原文:

http://python3-cookbook.readthedocs.io/zh_CN/latest/c14/p11_issuing_warning_messages.html