如何使用 warnings.filterwarnings 抑制第三方警告 [英] How to suppress a third-party warning using warnings.filterwarnings

查看:96
本文介绍了如何使用 warnings.filterwarnings 抑制第三方警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 python 代码中使用 Paramiko(用于 sftp).一切正常,除了每次我导入或调用 paramiko 函数时.将显示此警告:

I am using Paramiko in my python code (for sftp). Everything works fine except that everytime I import or call a paramiko function. 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)

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

I know that this has to do with the fact that Paramiko is using some Deprecated functionalities of 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')

甚至这个:

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

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

before 'import paramiko' statement and before paramiko-specific function calls, but nothing works. This warning keeps showing up no matter what. If it helps, here's the code in the third party library that prints the warning:

在 randpool.py 中:

in 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

这篇关于如何使用 warnings.filterwarnings 抑制第三方警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