简体   繁体   English

如何使用 warnings.filterwarnings 抑制第三方警告

[英]How to suppress a third-party warning using warnings.filterwarnings

I am using Paramiko in my python code (for sftp).我在我的 python 代码中使用 Paramiko(用于 sftp)。 Everything works fine except that everytime I import or call a paramiko function.一切正常,除了每次我导入或调用 paramiko 函数时。 This warning would show up:这个警告会出现:

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)

I know that this has to do with the fact that Paramiko is using some Deprecated functionalities of PyCrypto.我知道这与 Paramiko 正在使用 PyCrypto 的一些已弃用功能有关。

My question is, is there a way to suppress this warning programmatically ?我的问题是,有没有办法以编程方式抑制此警告? I have tried this:我试过这个:

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

and even this:甚至这个:

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

before 'import paramiko' statement and before paramiko-specific function calls, but nothing works.在 'import paramiko' 语句之前和 paramiko 特定的函数调用之前,但没有任何效果。 This warning keeps showing up no matter what.无论如何,此警告都会不断出现。 If it helps, here's the code in the third party library that prints the warning:如果有帮助,这里是打印警告的第三方库中的代码:

in randpool.py:在 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)

If you know a way around this, please help me shut this warning off.如果您知道解决此问题的方法,请帮助我关闭此警告。

Easiest way would be as the warnings module suggests here :最简单的方法是警告模块在这里建议:

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

The module argument to warnings.filterwarnings takes a case-sensitive regular expression which should match the fully qualified module name, so warnings.filterwarningsmodule参数采用区分大小写的正则表达式,该表达式应与完全限定的模块名称匹配,因此

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

or

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

should work.应该工作。 You may need to write RandomPool_DeprecationWarning explicitly instead of DeprecationWarning if for some reason RandomPool_DeprecationWarning is not a subclass of DeprecationWarning .您可能需要编写RandomPool_DeprecationWarning明确,而不是DeprecationWarning如果由于某种原因RandomPool_DeprecationWarning不是一个子类DeprecationWarning

You can also disable the warning on the command line when you invoke the script by passing the -W option to the interpreter like so:您还可以在调用脚本时通过将-W选项传递给解释器来禁用命令行上的警告,如下所示:

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

The -W takes filters in the format action:message:category:module:lineno , where this time module must exactly match the (fully-qualified) module name where the warning is raised. -W采用格式为action:message:category:module:lineno过滤器,此时module必须与引发警告的(完全限定的)模块名称完全匹配。

See https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filter and https://docs.python.org/2/using/cmdline.html#cmdoption-w请参阅https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filterhttps://docs.python.org/2/using/cmdline.html#cmdoption-w

To filter only a specific warning:仅过滤特定警告:

with warnings.catch_warnings():
    warnings.simplefilter('ignore', SpecificWarningObject)

    #do something that raises a Warning

The most flexible way is to combine warnings.filterwarnings() with the warnings.catch_warnings() context manager.最灵活的方法是将warnings.filterwarnings()warnings.catch_warnings()上下文管理器结合起来。 This way you ge the flexibility of filterwarnings but the filtering is only applied inside the with block:通过这种方式,您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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能使用 warnings.filterwarnings 用正则表达式抑制警告 - why I cannot suppress warning with regex using warnings.filterwarnings 使用 warnings.filterwarnings() 将警告转换为异常 - using warnings.filterwarnings() to convert a warning into an exception 无法使用 warnings.filterwarnings("ignore") 抑制 Python 警告 - Cannot supress Python Warnings with warnings.filterwarnings("ignore") 在不修改第三方代码的情况下禁止警告 - Suppress warning without modifying third party code python warnings.filterwarnings 不会忽略“import sklearn.ensemble”中的 DeprecationWarning - python warnings.filterwarnings does not ignore DeprecationWarning from 'import sklearn.ensemble' 如何在运行单元测试时摆脱第三方库警告? - How to get rid of third-party library warnings while running unit tests? 如何在没有第三方库的情况下使用python验证xml? - How to validate xml using python without third-party libs? Python:如何使用第三方库 - Python: How to use a third-party library 如何抑制 pytest 中的第三方日志 - How to suppress third party logs in pytest 抑制第三方服务不可用时引发的异常的最佳方法是什么? - Best way to suppress exceptions raised when third-party service is unavailable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM