3
tûœ]ÇH  ã            
   @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZ ejdƒZ	ejdƒZ
ejZejdƒZejƒ Zd	Zd
Zed dhZddddddddddh
Zdd„ ZG dd„ dƒZd+dd„Zdd„ ZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$eƒZG d%d&„ d&eƒZG d'd(„ d(eƒZG d)d*„ d*eƒZdS ),zXThis module contains base classes and functions for the nodes and some
inference utils.
é    N)Úcontext)Ú
exceptions)Úutilzinterpreter.objectmodelÚhelpersÚmanagerÚbuiltinsÚ__bool__z	.propertyzabc.abstractpropertyÚcached_propertyZcachedpropertyZlazypropertyZlazy_propertyZreifyZlazyattributeZlazy_attributeZLazyPropertyZlazyZcache_readonlyc                sÒ   t j| jƒ ƒrdS dd„ | jƒ D ƒ‰ t‡ fdd„tD ƒƒr>dS | jsHdS x„| jjpTf D ]t}tj|ƒ}|d ksV|t	j
krxqV|jjdkrVxD|jD ]:}|jjdkržqŒ|j|jƒ\}}|jtkrŒ|jd	krŒdS qŒW qVW dS )
NTc             S   s$   h | ]}|t jk	r|jd ƒd ’qS )Ú.é   éÿÿÿÿ)r   ÚUninferableÚsplit)Ú.0Úname© r   ú0/tmp/pip-build-fn42xldr/astroid/astroid/bases.pyú	<setcomp>@   s   z_is_property.<locals>.<setcomp>c             3   s   | ]}|ˆ kV  qd S )Nr   )r   r   )Ústrippedr   r   ú	<genexpr>E   s    z_is_property.<locals>.<genexpr>FÚClassDefÚNameÚproperty)Ú
PROPERTIESÚintersectionZdecoratornamesÚanyÚPOSSIBLE_PROPERTIESÚ
decoratorsZnodesr   Z
safe_inferr   r   Ú	__class__Ú__name__ÚbasesÚlookupr   ÚBUILTINS)ÚmethZ	decoratorÚinferredZ
base_classÚmoduleÚ_r   )r   r   Ú_is_property=   s(    
r'   c               @   s0   e Zd ZdZdZd	dd„Zdd„ Zd
dd„ZdS )ÚProxyz£a simple proxy object

    Note:

    Subclasses of this object will need a custom __getattr__
    if new instance attributes are created. See the Const class
    Nc             C   s   |d k	r|| _ d S )N)Ú_proxied)ÚselfZproxiedr   r   r   Ú__init__e   s    zProxy.__init__c             C   s4   |dkrt | jdƒS || jkr(| j| S t | j|ƒS )Nr)   )Úgetattrr   Ú__dict__r)   )r*   r   r   r   r   Ú__getattr__i   s
    

zProxy.__getattr__c             c   s
   | V  d S )Nr   )r*   r   r   r   r   Úinferp   s    zProxy.infer)N)N)r   Ú
__module__Ú__qualname__Ú__doc__r)   r+   r.   r/   r   r   r   r   r(   Z   s
   
r(   c             c   sÒ   d}|dk	r|j }|jƒ }nd}tjƒ }xŽ| D ]†}|tjkrH|V  d}q.|j||ƒ|_ y$x|j|d�D ]}|V  d}qfW W q. tj	k
r’   w.Y q. tj
k
r²   tjV  d}Y q.X q.W |sÎtj
d| ||d�‚dS )zGReturn an iterator on statements inferred by each statement in *stmts*.FNT)r   z.Inference failed for all members of {stmts!r}.)ÚstmtsÚframer   )Z
lookupnameÚcloneÚ
contextmodÚInferenceContextr   r   Z_infer_namer/   r   ZNameInferenceErrorÚInferenceError)r3   r   r4   r$   r   Ústmtr   r   r   Ú_infer_stmtst   s6    


