3
tûœ]¨A  ã               @   sj  d Z ddl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 ddlm	Z	 ddlm
Z
 ejƒ Zee	jƒZeeƒZedƒZeeƒZedƒZdd	„ Zd
d„ Zdd„ Zeƒ Zefdd„Zdd„ Zee
j_dd„ Z dd„ Z!d2dd„Z"f dfdd„Z#d3dd„Z$dd„ Z%d4dd„Z&d d!„ Z'd"d#„ Z(d$d%„ Z)d&d'„ Z*d5d(d)„Z+d*d+„ Z,G d,d-„ d-ƒZ-i Z.d.d/„ Z/d0d1„ Z0e0ƒ  dS )6z’this module contains a set of functions to create astroid trees from scratch
(build_* functions) or from living object (object_build_* functions)
é    N)Úbases)Úmanager)Únode_classes)Únodes.c             C   s.   t | dd ƒ}|o,tj|ƒo,|jdko,| jdkS )NÚ__self__Ú_ioÚio)ÚgetattrÚinspectÚismoduleÚ__name__Ú
__module__)ÚmemberZmember_self© r   ú7/tmp/pip-build-fn42xldr/astroid/astroid/raw_building.pyÚ_io_discrepancy*   s
    

r   c             C   s   ||_ | j|ƒ d S )N)ÚnameÚadd_local_node)ÚparentÚnoder   r   r   r   Ú_attach_local_node5   s    r   c             C   sH   |j }t|ddƒ}|sdS dd„ |jD ƒ}t|||jƒ}|g| jd< dS )zFAdd a __class__ member to the given func node, if we can determine it.r   Nc             S   s   g | ]
}|j ‘qS r   )r   )Ú.0Zancestorr   r   r   ú
<listcomp>@   s    z%_add_dunder_class.<locals>.<listcomp>Ú	__class__)r   r	   Ú	__bases__Úbuild_classÚ__doc__Úinstance_attrs)Úfuncr   Z
python_clsZcls_nameZ	cls_basesZ	ast_klassr   r   r   Ú_add_dunder_class:   s    r   c             C   s   t jƒ }||_t| ||ƒ dS )zdcreate a dummy node and register it in the locals of the given
    node with the specified name
    N)r   Ú	EmptyNodeÚobjectr   )r   r   Zruntime_objectZenoder   r   r   Úattach_dummy_nodeH   s    r"   c             C   s   | j d k	o| j tk	S )N)r!   Ú_marker)Úselfr   r   r   Ú_has_underlying_objectQ   s    r%   c             C   s    || j krt| tj|ƒ|ƒ dS )zdcreate a Const node and register it in the locals of the given
    node with the specified name
    N)Zspecial_attributesr   r   Úconst_factory)r   r   Úvaluer   r   r   Úattach_const_nodeX   s    
r(   c             C   s"   t j||dfgƒ}t| ||ƒ dS )zicreate a ImportFrom node and register it in the locals of the given
    node with the specified name
    N)r   Ú
