B
    S\>                 @   s`  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ZddlmZ ddlm	Z	 ddlm
Z
 ddlmZ e	 Zee
jZeeZee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!d3ddZ"d4ddZ#dd Z$d5ddZ%d d! Z&d"d# Z'd$d% Z(d&d' Z)d6d(d)Z*d*d+ Z+G d,d- d-Z,i Z-d.d/ Z.d0d1 Z/e/  dS )7zthis 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)nodesc             C   s.   t | dd }|o,t|o,|jdko,| jdkS )N__self___ioio)getattrinspectZismodule__name__
__module__)memberZmember_self r   3lib/python3.7/site-packages/astroid/raw_building.py_io_discrepancy)   s
    

r   c             C   s   ||_ | | d S )N)nameadd_local_node)parentnoder   r   r   r   _attach_local_node4   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_class9   s    r   c             C   s   t  }||_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_nodeG   s    r!   c             C   s   | j d k	o| j tk	S )N)r    _marker)selfr   r   r   _has_underlying_objectP   s    r$   c             C   s    || j krt| t|| 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_nodeW   s    
r'   c             C   s"   t ||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   ZModulepackager   )r   docr   r   r   r   build_moduleg   s    r-   r   c             C   s>   t | |}x,|D ]$}t  }||_|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   o   s    

r   c             C   s   |pg |pg  }}t | |}t   |_}g |_x4|D ],}|jt   ||jd _||jd _q8W g |_x*|D ]"}|jt 	| ||jd _qtW d|_
d|_||_|rt| |S )z1create and initialize an astroid FunctionDef nodeN)r   ZFunctionDefZ	Argumentsargsr0   r/   r   r   defaultsr%   kwargvarargregister_arguments)r   r4   r5   r,   r   Zargsnodeargdefaultr   r   r   build_functionz   s$    

r;   c             C   s   t | 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(   )Zfromnamenamesr   r   r   build_from_import   s    r=   c             C   s~   |dkrD| j j }| j jr*| | j j| j  | j jrD| | j j| j  x4|D ],}t|tjrj| |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)
r4   r7   Z	set_localr6   
isinstancer   r/   r   r8   Zelts)r   r4   r9   r   r   r   r8      s    
r8   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   r2   r   r   r   r      s    z&object_build_class.<locals>.<listcomp>)	localname)r   _base_class_object_build)r   r   r?   r1   r   r   r   object_build_class   s    rA   c             C   sb   t |\}}}}|dk	r$|| |dk	r6|| tt|ddpF||||j}| || dS )z+create astroid for a living function objectNr   )r
   Z
getargspecr0   r;   r	   r   r   )r   r   r?   r4   ZvarargsZvarkwr5   r   r   r   r   object_build_function   s    

rB   c             C   s   t | |g |S )z2create astroid for a living data descriptor object)r@   )r   r   r   r   r   r   object_build_datadescriptor   s    rC   c             C   s<   t t|ddp||jd}d|j_| || t|| dS )z4create astroid for a living method descriptor objectr   N)r,   )r;   r	   r   r4   r   r   )r   r   r?   r   r   r   r   object_build_methoddescriptor   s
    rD   c       
      C   s   t |pt|ddp|||j}t|t|_| || yt|trL| j	}nt
W n   Y n>X x:| D ].\}}t }	||	_||	_d|	_|	g|j|< qjW |S )zbcreate astroid for a living class object, with a given set of base names
    (e.g. ancestors)
    r   N   )r   r	   r   r>   typeZ	_newstyler   
issubclass	Exception__dict__	TypeErroritemsr   r   r    r   linenor   )
r   r   r1   r   r?   klassZinstdictZ	item_nameobjZvalnoder   r   r   r@      s&    

r@   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	   r>   r    AssertionErrorrD   r!   rB   )r   r   r   modulecodefilenamer   r   r   _build_from_function   s    

rW   c               @   s2   e Zd ZdZdd ZdddZdd Zd	d
 ZdS )InspectBuilderzclass 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|n| |_|_||_	t
| t|d|_i | _| || |S )zbuild 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__)rZ   r   r-   r   rR   ospathabspathfiler   MANAGERcache_modulehasattrr+   rY   object_build)r#   rT   r)   r^   r   r   r   r   inspect_build  s    
zInspectBuilder.inspect_buildc          	   C   s  || j kr| j | S || j |< xt|D ]}yt||}W n  tk
r^   t|| w*Y nX t|rp|j}t|rt	|||| j
 q*t|rt|s| |||rq*t||| q*t|rN| |||rq*|| j kr| j | }||j|dkr(||| nt|||}| || |dkr|jdkr| j | j
 |_q*t|rxt|tsjtt||| q*t|rt|tstt||| q*t|trt||| q*t|rt	|||| 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   r   N)rY   dirr	   rR   r!   r
   Zismethod__func__Z
isfunctionrW   rZ   Z	isbuiltinr   imported_memberrD   Zisclasslocalsgetr   rA   rd   r   Zismethoddescriptorr>   r    rS   ZisdatadescriptorrC   
_CONSTANTSr'   Z	isroutine)r#   r   rN   r   r   Z
class_noder   r   r   rd   !  sR    








zInspectBuilder.object_buildc          	   C   s   yt |dd}W n   td d}Y nX |dkrT|dkrDtj}nt||| dS ddd||}|| jjkry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   Nz:unexpected error while building astroid from living object)__new____subclasshook__TZgtk_gtkr   )Zgtkr   F)r	   _LOGZ	exceptionbuiltinsr   r!   rj   rZ   sysmodulesKeyErrorrR   r*   )r#   r   r   r   r)   Z	real_namer   r   r   rh   W  s(    
zInspectBuilder.imported_member)NN)r   r   __qualname__r   r[   re   rd   rh   r   r   r   r   rX     s
   
6rX   c             C   s   t | jj S )N)_CONST_PROXYr&   r   )Zconstr   r   r   _set_proxied  s    ru   c        	   
   C   s  t  } | t}x~tj D ]p\}}|tdkr@td}||_n,|tt	kr\td}||_n|
|jd }|ttttfkr||_q|t|< qW tttj_ttjjtjj}||_|tj_| tjjtj ttdrttjjtjj}||_|tj_| tjjtj tj tjtj!tdtt	tj"tj#tj$tj%tj&f
}xF|D ]>}|j|krFt|j|j}||_| || |||j< qFW dS )z)astroid bootstrapping the builtins moduleNZNoneTypeZNotImplementedTyper   AsyncGeneratorType)'rX   re   ro   r   	CONST_CLSrK   rF   r   r   NotImplementedr	   r   dictlistsettupleZ_proxiedrt   propertyru   r   ZConstr.   typesGeneratorTyper   r   	Generatorrd   rc   rv   AsyncGeneratorGetSetDescriptorTypeMemberDescriptorTypeFunctionType
MethodTypeBuiltinFunctionType
ModuleTypeTracebackType)	ZbuilderZastroid_builtinclsZnode_clsproxyZ_GeneratorTypeZ_AsyncGeneratorTypeZbuiltin_typesZ_typer   r   r   _astroid_bootstrapping  sR    

r   )N)r   N)NNN)N)NN)0r   ro   r
   Zloggingr]   rp   r~   Zastroidr   r   r   r   ZAstroidManagerra   r|   rw   rk   varsZ	_BUILTINSZ	getLoggerr   rn   r   r   r   r    r"   r!   r$   r   Zhas_underlying_objectr'   r*   r-   r   r;   r=   r8   rA   rB   rC   rD   r@   rW   rX   rt   ru   r   r   r   r   r   <module>   sN   

	




 }<