r:   c             C   sŽ   t | j||d�d ƒ}|rˆt|dƒrˆ|jƒ s0tjS y@x:|j| |d�D ](}|tjkrT|S t |j|d�ƒ}|jƒ S W W n t	j
k
r†   Y nX tjS )N)r   Úinfer_call_result)ÚnextÚigetattrÚhasattrÚcallabler   r   r;   r/   Ú
bool_valuer   r8   )ÚinstanceÚmethod_namer   r#   Úvaluer$   r   r   r   Ú_infer_method_result_truth–   s    
rD   c               @   sD   e Zd ZdZdZdd„ Zddd„Zddd	„Zdd
d„Zddd„Z	dS )ÚBaseInstancezNAn instance base class, which provides lookup methods for potential instances.Nc             C   s   dS )NzInstance ofr   )r*   r   r   r   Údisplay_type®   s    zBaseInstance.display_typeTc             C   s´   y| j j||ƒ}W nj tjk
r| } zL| jrD|| jkrD| jj|ƒgS |rZ| j j||dd�S tj| ||d�|‚W Y d d }~X nX |r°y|| j j||dd� S  tjk
r®   Y nX |S )NF)Úclass_context)ÚtargetÚ	attributer   )r)   Zinstance_attrr   ÚAttributeInferenceErrorÚspecial_attributesr!   r,   )r*   r   r   ÚlookupclassÚvaluesÚexcr   r   r   r,   ±   s"    
zBaseInstance.getattrc             c   sö   |st jƒ }yP|j| j|fƒr.tjd| |d�‚| j||dd�}t| j||ƒ|| d�E dH  W n” tj	k
rð } zvy8| jj
jdkr‚‚ | jj||dd�}| j||ƒE dH  W n6 tj	k
rÞ } ztjf t|ƒŽ|‚W Y dd}~X nX W Y dd}~X nX dS )	zinferred getattrz%Cannot infer the same attribute again)ÚmessageÚnoder   F)rL   )r4   Nr   )rG   )r6   r7   Úpushr)   r   r8   r,   r:   Ú
_wrap_attrrJ   r   r   r=   Úvars)r*   r   r   Zget_attrÚerrorÚattrsr   r   r   r=   Ë   s&    zBaseInstance.igetattrc             c   s�   xŠ|D ]‚}t |tƒr>t|ƒr0|j| |ƒE dH  qˆt|| ƒV  qt|dƒr‚|jdkr‚|jjrz|jjd jdkrzt|| ƒV  q|V  q|V  qW dS )z7wrap bound methods of attrs in a InstanceMethod proxiesNr   z<lambda>r   r*   )Ú
isinstanceÚUnboundMethodr'   r;   ÚBoundMethodr>   r   Úargs)r*   rU   r   Úattrr   r   r   rR   é   s    

zBaseInstance._wrap_attrc             c   sv   t j|| ƒ}d}xL| jjd|ƒD ]:}|tjks |jƒ  r:q x|j||ƒD ]}d}|V  qHW q W |srtj	| ||d�‚dS )z4infer what a class instance is returning when calledFÚ__call__T)rP   Úcallerr   N)
r6   Úbind_context_to_noder)   r=   r   r   r?   r;   r   r8   )r*   r\   r   r$   rP   Úresr   r   r   r;   ù   s    zBaseInstance.infer_call_result)NT)N)N)N)
r   r0   r1   r2   rK   rF   r,   r=   rR   r;   r   r   r   r   rE   ©   s   


rE   c               @   sX   e Zd ZdZejdd„ ƒZdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zddd„ZdS )ÚInstancez-A special node representing a class instance.c               C   s   t jƒ S )N)ÚobjectmodelZInstanceModelr   r   r   r   Ú<lambda>  s    zInstance.<lambda>c             C   s   d| j jƒ j| j jt| ƒf S )Nz<Instance of %s.%s at 0x%s>)r)   Úrootr   Úid)r*   r   r   r   Ú__repr__  s    
zInstance.__repr__c             C   s   d| j jƒ j| j jf S )NzInstance of %s.%s)r)   rb   r   )r*   r   r   r   Ú__str__  s    zInstance.__str__c             C   s0   y| j jddd� dS  tjk
r*   dS X d S )Nr[   F)rG   T)r)   r,   r   rJ   )r*   r   r   r   r?     s
    zInstance.callablec             C   s
   | j jƒ S )N)r)   Úqname)r*   r   r   r   Úpytype  s    zInstance.pytypec             C   s   dS )NzInstance ofr   )r*   r   r   r   rF   !  s    zInstance.display_typec             C   s|   t jƒ }t jg d�|_| |_yt| t|ƒ}W nJ tjtj	fk
rv   yt| d|ƒ}W n tj	tjfk
rp   dS X Y nX |S )aV  Infer the truth value for an Instance

        The truth value of an instance is determined by these conditions:

           * if it implements __bool__ on Python 3 or __nonzero__
             on Python 2, then its bool value will be determined by
             calling this special method and checking its result.
           * when this method is not defined, __len__() is called, if it
             is defined, and the object is considered true if its result is
             nonzero. If a class defines neither __len__() nor __bool__(),
             all its instances are considered true.
        )rY   Ú__len__T)