ImportFromr   )r   ÚmodnameZ
membernameZ	from_noder   r   r   Úattach_import_node`   s    r+   c             C   s    t j| |dd�}d|_d|_|S )z,create and initialize an astroid Module nodeF)Zpure_pythonN)r   ÚModuleÚpackager   )r   Údocr   r   r   r   Úbuild_moduleh   s    r/   c             C   s>   t j| |ƒ}x,|D ]$}t jƒ }||_|jj|ƒ ||_qW |S )z.create and initialize an astroid ClassDef node)r   ÚClassDefÚNamer   r   Úappendr   )r   Ú	basenamesr.   r   ÚbaseZbasenoder   r   r   r   p   s    

r   c       	      C   s  |pg |pg |pg   }}}t j| |ƒ}t jƒ  |_}g |_g |_x4|D ],}|jjt jƒ ƒ ||jd _||jd _qHW x4|D ],}|jjt jƒ ƒ ||jd _||jd _q~W g |_	x*|D ]"}|j	jt j
|ƒƒ ||j	d _qºW d|_d|_||_|�r t|ƒ |S )z1create and initialize an astroid FunctionDef nodeé   Néÿÿÿÿr6   r6   r6   r6   )r   ÚFunctionDefÚ	ArgumentsÚargsÚposonlyargsr2   r1   r   r   Údefaultsr&   ÚkwargÚvarargÚregister_arguments)	r   r9   r:   r;   r.   r   ZargsnodeÚargÚdefaultr   r   r   Úbuild_function{   s.    


rA   c             C   s   t j| dd„ |D ƒƒS )z<create and initialize an astroid ImportFrom import statementc             S   s   g | ]}|d f‘qS )Nr   )r   r   r   r   r   r   ™   s    z%build_from_import.<locals>.<listcomp>)r   r)   )ÚfromnameÚnamesr   r   r   Úbuild_from_import—   s    rD   c             C   s~   |dkrD| j j }| j jr*| j| j j| j ƒ | j jrD| j| j j| j ƒ x4|D ],}t|tjƒrj| j|j|ƒ qJt| |j	ƒ qJW dS )z{add given arguments to local

    args is a list that may contains nested lists
    (i.e. def func(a, (b, c, d)): ...)
    N)
r9   r=   Z	set_localr<   Ú
isinstancer   r1   r   r>   Úelts)r   r9   r?   r   r   r   r>   œ   s    
r>   c             C   s    dd„ |j D ƒ}t| |||d�S )z(create astroid for a living class objectc             S   s   g | ]
}|j ‘qS r   )r   )r   r4   r   r   r   r   ±   s    z&object_build_class.<locals>.<listcomp>)Ú	localname)r   Ú_base_class_object_build)r   r   rG   r3   r   r   r   Úobject_build_class¯   s    rI   c       
      C   sÚ   t j|ƒ}g }g }g }x”|jjƒ D ]†\}}|jt jjkrD|j|ƒ nL|jt jjkr^|j|ƒ n2|jt jj	krx|j|ƒ n|jt jj
kr�|j|ƒ |jt jk	r"|j|jƒ q"W tt|ddƒp¼|||||jƒ}	| j|	|ƒ dS )z+create astroid for a living function objectr   N)r
   Ú	signatureÚ
parametersÚitemsÚkindÚ	ParameterÚPOSITIONAL_ONLYr2   ÚPOSITIONAL_OR_KEYWORDÚVAR_POSITIONALÚVAR_KEYWORDr@   Ú_emptyrA   r	   r   r   )
r   r   rG   rJ   r9   r;   r:   Ú
param_nameÚparamr   r   r   r   Úobject_build_functionµ   s,    

rV   c             C   s   t | |g |ƒS )z2create astroid for a living data descriptor object)rH   )r   r   r   r   r   r   Úobject_build_datadescriptorÐ   s    rW   c             C   s<   t t|ddƒp||jd�}d|j_| j||ƒ t||ƒ dS )z4create astroid for a living method descriptor objectr   N)r.   )rA   r	   r   r9   r   r   )r   r   rG   r   r   r   r   Úobject_build_methoddescriptorÕ   s
    rX   c       
      C   s¨   t |pt|ddƒp|||jƒ}t|tƒ|_| j||ƒ yt|tƒrL|ƒ j	}nt
‚W n t
k
rf   Y n>X x:|jƒ D ].\}}tjƒ }	||	_||	_d|	_|	g|j|< qrW |S )zbcreate astroid for a living class object, with a given set of base names
    (e.g. ancestors)
    r   Nr5   )r   r	   r   rE   ÚtypeZ	_newstyler   Ú
issubclassÚ	ExceptionÚ__dict__Ú	TypeErrorrL   r   r    r!   r   Úlinenor   )
r   r   r3   r   rG   ÚklassZinstdictZ	item_nameÚobjZvalnoder   r   r   rH   â   s&    

rH   c             C   s‚   y
|j }W n tk
r"   d }Y nX t|dd ƒ}|d krTt|tƒsFt‚t| ||ƒ n*|t|dd ƒkrrt| ||ƒ nt| ||ƒ d S )NÚco_filenameÚ__file__)	Ú__code__ÚAttributeErrorr	   rE   r!   ÚAssertionErrorrX   r"   rV   )r   r   r   ÚmoduleÚcodeÚfilenamer   r   r   Ú_build_from_function  s    

ri   c               @   s2   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ ZdS )ÚInspectBuilderz½class for building nodes from living object

    this is actually a really minimal representation, including only Module,
    FunctionDef and ClassDef nodes and some others as guessed.
    c             C   s   i | _ d | _d S )N)Ú_doneÚ_module)r$   r   r   r   Ú__init__  s    zInspectBuilder.__init__Nc             C   s�   || _ |dkr|j}yt||jƒ}W n tk
r@   t|ƒ}Y nX |rRtjj|ƒn| |_|_||_	t
j|ƒ t|dƒ|_i | _| j||ƒ |S )z×build astroid from a living module (i.e. using inspect)
        this is used when there is no python source code available (either
        because it's a built-in module or because the .py is not available)
        NÚ__path__)rl   r   r/   r   rd   ÚosÚpathÚabspathÚfiler   ÚMANAGERÚcache_moduleÚhasattrr-   rk   Úobject_build)r$   rf   r*   rp   r   r   r   r   Úinspect_build  s    
zInspectBuilder.inspect_buildc             C   sð  || j kr| j | S || j |< �xÊt|ƒD �]¼}yt||ƒ}W n  tk
r^   t||ƒ w*Y nX tj|ƒrp|j}tj|ƒrŒt	|||| j
ƒ q*tj|ƒr¾t|ƒ r°| j|||ƒr°q*t|||ƒ q*tj|ƒ�rP| j|||ƒrÚq*|| j k�r| j | }||jj|f ƒk�r*|j||ƒ nt|||ƒ}| j||ƒ |dk�rè|jdk�rè| j | j
 |_q*tj|ƒ�rzt|tƒ�slt‚t|||ƒ q*tj|ƒ�r¤t|tƒ�s–t‚t|||ƒ q*t|tƒ�r¾t|||ƒ q*tj|ƒ�rÜt	|||| j
ƒ q*t|||ƒ q*W dS )z~recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        r   N)rk   Údirr	   rd   r"   r
   ÚismethodÚ__func__Ú
isfunctionri   rl   Ú	isbuiltinr   Úimported_memberrX   ÚisclassÚlocalsÚgetr   rI   rv   r   ÚismethoddescriptorrE   r!   re   ÚisdatadescriptorrW   Ú
_CONSTANTSr(   Ú	isroutine)r$   r   r`   r   r   Z
class_noder   r   r   rv   4  sR    








