
'![c           @   s  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l Z d d l m Z d d l	 m
 Z
 d d l m Z d   Z d d  Z e j dK k Z dL Z e j Z i dM d 6dN d 6d d d i dO d 6f d 6dP d 6dQ d  6dR d$ 6dS d( 6dT d, 6dU d0 6dV d4 6d5 d6 d7 i dW d8 6f d9 6dX d= 6dY dA 6Z dB e f dC     YZ dD e f dE     YZ dF e f dG     YZ dH e j f dI     YZ dJ   Z d S(Z   s,   Checks for various exception related errors.iN(   t   builtins(   t   checkers(   t   utils(   t
   interfacesc          C   s/   d   }  t  j t j j |   } d   | D S(   Nc         S   s   t  |  t  o t |  t  S(   N(   t
   isinstancet   typet
   issubclasst   BaseException(   t   obj(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt	   predicate#   s    c         S   s   h  |  ] \ } } | j   q S(    (   t   __name__(   t   .0t   _t   exc(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pys	   <setcomp>'   s   	 (   t   inspectt
   getmemberst   sixt   movesR    (   R	   t   members(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   _builtin_exceptions"   s    	c         c   s   t  |  t j t j f  re xC |  j D]8 } t j |  } | r% | t j k	 r% | | f Vq% q% Wd Sx7 |  j |  D]& } | t j k r qu n  |  | f Vqu Wd S(   s   
    Recursively generate nodes inferred by the given statement.
    If the inferred value is a list or a tuple, recurse on the elements.
    Returns an iterator which yields tuples in the format
    ('original node', 'infered node').
    N(	   R   t   astroidt   Listt   Tuplet   eltsR   t
   safe_infert   YESt   infer(   t   stmtt   contextt   eltt   inferredt   infered(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   _annotated_unpack_infer*   s    i   i    t	   Exceptions   Bad except clauses order (%s)s   bad-except-orders   Used when except clauses are not in the correct order (from the more specific to the more generic). If you don't fix the order, some exceptions may not be caught by the most specific handler.t   E0701s6   Raising %s while only classes or instances are alloweds   raising-bad-types   Used when something which is neither a class, an instance or a               string is raised (i.e. a `TypeError` will be raised).t   E0702sF   Exception context set to something which is not an exception, nor Nones   bad-exception-contextsk   Used when using the syntax "raise ... from ...", where the exception context is not an exception, nor None.t
   minversiont   E0703s2   The raise statement is not inside an except clauses   misplaced-bare-raisesg  Used when a bare raise is not used inside an except clause. This generates an error, since there are no active exceptions to be reraised. An exception to this rule is represented by a bare raise inside a finally clause, which might work, as long as an exception is raised inside the try block, but it is nevertheless a code smell that must not be relied upon.t   E0704sB   Raising a new style class which doesn't inherit from BaseExceptions   raising-non-exceptions^   Used when a new style class which doesn't inherit from                BaseException is raised.t   E0710s8   NotImplemented raised - should raise NotImplementedErrors   notimplemented-raisedsO   Used when NotImplemented is raised instead of               NotImplementedErrort   E0711s>   Catching an exception which doesn't inherit from Exception: %ss   catching-non-exceptionsr   Used when a class which doesn't inherit from                Exception is used as an exception in an except clause.t   E0712s   No exception type(s) specifieds   bare-exceptsR   Used when an except clause doesn't specify exceptions type to               catch.t   W0702s!   Catching too general exception %ss   broad-exceptse   Used when an except catches a too general exception,               possibly burying unrelated errors.t   W0703s,   Catching previously caught exception type %ss   duplicate-exceptsQ   Used when an except catches a type that was already caught by a previous handler.t   W0705s9   Exception doesn't inherit from standard "Exception" classs   nonstandard-exceptionsr   Used when a custom exception class is raised but doesn't               inherit from the builtin "Exception" class.t
   maxversiont   W0710s;   Exception to catch is the result of a binary "%s" operations   binary-op-exceptions   Used when the exception to catch is of the form               "except A or B:".  If intending to catch multiple,               rewrite as "except (A, B):"t   W0711s?   Exception arguments suggest string formatting might be intendeds   raising-format-tuples   Used when passing multiple arguments to an exception               constructor, the first of them a string literal containing what               appears to be placeholders intended for formattingt   W0715t   BaseVisitorc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s/   Base class for visitors defined in this module.c         C   s   | |  _  | |  _ d  S(   N(   t   _checkert   _node(   t   selft   checkert   node(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   __init__   s    	c         C   sL   | j  j j   } t |  d | d   } | r; | |  n |  j |  d  S(   Nt   visit_(   t	   __class__R
   t   lowert   getattrt   Nonet   visit_default(   R4   R6   t   namet   dispatch_meth(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   visit   s
    c         C   s   d S(   s)   Default implementation for all the nodes.N(    (   R4   R6   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyR=      s    (   R
   t
   __module__t   __doc__R7   R@   R=   (    (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyR1      s   		t   ExceptionRaiseRefVisitorc           B   s    e  Z d  Z d   Z d   Z RS(   s4   Visit references (anything that is not an AST leaf).c         C   s/   | j  d k r+ |  j j d d |  j n  d  S(   Nt   NotImplementeds   notimplemented-raisedR6   (   R>   R2   t   add_messageR3   (   R4   R>   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt
   visit_name   s    	c         C   s   t  | j t j  r( |  j | j  n  t | j  d k r t  | j d t j  r t  | j d j t	 j
  r | j d j } d | k s d | k r d | k r |  j j d d |  j q n  d  S(   Ni   i    t   %t   {t   }s   raising-format-tupleR6   (   R   t   funcR   t   NameRF   t   lent   argst   Constt   valueR   t   string_typesR2   RE   R3   (   R4   t   callt   msg(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt
   visit_call   s    	(   R
   RA   RB   RF   RS   (    (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRC      s   	t   ExceptionRaiseLeafVisitorc           B   sA   e  Z d  Z d   Z d   Z e Z d   Z d   Z d   Z RS(   s1   Visitor for handling leaf kinds of a raise value.c         C   sA   t  | j t  s= |  j j d d |  j d | j j j n  d  S(   Ns   raising-bad-typeR6   RM   (   R   RO   t   strR2   RE   R3   R9   R
   (   R4   t   const(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   visit_const   s    c         C   s   | j  } |  j |  d  S(   N(   t   _proxiedt   visit_classdef(   R4   t   instancet   cls(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   visit_instance   s    	c         C   sd   t  j |  r` t  j |  r` | j rD |  j j d d |  j q` |  j j d d |  j n  d  S(   Ns   raising-non-exceptionR6   s   nonstandard-exception(   R   t   inherit_from_std_ext   has_known_basest   newstyleR2   RE   R3   (   R4   R[   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRY      s
    	c         C   s   t  s | j r3 |  j j d d |  j d d d  S| j d } t j |  } | se | t j k ri d  St	 | t j
  r | j j d k r |  j |  n |  j |  d  S(   Ns   raising-bad-typeR6   RM   t   tuplei    t   Instance(   t   PY3KR   R2   RE   R3   R   R   R   t   UninferableR   Ra   R9   R
   R=   R@   (   R4   t
   tuple_nodet   firstR   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   visit_tuple   s    	c         C   s;   t  | d | j j  } |  j j d d |  j d | d  S(   NR>   s   raising-bad-typeR6   RM   (   R;   R9   R
   R2   RE   R3   (   R4   R6   R>   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyR=      s    	(	   R
   RA   RB   RW   R\   t   visit_exceptioninstanceRY   Rf   R=   (    (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRT      s   				t   ExceptionsCheckerc           B   s   e  Z d  Z e j Z d Z e Z d Z	 d i e
 d 6d d 6d d 6d	 d
 j e
  f d 6f f Z d   Z e j d d d d d d d  d    Z d   Z d   Z d   Z e j d d d d d d  d    Z RS(   s   Exception related checks.t
   exceptionsis   overgeneral-exceptionst   defaultt   csvR   s   <comma-separated class names>t   metavarsG   Exceptions that will emit a warning when being caught. Defaults to "%s"s   , t   helpc         C   s#   t    |  _  t t |   j   d  S(   N(   R   t   superRh   t   open(   R4   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRo      s    s   nonstandard-exceptions   misplaced-bare-raises   raising-bad-types   raising-non-exceptions   notimplemented-raiseds   bad-exception-contexts   raising-format-tuplec         C   s   | j  d  k r  |  j |  d  St r? | j r? |  j |  n  | j  } y t | j    } Wn t j	 k
 rz d  } n Xt
 |  |  j |  | r t |  |  j |  n  d  S(   N(   R   R<   t   _check_misplaced_bare_raiseRb   t   causet   _check_bad_exception_contextt   nextR   R   t   InferenceErrorRC   R@   RT   (   R4   R6   t   exprt   inferred_value(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   visit_raise   s    	
c         C   s   | j    } t | t j  r= | j   r= | j d k r= d  S| } t j t j t j f } x& | r t | j |  r | j } q^ Wt j f } | s t | j |  r |  j	 d d | n  d  S(   Nt   __exit__s   misplaced-bare-raiseR6   (
   t   scopeR   R   t   FunctionDeft	   is_methodR>   t   ExceptHandlert
   TryFinallyt   parentRE   (   R4   R6   Ry   t   currentt   ignorest   expected(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRp     s    c         C   s   t  j | j  } | t j d f k r+ d St | t j  re | j d k	 r |  j	 d d | q n9 t | t j
  r t  j |  r |  j	 d d | n  d S(   s}   Verify that the exception context is properly set.

        An exception context can be only `None` or an exception.
        Ns   bad-exception-contextR6   (   R   R   Rq   R   R   R<   R   RN   RO   RE   t   ClassDefR]   (   R4   R6   Rq   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRr   &  s    c         C   s  t  | t j  rn g  | j D] } t j |  ^ q } t d   | D  rQ d  St d   | D  rn d  Sn  t  | t j  s.t  | t j	  r| j
 d  k rt  | j t j	  r | j j
 d  k s | j j |  r*|  j d d | j d | j   f q*n% |  j d d | j d | j   f d  St j |  r| j |  j k rt j |  r|  j d d | j d | j f qn  d  S(   Nc         s   s   |  ] } | t  j k Vq d  S(   N(   R   R   (   R   R6   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pys	   <genexpr><  s    c         s   s4   |  ]* } | o+ t  j |  p+ t  j |  Vq d  S(   N(   R   R]   R^   (   R   R6   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pys	   <genexpr>?  s   s   catching-non-exceptionR6   RM   (   R   R   R   R   R   R   t   anyt   allR   RN   RO   R<   R   t	   parent_ofRE   t	   as_stringR]   R>   R   R^   (   R4   t   handlerR   t   partR   R   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   _check_catching_non_exception8  s4    %				s   bare-excepts   broad-excepts   binary-op-exceptions   bad-except-orders   catching-non-exceptions   duplicate-exceptc      	   C   s  g  } t  | j  } xt | j  D]\ } } | j d k r t j | j  sh |  j d d | n  | | d k  rd } |  j d d | d | qq% t	 | j t
 j  r |  j d d | d | j j q% y t t | j   } Wn t
 j k
 rq% n Xx| D]\ } }	 |	 t
 j k r/qn  t	 |	 t
 j  r\t j |	  r\|	 j }	 n  |  j | |	 |  t	 |	 t
 j  sqn  g  |	 j   D] }
 t	 |
 t
 j  r|
 ^ q} xO | D]G } | | k rd | j |	 j f } |  j d d | j d | qqW|	 j |  j j k rl|	 j   j t j k rlt j | j  rl|  j d	 d |	 j d | j n  |	 | k r|  j d
 d |	 j d | j qqW| g  | D] \ } }	 |	 ^ q7} q% Wd S(   s   check for empty excepts   bare-exceptR6   i   s-   empty except clause should always appear lasts   bad-except-orderRM   s   binary-op-exceptions   %s is an ancestor class of %ss   broad-excepts   duplicate-exceptN(   RL   t   handlerst	   enumerateR   R<   R   t
   is_raisingt   bodyRE   R   R   t   BoolOpt   opt   listR    Rt   R   Ra   R]   RX   R   R   t	   ancestorsR>   t   configt   overgeneral_exceptionst   roott   EXCEPTIONS_MODULE(   R4   R6   t   exceptions_classest   nb_handlerst   indexR   RR   t   excsR   R   t   anct   exc_ancestorst   previous_excR   (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   visit_tryexcepta  sT    (   R
   RA   RB   R   t   IAstroidCheckert   __implements__R>   t   MSGSt   msgst   priorityt   OVERGENERAL_EXCEPTIONSt   joint   optionsRo   R   t   check_messagesRw   Rp   Rr   R   R   (    (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyRh      s*   	
				)c         C   s   |  j  t |    d S(   s-   required method to auto register this checkerN(   t   register_checkerRh   (   t   linter(    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   register  s    (   i   i    (   R!   (   s   Bad except clauses order (%s)s   bad-except-orders   Used when except clauses are not in the correct order (from the more specific to the more generic). If you don't fix the order, some exceptions may not be caught by the most specific handler.(   s6   Raising %s while only classes or instances are alloweds   raising-bad-types   Used when something which is neither a class, an instance or a               string is raised (i.e. a `TypeError` will be raised).(   i   i    (   s2   The raise statement is not inside an except clauses   misplaced-bare-raisesg  Used when a bare raise is not used inside an except clause. This generates an error, since there are no active exceptions to be reraised. An exception to this rule is represented by a bare raise inside a finally clause, which might work, as long as an exception is raised inside the try block, but it is nevertheless a code smell that must not be relied upon.(   sB   Raising a new style class which doesn't inherit from BaseExceptions   raising-non-exceptions^   Used when a new style class which doesn't inherit from                BaseException is raised.(   s8   NotImplemented raised - should raise NotImplementedErrors   notimplemented-raisedsO   Used when NotImplemented is raised instead of               NotImplementedError(   s>   Catching an exception which doesn't inherit from Exception: %ss   catching-non-exceptionsr   Used when a class which doesn't inherit from                Exception is used as an exception in an except clause.(   s   No exception type(s) specifieds   bare-exceptsR   Used when an except clause doesn't specify exceptions type to               catch.(   s!   Catching too general exception %ss   broad-exceptse   Used when an except catches a too general exception,               possibly burying unrelated errors.(   s,   Catching previously caught exception type %ss   duplicate-exceptsQ   Used when an except catches a type that was already caught by a previous handler.(   i   i    (   s;   Exception to catch is the result of a binary "%s" operations   binary-op-exceptions   Used when the exception to catch is of the form               "except A or B:".  If intending to catch multiple,               rewrite as "except (A, B):"(   s?   Exception arguments suggest string formatting might be intendeds   raising-format-tuples   Used when passing multiple arguments to an exception               constructor, the first of them a string literal containing what               appears to be placeholders intended for formatting(   RB   R   t   sysR   t	   six.movesR    R   t   pylintR   t   pylint.checkersR   R   R   R<   R    t   version_infoRb   R   R
   t   BUILTINS_NAMER   t   objectR1   RC   RT   t   BaseCheckerRh   R   (    (    (    s9   lib/python2.7/site-packages/pylint/checkers/exceptions.pyt   <module>   sx   		                      
9