r6   r7   ZCallContextZcallcontextZ	boundnoderD   ÚBOOL_SPECIAL_METHODr   r8   rJ   )r*   r   Úresultr   r   r   r@   $  s    zInstance.bool_valueNc             C   s   d S )Nr   )r*   Úindexr   r   r   r   Úgetitem@  s    zInstance.getitem)N)r   r0   r1   r2   r   Úlazy_descriptorrK   rd   re   r?   rg   rF   r@   rl   r   r   r   r   r_     s   r_   c               @   sZ   e Zd ZdZejdd„ ƒZdd„ Zdd„ Zdd	„ Z	ddd„Z
ddd„Zdd„ Zdd„ Zd
S )rW   z=a special node representing a method not bound to an instancec               C   s   t jƒ S )N)r`   ZUnboundMethodModelr   r   r   r   ra   H  s    zUnboundMethod.<lambda>c             C   s,   | j jjƒ }d| jj| j j|jƒ t| ƒf S )Nz<%s %s of %s at 0x%s)r)   Úparentr4   r   r   r   rf   rc   )r*   r4   r   r   r   rd   J  s    zUnboundMethod.__repr__c             C   s   dS )Nr   r   )r*   r   r   r   Úimplicit_parametersS  s    z!UnboundMethod.implicit_parametersc             C   s   dS )NFr   )r*   r   r   r   Úis_boundV  s    zUnboundMethod.is_boundNc             C   s&   || j kr| j j|ƒgS | jj||ƒS )N)rK   r!   r)   r,   )r*   r   r   r   r   r   r,   Y  s    
zUnboundMethod.getattrc             C   s*   || j krt| j j|ƒfƒS | jj||ƒS )N)rK   Úiterr!   r)   r=   )r*   r   r   r   r   r   r=   ^  s    
zUnboundMethod.igetattrc             C   sp   | j jdkrb| j jjƒ jƒ dt krb|jrP|jj|jd ƒ}|jd j	|d�}ng }dd„ |D ƒS | j j
||ƒS )aÏ  
        The boundnode of the regular context with a function called
        on ``object.__new__`` will be of type ``object``,
        which is incorrect for the argument in general.
        If no context is given the ``object.__new__`` call argument will
        correctly inferred except when inside a call that requires
        the additional context (such as a classmethod) of the boundnode
        to determine which class the method was called from
        Ú__new__z	%s.objectr   )r   c             s   s$   | ]}|t jk	rt|ƒn|V  qd S )N)r   r   r_   )r   Úxr   r   r   r   y  s    z2UnboundMethod.infer_call_result.<locals>.<genexpr>)r)   r   rn   r4   rf   r"   rY   Zextra_contextÚgetr/   r;   )r*   r\   r   Znode_contextr/   r   r   r   r;   c  s    zUnboundMethod.infer_call_resultc             C   s   dS )NTr   )r*   r   r   r   r@   |  s    zUnboundMethod.bool_value)N)N)r   r0   r1   r2   r   rm   rK   rd   ro   rp   r,   r=   r;   r@   r   r   r   r   rW   D  s   	

rW   c                   sX   e Zd ZdZejdd„ ƒZdd„ Zdd„ Zdd	„ Z	d
d„ Z
d‡ fdd„	Zdd„ Z‡  ZS )rX   z9a special node representing a method bound to an instancec               C   s   t jƒ S )N)r`   ZBoundMethodModelr   r   r   r   ra   „  s    zBoundMethod.<lambda>c             C   s   t j| |ƒ || _d S )N)rW   r+   Úbound)r*   Úproxyru   r   r   r   r+   †  s    zBoundMethod.__init__c             C   s   dS )Nr   r   )r*   r   r   r   ro   Š  s    zBoundMethod.implicit_parametersc             C   s   dS )NTr   )r*   r   r   r   rp   �  s    zBoundMethod.is_boundc                s   ddl m} t|jd jˆ d�ƒ}|jjdkr2dS |jdt ƒsDdS t|jd jˆ d�ƒ}|jjdkrjdS t	|j
tƒszdS t|jd	 jˆ d�ƒ}|jjd
kr dS ‡ fdd„|jD ƒ}tdd„ |D ƒƒrÊdS t|jd jˆ d�ƒ}|jjdkrðdS tjtƒ}	x`|jD ]V\}
}t|
jˆ d�ƒ}
t|jˆ d�ƒ}|
jjdk�rt	|
j
tƒ�r|	|
j
 j|ƒ �qW |j|j
|j|j|d�}|jƒ }|j|j|gg d|g d� |	|_|S )zÿTry to infer what type.__new__(mcs, name, bases, attrs) returns.

        In order for such call to be valid, the metaclass needs to be
        a subtype of ``type``, the name needs to be a string, the bases
        needs to be a tuple of classes
        r   )Únode_classes)r   r   Nz%s.typer   ZConsté   ÚTuplec                s   g | ]}t |jˆ d �ƒ‘qS ))r   )r<   r/   )r   Úelt)r   r   r   ú
<listcomp>°  s    z4BoundMethod._infer_type_new_call.<locals>.<listcomp>c             s   s   | ]}|j jd kV  qdS )r   N)r   r   )r   Úbaser   r   r   r   ±  s    z3BoundMethod._infer_type_new_call.<locals>.<genexpr>é   ÚDict)r   ÚlinenoÚ
col_offsetrn   T)r    Úbodyr   ZnewstyleÚ	metaclassÚkeywords)Úastroidrw   r<   rY   r/   r   r   Zis_subtype_ofr"   rV   rC   ÚstrÚeltsr   ÚcollectionsÚdefaultdictÚlistÚitemsÚappendr   r€   ÚPassZpostinitÚlocals)r*   r\   r   rw   Zmcsr   r    Zinferred_basesrU   Z
cls_localsÚkeyrC   ÚclsÚemptyr   )r   r   Ú_infer_type_new_call�  sR    
z BoundMethod._infer_type_new_callNc                sl   t j|| jƒ}| jjjdkrZ| jjdkrZ| jdkrZt|jƒdkrZ| j||ƒ}|rZt	|fƒS t
t| ƒj||ƒS )Nr   Útyperr   é   )r6   r]   ru   r   r   r   ÚlenrY   r‘   rq   ÚsuperrX   r;   )r*   r\   r   Znew_cls)r   r   r   r;   Õ  s    

zBoundMethod.infer_call_resultc             C   s   dS )NTr   )r*   r   r   r   r@   ä  s    zBoundMethod.bool_value)N)r   r0   r1   r2   r   rm   rK   r+   ro   rp   r‘   r;   r@   Ú__classcell__r   r   )r   r   rX   €  s   ErX   c               @   sX   e Zd ZdZejdd„ ƒZddd„Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )Ú	Generatorzea special node representing a generator.

    Proxied class is set once for all in raw_building.
    c               C   s   t jƒ S )N)r`   ZGeneratorModelr   r   r   r   ra   ï  s    zGenerator.<lambda>Nc             C   s
   || _ d S )N)rn   )r*   rn   r   r   r   r+   ò  s    zGenerator.__init__c             C   s   dS )NFr   )r*   r   r   r   r?   õ  s    zGenerator.callablec             C   s   dt  S )Nz%s.generator)r"   )r*   r   r   r   rg   ø  s    zGenerator.pytypec             C   s   dS )Nr—   r   )r*   r   r   r   rF   û  s    zGenerator.display_typec             C   s   dS )NTr   )r*   r   r   r   r@   þ  s    zGenerator.bool_valuec             C   s   d| j j| jt| ƒf S )Nz<Generator(%s) l.%s at 0x%s>)r)   r   r   rc   )r*   r   r   r   rd     s    zGenerator.__repr__c             C   s   d| j j S )NzGenerator(%s))r)   r   )r*   r   r   r   re     s    zGenerator.__str__)N)r   r0   r1   r2   r   rm   rK   r+   r?   rg   rF   r@   rd   re   r   r   r   r   r—   è  s   
r—   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚAsyncGeneratorz,Special node representing an async generatorc             C   s   dt  S )Nz%s.async_generator)r"   )r*   r   r   r   rg     s    zAsyncGenerator.pytypec             C   s   dS )Nr˜   r   )r*   r   r   r   rF     s    zAsyncGenerator.display_typec             C   s   d| j j| jt| ƒf S )Nz!<AsyncGenerator(%s) l.%s at 0x%s>)r)   r   r   rc   )r*   r   r   r   rd     s    zAsyncGenerator.__repr__c             C   s   d| j j S )NzAsyncGenerator(%s))r)   r   )r*   r   r   r   re     s    zAsyncGenerator.__str__N)r   r0   r1   r2   rg   rF   rd   re   r   r   r   r   r˜     s
   r˜   )N)r2   r   r‡   r„   r   r6   r   r   Zlazy_importr`   r   r   r"   r   ZAstroidManagerZMANAGERri   r   r   r'   r(   r:   rD   rE   r_   rW   rX   r—   r˜   r   r   r   r   Ú<module>   sB   




"^=<h$