2010-10-13 4 views
17

J'utilise Paramiko dans mon code python (pour sftp). Tout fonctionne bien sauf que chaque fois que j'importe ou appelle une fonction paramiko. Cet avertissement apparaîtrait:Comment supprimer un avertissement d'un tiers en utilisant warnings.filterwarnings

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) 

Je sais que cela a à voir avec le fait que Paramiko utilise certaines fonctionnalités dépréciées des PyCrypto.

Ma question est, est-il un moyen de supprimer cet avertissement par programme? Je l'ai essayé:

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

et même ceci:

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

avant 'importation paramiko' déclaration et avant les appels de fonction spécifiques à paramiko, mais rien ne fonctionne. Cet avertissement continue d'apparaître quoi qu'il arrive. Si elle aide, voici le code dans la bibliothèque tiers qui imprime l'avertissement:

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

Si vous connaissez un moyen de contourner cela, s'il vous plaît me aider à fermer cet avertissement au large.

Répondre

28

manière la plus simple serait que le module avertissements suggère here:

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

Comment voulez-vous modifier cela pour filtrer uniquement l'OP d'avertissement mentions particulières? – VF1

0

filtre seulement un avertissement spécifique:

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

    #do something that raises a Warning 
Questions connexes