[英]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.如果您知道解决此问题的方法,请帮助我关闭此警告。
The module
argument to warnings.filterwarnings
takes a case-sensitive regular expression which should match the fully qualified module name, so warnings.filterwarnings
的module
参数采用区分大小写的正则表达式,该表达式应与完全限定的模块名称匹配,因此
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-filter和https://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.