【问题标题】:How to suppress a third-party warning using warnings.filterwarnings如何使用 warnings.filterwarnings 抑制第三方警告
【发布时间】:2010-10-13 03:08:02
【问题描述】:

我在我的 python 代码(用于 sftp)中使用 Paramiko。一切正常,除了每次我导入或调用 paramiko 函数。会出现此警告:

C:\Python26\lib\site-packages\Crypto\Util\randpool.py:40: RandomPool_Deprecation
Warning: This application uses RandomPool, which is BROKEN in older releases.  S
ee http://www.pycrypto.org/randpool-broken
  RandomPool_DeprecationWarning)

我知道这与 Paramiko 使用 PyCrypto 的一些已弃用功能有关。

我的问题是,有没有办法以编程方式抑制此警告? 我试过这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')

甚至这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')

在“import paramiko”语句之前和 paramiko 特定函数调用之前,但没有任何效果。无论如何,此警告都会不断出现。 如果有帮助,这里是第三方库中打印警告的代码:

在 randpool.py 中:

from Crypto.pct_warnings import RandomPool_DeprecationWarning
import Crypto.Random
import warnings

class RandomPool:
    """Deprecated.  Use Random.new() instead.

    See http://www.pycrypto.org/randpool-broken
    """
    def __init__(self, numbytes = 160, cipher=None, hash=None, file=None):
        warnings.warn("This application uses RandomPool, which is BROKEN in older releases.  See http://www.pycrypto.org/randpool-broken",
            RandomPool_DeprecationWarning)

如果您知道解决此问题的方法,请帮我关闭此警告。

【问题讨论】:

    标签: python suppress-warnings paramiko pycrypto


    【解决方案1】:

    最简单的方法是警告模块建议here

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        import paramiko
    

    【讨论】:

    • 您将如何修改它以仅过滤 OP 提到的特定警告?
    【解决方案2】:

    warnings.filterwarningsmodule 参数采用区分大小写的正则表达式,它应该匹配完全限定的模块名称,所以

    warnings.filterwarnings(
        action='ignore',
        category=DeprecationWarning,
        module=r'.*randpool'
    )
    

    warnings.filterwarnings(
        action='ignore',
        category=DeprecationWarning,
        module=r'Crypto\.Utils\.randpool'
    )
    

    应该可以。如果出于某种原因 RandomPool_DeprecationWarning 不是 DeprecationWarning 的子类,您可能需要显式编写 RandomPool_DeprecationWarning 而不是 DeprecationWarning

    您还可以在调用脚本时禁用命令行上的警告,方法是将-W 选项传递给解释器,如下所示:

    $ python -W ignore::RandomPool_DeprecationWarning:Crypto.Utils.randpool: my_script.py
    

    -W 采用action:message:category:module:lineno 格式的过滤器,此时module 必须与引发警告的(完全限定的)模块名称完全匹配。

    https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filterhttps://docs.python.org/2/using/cmdline.html#cmdoption-w

    【讨论】:

    • import自定义警告,然后在category中使用
    【解决方案3】:

    仅过滤特定警告:

    with warnings.catch_warnings():
        warnings.simplefilter('ignore', SpecificWarningObject)
    
        #do something that raises a Warning
    

    【讨论】:

      【解决方案4】:

      最灵活的方法是将warnings.filterwarnings()warnings.catch_warnings() 上下文管理器结合使用。这样你就获得了filterwarnings 的灵活性,但过滤只在with 块内应用:

      import warnings
      from Crypto.pct_warnings import RandomPool_DeprecationWarning
      
      with warnings.catch_warnings():
          warnings.filterwarnings(
              action='ignore',
              category=RandomPool_DeprecationWarning,
              message='This application uses RandomPool, which is BROKEN in older releases')
         
          # Do stuff that causes the warning
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 2021-02-08
        • 2019-11-04
        • 1970-01-01
        • 2018-07-11
        • 2019-02-25
        • 2011-03-18
        相关资源
        最近更新 更多