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

PythonSuppress WarningsParamikoPycrypto

Python Problem Overview


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)

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')

and even this:

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

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:

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.

Python Solutions


Solution 1 - Python

Easiest way would be as the warnings module suggests here:

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

Solution 2 - Python

The module argument to warnings.filterwarnings takes a case-sensitive regular expression which should match the fully qualified module name, so

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.

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:

$ 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.

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

Solution 3 - Python

To filter only a specific warning:

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

    #do something that raises a Warning

Solution 4 - Python

The most flexible way is to combine warnings.filterwarnings() with the warnings.catch_warnings() context manager. This way you ge the flexibility of filterwarnings but the filtering is only applied inside the with block:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTom NguyenView Question on Stackoverflow
Solution 1 - PythonsnapshoeView Answer on Stackoverflow
Solution 2 - PythonBlaine RogersView Answer on Stackoverflow
Solution 3 - PythonKyleView Answer on Stackoverflow
Solution 4 - Pythonnp8View Answer on Stackoverflow