3
tûœ]¢Q  ã               @   sè   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ dd„ ZdDdd	„Zejed
œdd„ZdEZejZdFdGdHdIdJdKdLdMdNdOdPdQdRdSd9œZG d:d;„ d;ƒZG d<d=„ d=eƒZG d>d?„ d?eƒZG d@dA„ dAejƒZdBdC„ ZdS )Tz,Checks for various exception related errors.é    N)ÚNodeNG)ÚcheckersÚ
interfaces)Úutilsc              C   s"   dd„ } t jt| ƒ}dd„ |D ƒS )Nc             S   s   t | tƒot| tƒS )N)Ú
isinstanceÚtypeÚ
issubclassÚBaseException)Úobj© r   ú</tmp/pip-build-fn42xldr/pylint/pylint/checkers/exceptions.pyÚ	predicate%   s    z&_builtin_exceptions.<locals>.predicatec             S   s   h | ]\}}|j ’qS r   )Ú__name__)Ú.0Ú_Úexcr   r   r   ú	<setcomp>)   s    z&_builtin_exceptions.<locals>.<setcomp>)ÚinspectÚ
getmembersÚbuiltins)r   Úmembersr   r   r   Ú_builtin_exceptions$   s    r   c             c   sv   t | tjtjfƒrHx0| jD ]&}tj|ƒ}|r|tjk	r||fV  qW dS x(| j|ƒD ]}|tjkrdqT| |fV  qTW dS )zð
    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', 'inferred node').
    N)	r   ÚastroidÚListÚTupleÚeltsr   Ú
safe_inferÚUninferableZinfer)ÚstmtÚcontextÚeltÚinferredr   r   r   Ú_annotated_unpack_infer,   s    

r"   )ÚbodyÚreturnc             C   s"   x| D ]}t |tjƒrdS qW dS )z:Return true if the given statement node raise an exceptionTF)r   r   ÚRaise)r#   Únoder   r   r   Ú_is_raising?   s    
r'   r	   Ú	ExceptionúBad except clauses order (%s)úbad-except-orderú¿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.ú6Raising %s while only classes or instances are allowedúraising-bad-typeútUsed when something which is neither a class, an instance or a string is raised (i.e. a `TypeError` will be raised).úFException context set to something which is not an exception, nor Noneúbad-exception-contextúkUsed when using the syntax "raise ... from ...", where the exception context is not an exception, nor None.ú2The raise statement is not inside an except clauseúmisplaced-bare-raiseág  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.úBRaising a new style class which doesn't inherit from BaseExceptionúraising-non-exceptionúOUsed when a new style class which doesn't inherit from BaseException is raised.ú8NotImplemented raised - should raise NotImplementedErrorúnotimplemented-raisedúAUsed when NotImplemented is raised instead of NotImplementedErrorú>Catching an exception which doesn't inherit from Exception: %súcatching-non-exceptionúcUsed when a class which doesn't inherit from Exception is used as an exception in an except clause.úNo exception type(s) specifiedúbare-exceptúDUsed when an except clause doesn't specify exceptions type to catch.ú!Catching too general exception %súbroad-exceptúWUsed when an except catches a too general exception, possibly burying unrelated errors.ú,Catching previously caught exception type %súduplicate-exceptúQUsed when an except catches a type that was already caught by a previous handler.ú%The except handler raises immediatelyútry-except-raiseúÇUsed when an except handler uses raise as its first or only operator. This is useless because it raises back the exception immediately. Remove the raise operator or the entire try-except-raise block!ú;Exception to catch is the result of a binary "%s" operationúbinary-op-exceptionú~Used when the exception to catch is of the form "except A or B:".  If intending to catch multiple, rewrite as "except (A, B):"ú?Exception arguments suggest string formatting might be intendedúraising-format-tupleú§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úInvalid exception operation. %súwrong-exception-operationúÇUsed when an operation is done against an exception, but the operation is not valid for the exception in question. Usually emitted when having binary operations between exceptions in except handlers.)ZE0701ZE0702ZE0703ZE0704ZE0710ZE0711ZE0712ZW0702ZW0703ZW0705ZW0706ZW0711ZW0715ZW0716c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBaseVisitorz/Base class for visitors defined in this module.c             C   s   || _ || _d S )N)Ú_checkerÚ_node)ÚselfÚcheckerr&   r   r   r   Ú__init__®   s    zBaseVisitor.__init__c             C   s8   |j jjƒ }t| d| d ƒ}|r*||ƒ n
| j|ƒ d S )NÚvisit_)Ú	__class__r   ÚlowerÚgetattrÚvisit_default)rV   r&   ÚnameZdispatch_methr   r   r   Úvisit²   s
    
zBaseVisitor.visitc             C   s   dS )z)Default implementation for all the nodes.Nr   )rV   r&   r   r   r   r]   º   s    zBaseVisitor.visit_defaultN)r   Ú
__module__Ú__qualname__Ú__doc__rX   r_   r]   r   r   r   r   rS   «   s   rS   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚExceptionRaiseRefVisitorz4Visit references (anything that is not an AST leaf).c             C   s    |j dkr| jjd| jd� d S )NÚNotImplementedznotimplemented-raised)r&   )r^   rT   Úadd_messagerU   )rV   r^   r   r   r   Ú
visit_nameÁ   s    
z#ExceptionRaiseRefVisitor.visit_namec             C   s†   t |jtjƒr| j|jƒ t|jƒdkr‚t |jd tjƒr‚t |jd jt	ƒr‚|jd j}d|kspd|kr‚d|kr‚| j
jd| jd� d S )Né   r   ú%Ú{Ú}zraising-format-tuple)r&   )r   Úfuncr   ÚNamerf   ÚlenÚargsÚConstÚvalueÚstrrT   re   rU   )rV   ÚcallÚmsgr   r   r   Ú
visit_callÅ   s    z#ExceptionRaiseRefVisitor.visit_callN)r   r`   ra   rb   rf   rt   r   r   r   r   rc   ¾   s   rc   c               @   s<   e Zd ZdZdd„ Zdd„ ZeZdd„ Zdd	„ Zd
d„ Z	dS )ÚExceptionRaiseLeafVisitorz1Visitor for handling leaf kinds of a raise value.c             C   s*   t |jtƒs&| jjd| j|jjjd� d S )Nzraising-bad-type)r&   rn   )r   rp   rq   rT   re   rU   rZ   r   )rV   Úconstr   r   r   Úvisit_constÕ   s    z%ExceptionRaiseLeafVisitor.visit_constc             C   s   |j }| j|ƒ d S )N)Ú_proxiedÚvisit_classdef)rV   ÚinstanceÚclsr   r   r   Úvisit_instanceÜ   s    z(ExceptionRaiseLeafVisitor.visit_instancec             C   s2   t j|ƒ r.t j|ƒr.|jr.| jjd| jd� d S )Nzraising-non-exception)r&   )r   Úinherit_from_std_exÚhas_known_basesZnewstylerT   re   rU   )rV   r{   r   r   r   ry   ä   s    z(ExceptionRaiseLeafVisitor.visit_classdefc             C   s   | j jd| jdd� d S )Nzraising-bad-typeÚtuple)r&   rn   )rT   re   rU   )rV   r   r   r   r   Úvisit_tupleé   s    z%ExceptionRaiseLeafVisitor.visit_tuplec             C   s(   t |d|jjƒ}| jjd| j|d� d S )Nr^   zraising-bad-type)r&   rn   )r\   rZ   r   rT   re   rU   )rV   r&   r^   r   r   r   r]   ì   s    z'ExceptionRaiseLeafVisitor.visit_defaultN)
r   r`   ra   rb   rw   r|   Zvisit_exceptioninstancery   r€   r]   r   r   r   r   ru   Ò   s   ru   c                   sÐ   e Zd ZdZejZdZeZ	d*Z
deddddjeƒf d	œffZ‡ fd
d„Zejddddddƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zejdƒdd„ ƒZejdƒdd „ ƒZejd!d"d#d$d%d&d'ƒd(d)„ ƒZ‡  ZS )+ÚExceptionsCheckerzException related checks.Ú
exceptionsé   zovergeneral-exceptionsÚcsvz<comma-separated class names>zHExceptions that will emit a warning when being caught. Defaults to "%s".z, )Údefaultr   ÚmetavarÚhelpc                s   t ƒ | _ tt| ƒjƒ  d S )N)r   Úsuperr�   Úopen)rV   )rZ   r   r   r‰     s    zExceptionsChecker.openzmisplaced-bare-raisezraising-bad-typezraising-non-exceptionznotimplemented-raisedzbad-exception-contextzraising-format-tuplec             C   s~   |j d kr| j|ƒ d S |jr(| j|ƒ |j }t| |ƒj|ƒ y|jƒ d }W n tjk
rd   Y nX |rzt	| |ƒj|ƒ d S )Nrg   éÿÿÿÿ)
r   Ú_check_misplaced_bare_raiseÚcauseÚ_check_bad_exception_contextrc   r_   r!   r   ÚInferenceErrorru   )rV   r&   ÚexprZinferred_valuer   r   r   Úvisit_raise  s    	


zExceptionsChecker.visit_raisec             C   s†   |j ƒ }t|tjƒr*|jƒ r*|jdkr*d S |}tjtjf}x|rVt|j|ƒ rV|j}q<W tjf}| stt|j|ƒ r‚| jd|d� d S )NÚ__exit__zmisplaced-bare-raise)r&   )	Úscoper   r   ÚFunctionDefZ	is_methodr^   ÚExceptHandlerÚparentre   )rV   r&   r’   ÚcurrentZignoresÚexpectedr   r   r   r‹   &  s    

z-ExceptionsChecker._check_misplaced_bare_raisec             C   sp   t j|jƒ}|tjdfkrdS t|tjƒrD|jdk	rl| jd|d� n(t|tj	ƒ rlt j
|ƒ rl| jd|d� dS )z}Verify that the exception context is properly set.

        An exception context can be only `None` or an exception.
        Nzbad-exception-context)r&   )r   r   rŒ   r   r   r   ro   rp   re   ÚClassDefr}   )rV   r&   rŒ   r   r   r   r�   ;  s    
z.ExceptionsChecker._check_bad_exception_contextc             C   s  t |tjƒrHdd„ |jD ƒ}tdd„ |D ƒƒr2d S tdd„ |D ƒƒrHd S 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|j	|jƒ fd� n| jd|j	|jƒ fd� d S tj|ƒ oÜ|j| jk�rtj|ƒ�r| jd|j	|jfd� d S )Nc             S   s   g | ]}t j|ƒ‘qS r   )r   r   )r   r    r   r   r   ú
<listcomp>O  s    zCExceptionsChecker._check_catching_non_exception.<locals>.<listcomp>c             s   s   | ]}|t jkV  qd S )N)r   r   )r   r&   r   r   r   ú	<genexpr>P  s    zBExceptionsChecker._check_catching_non_exception.<locals>.<genexpr>c             s   s(   | ] }|ot j|ƒpt j|ƒ V  qd S )N)r   r}   r~   )r   r&   r   r   r   rš   T  s   zcatching-non-exception)r&   rn   )r   r   r   r   ÚanyÚallr˜   ro   rp   r   Z	parent_ofre   Ú	as_stringr   r}   r^   r   r~   )rV   Úhandlerr   Úpartr!   r   r   r   Ú_check_catching_non_exceptionL  s6    
z/ExceptionsChecker._check_catching_non_exceptionc       	         sÐ   t jt jt  dœdd„}d}d }g }x¤|jD ]ˆ}|r†||ƒ}|sHd}P |d krRP x2|D ]*}tj|ƒ‰ t‡ fdd„|D ƒƒrXd}P qXW t|j	d gƒr.|j	d j
d kr.d}|}||ƒ}q.W |rÌ| jd	|d
� d S )N)r$   c             S   sL   g }| j rHtj| j ƒ}t|tjƒr8tdd„ |jD ƒƒ}n|rD|g}nd S |S )Nc             S   s   h | ]}t |tjƒr|’qS r   )r   r   rl   )r   Ú	exceptionr   r   r   r   †  s   zdExceptionsChecker._check_try_except_raise.<locals>.gather_exceptions_from_handler.<locals>.<setcomp>)r   r   r   r   r   r   Úlistr   )rž   r‚   Zexceptions_in_handlerr   r   r   Úgather_exceptions_from_handler~  s    zQExceptionsChecker._check_try_except_raise.<locals>.gather_exceptions_from_handlerFc             3   s    | ]}t jt j|ƒˆ ƒV  qd S )N)r   Zis_subclass_ofr   )r   Zexc_in_bare_handler)Úinferred_currentr   r   rš   ¨  s   z<ExceptionsChecker._check_try_except_raise.<locals>.<genexpr>r   Tztry-except-raise)r&   )ÚtypingÚOptionalr   r   Úhandlersr   r   r›   r'   r#   r   re   )	rV   r&   r£   Z
bare_raiseZhandler_having_bare_raiseZexcs_in_bare_handlerrž   Zexcs_in_current_handlerZexc_in_current_handlerr   )r¤   r   Ú_check_try_except_raise}  s4    



z)ExceptionsChecker._check_try_except_raisezwrong-exception-operationc             C   s<   t |jtjƒr8d|jjƒ |jjƒ f }| jd||fd� d S )Nz Did you mean '(%s, %s)' instead?zwrong-exception-operation)r&   rn   )r   r•   r   r”   Úleftr�   Úrightre   )rV   r&   Ú
suggestionr   r   r   Úvisit_binop»  s
    zExceptionsChecker.visit_binopc             C   sH   t |jtjƒrDd|jjƒ djdd„ |jD ƒƒf }| jd||fd� d S )Nz Did you mean '(%s, %s)' instead?z, c             s   s   | ]\}}|j ƒ V  qd S )N)r�   )r   r   Úoperandr   r   r   rš   Ë  s    z2ExceptionsChecker.visit_compare.<locals>.<genexpr>zwrong-exception-operation)r&   rn   )	r   r•   r   r”   r©   r�   ÚjoinÚopsre   )rV   r&   r«   r   r   r   Úvisit_compareÅ  s
    zExceptionsChecker.visit_comparezbare-exceptzbroad-exceptztry-except-raisezbinary-op-exceptionzbad-except-orderzcatching-non-exceptionzduplicate-exceptc             C   sê  | j |ƒ g }t|jƒ}�xÊt|jƒD �]º\}}|jdkrtt|jƒsR| jd|d� ||d k rrd}| jd||d� q&t|jt	j
ƒr˜| jd||jjd� q&ytt|jƒƒ}W n t	jk
rÂ   w&Y nX �x|D � ]þ\}}	|	t	jkrâqÌt|	t	jƒoötj|	ƒ�r |	j}	| j||	|ƒ t|	t	jƒ�sqÌd	d
„ |	jƒ D ƒ}
x:|D ]2}||
k�r6d|j|	jf }| jd|j|d� �q6W |	j| jjk�r°|	jƒ jtjk�r°t|jƒ �r°| jd|	j|jd� |	|krÌ| jd|	j|jd� qÌW |dd
„ |D ƒ7 }q&W dS )zcheck for empty exceptNzbare-except)r&   rg   z-empty except clause should always appear lastzbad-except-order)r&   rn   zbinary-op-exceptionc             S   s   g | ]}t |tjƒr|‘qS r   )r   r   r˜   )r   Zancr   r   r   r™     s   z5ExceptionsChecker.visit_tryexcept.<locals>.<listcomp>z%s is an ancestor class of %szbroad-except)rn   r&   zduplicate-exceptc             S   s   g | ]\}}|‘qS r   r   )r   r   r   r   r   r   r™     s    )r¨   rm   r§   Ú	enumerater   r'   r#   re   r   r   ÚBoolOpÚopr¢   r"   rŽ   r   ZInstancer   r}   rx   r    r˜   Z	ancestorsr^   ÚconfigZovergeneral_exceptionsÚrootZEXCEPTIONS_MODULE)rV   r&   Zexceptions_classesZnb_handlersÚindexrž   rs   ZexcsrŸ   r   Zexc_ancestorsZprevious_excr   r   r   Úvisit_tryexceptÏ  sX    







z!ExceptionsChecker.visit_tryexceptéüÿÿÿ)r   r`   ra   rb   r   ZIAstroidCheckerZ__implements__r^   ÚMSGSZmsgsÚpriorityÚOVERGENERAL_EXCEPTIONSr®   Úoptionsr‰   r   Zcheck_messagesr�   r‹   r�   r    r¨   r¬   r°   r·   Ú__classcell__r   r   )rZ   r   r�   ñ   sB   1>

r�   c             C   s   | j t| ƒƒ dS )z-required method to auto register this checkerN)Zregister_checkerr�   )Zlinterr   r   r   Úregister   s    r¾   )N)r	   r(   )r)   r*   r+   )r,   r-   r.   )r/   r0   r1   )r2   r3   r4   )r5   r6   r7   )r8   r9   r:   )r;   r<   r=   )r>   r?   r@   )rA   rB   rC   )rD   rE   rF   )rG   rH   rI   )rJ   rK   rL   )rM   rN   rO   )rP   rQ   rR   )rb   r   r   r¥   r   Zastroid.node_classesr   Zpylintr   r   Zpylint.checkersr   r   r"   r   Úboolr'   r»   r   ZBUILTINS_NAMEr¹   rS   rc   ru   ZBaseCheckerr�   r¾   r   r   r   r   Ú<module>   sz   
                              1