zInspectBuilder.object_buildc             C   s¾   yt |ddƒ}W n tk
r(   d}Y nX |dkrR|d
krBtj}nt|||ƒ dS dddœj||ƒ}|| jjkrºyt tj| |ƒ W n$ t	t
fk
r¨   t|||ƒ Y nX t|||ƒ dS d	S )z1verify this is not an imported class or handle itr   NÚ__new__Ú__subclasshook__TZgtk_gtkr   )Zgtkr   F)r…   r†   )r	   r]   Úbuiltinsr   r"   r€   rl   ÚsysÚmodulesÚKeyErrorrd   r+   )r$   r   r   r   r*   Z	real_namer   r   r   r}   j  s$    
zInspectBuilder.imported_member)NN)r   r   Ú__qualname__r   rm   rw   rv   r}   r   r   r   r   rj     s
   
6rj   c             C   s   t | jj S )N)Ú_CONST_PROXYr'   r   )Úconstr   r   r   Ú_set_proxied‘  s    rŽ   c        	   
   C   s”  t ƒ } | jtƒ}xŽtjjƒ D ]€\}}|tkr<tdƒ}||_n@|t	krTtdƒ}||_n(|t
krltdƒ}||_n|j|jƒd }|ttttfkr”||_q|t|< qW ttƒtj_tjtjjtjjƒ}||_|tj_| jtjjtjƒ ttdƒ�r tjtjjtjjƒ}||_|tj _| jtj jtjƒ tj!tjtj"tt	tj#tj$tj%tj&tj'f
}xF|D ]>}|j|k�rNtj|j|jƒ}||_| j||ƒ |||j< �qNW dS )z)astroid bootstrapping the builtins moduleZNoneTypeZNotImplementedTypeÚEllipsisr   ÚAsyncGeneratorTypeN)(rj   rw   r‡   r   Ú	CONST_CLSrL   Ú	TYPE_NONEr   r   ÚTYPE_NOTIMPLEMENTEDÚTYPE_ELLIPSISr	   r   ÚdictÚlistÚsetÚtupleZ_proxiedrŒ   ÚpropertyrŽ   r   ZConstr0   ÚtypesÚGeneratorTyper   r   Ú	Generatorrv   ru   r�   ÚAsyncGeneratorÚGetSetDescriptorTypeÚMemberDescriptorTypeÚFunctionTypeÚ
MethodTypeÚBuiltinFunctionTypeÚ
ModuleTypeÚTracebackType)	ZbuilderZastroid_builtinÚclsZnode_clsÚproxyZ_GeneratorTypeZ_AsyncGeneratorTypeZbuiltin_typesÚ_typer   r   r   Ú_astroid_bootstrapping•  sX    

r¨   )N)NNNN)N)NN)1r   r‡   r
   ro   rˆ   rš   Zastroidr   r   r   r   ZAstroidManagerrs   r˜   r‘   rƒ   ÚvarsZ	_BUILTINSrY   r’   ÚNotImplementedr“   r”   r   r   r   r!   r#   r"   r%   r    Zhas_underlying_objectr(   r+   r/   r   rA   rD   r>   rI   rV   rW   rX   rH   ri   rj   rŒ   rŽ   r¨   r   r   r   r   Ú<module>   sP   
	



 z?