o
    b>                     @   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$d2d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 Z/e/  dS )6zthis 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inspectismodule__name__
__module__)memberZmember_self r   S/var/www/secure340b-portal/env/lib/python3.10/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      g | ]}|j qS r   r   ).0Zancestorr   r   r   
<listcomp>?       z%_add_dunder_class.<locals>.<listcomp>	__class__)r   r	   	__bases__build_class__doc__instance_attrs)funcr   Z
python_cls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 uo	| j tuS r   )r&   _markerselfr   r   r   _has_underlying_objectP   s   r+   c                 C   s$   || j vrt| t|| dS 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   r1   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_moduleg   s   r5   r   c                 C   s:   t | |}|D ]}t  }||_|j| ||_q|S )z.create and initialize an astroid ClassDef node)r   ClassDefNamer   r   appendr   )r   	basenamesr4   r   baseZbasenoder   r   r   r   o   s   r   c                 C   s   |pg |pg }}t | |}t   |_}g |_|D ]}|jt   ||jd _||jd _qg |_|D ]}|jt 	| ||jd _q7d|_
d|_||_|rXt| |S )z1create and initialize an astroid FunctionDef nodeN)r   FunctionDef	Argumentsargsr8   r7   r   r   defaultsr,   kwargvarargregister_arguments)r   r>   r?   r4   r"   Zargsnodeargdefaultr   r   r   build_functionz   s$   rE   c                 C   s   t | dd |D S )z<create and initialize an astroid ImportFrom import statementc                 S   s   g | ]}|d fqS r   r   )r   r   r   r   r   r      s    z%build_from_import.<locals>.<listcomp>)r   r/   )fromnamenamesr   r   r   build_from_import   s   rH   c                 C   sz   |du r"| j j }| j jr| | j j| j  | j jr"| | j j| j  |D ]}t|tjr4| |j| q$t| |j	 q$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)
r>   rA   Z	set_localr@   
isinstancer   r7   r   rB   elts)r"   r>   rC   r   r   r   rB      s   rB   c                 C   s    dd |j D }t| |||dS )z(create astroid for a living class objectc                 S   r   r   r   )r   r:   r   r   r   r      r   z&object_build_class.<locals>.<listcomp>)	localname)r   _base_class_object_build)r   r   rK   r9   r   r   r   object_build_class   s   rM   c                 C   sb   t |\}}}}|dur|| |dur|| tt|ddp#||||j}| || dS )z+create astroid for a living function objectNr   )r
   
getargspecr8   rE   r	   r    r   )r   r   rK   r>   varargsvarkwr?   r"   r   r   r   object_build_function   s   

rQ   c                 C   s   t | |g |S )z2create astroid for a living data descriptor object)rL   )r   r   r   r   r   r   object_build_datadescriptor   s   rR   c                 C   s<   t t|ddp||jd}d|j_| || t|| dS )z4create astroid for a living method descriptor objectr   N)r4   )rE   r	   r    r>   r   r$   )r   r   rK   r"   r   r   r   object_build_methoddescriptor   s   rS   c           
      C   s   t |p
t|ddp
|||j}t|t|_| || zt|tr&| j	}nt
W n   Y |S | D ]\}}t }	||	_||	_d|	_|	g|j|< q4|S )zbcreate astroid for a living class object, with a given set of base names
    (e.g. ancestors)
    r   N   )r   r	   r    rI   typeZ	_newstyler   
issubclass	Exception__dict__	TypeErroritemsr   r%   r&   r   linenor!   )
r   r   r9   r   rK   klassZinstdictZ	item_nameobjZvalnoder   r   r   rL      s,   

rL   c                 C   s   z|j }W n ty   d }Y nw t|dd }|d u r*t|ts"J t| || d S |t|dd kr:t| || d S t| || d S )Nco_filename__file__)__code__AttributeErrorr	   rI   r&   rS   r'   rQ   )r   r   r   modulecodefilenamer   r   r   _build_from_function   s   
re   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 r   )_done_moduler)   r   r   r   __init__  s   
zInspectBuilder.__init__Nc                 C   s   || _ |du r
|j}zt||j}W n ty   t|}Y nw |r(t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__)rh   r   r5   r    ra   ospathabspathfiler   MANAGERcache_modulehasattrr3   rg   object_build)r*   rb   r0   rl   r   r   r   r   inspect_build  s   
zInspectBuilder.inspect_buildc              	   C   s  || j v r
| j | S || j |< t|D ]}zt||}W n ty*   t|| Y qw t|r3|j}t|rAt	|||| j
 qt|rYt|sR| |||rRqt||| qt|r| |||rfq|| j v r| j | }||j|dvr||| nt|||}| || |dkr|jdu r| j | j
 |_qt|rt|tsJ t||| qt|rt|tsJ t||| qt|trt||| qt|rt	|||| j
 qt||| qdS )z~recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        r   r   N)rg   dirr	   ra   r'   r
   ismethod__func__
isfunctionre   rh   	isbuiltinr   imported_memberrS   isclasslocalsgetr   rM   rr   r   ismethoddescriptorrI   r&   isdatadescriptorrR   
_CONSTANTSr.   	isroutine)r*   r   r]   r   r   Z
class_noder   r   r   rr   !  sZ   













zInspectBuilder.object_buildc              	   C   s   zt |dd}W n   td d}Y |du r(|dv r tj}nt||| dS ddd||}|| jjkr\z
t tj	| | W n t
tfyS   t||| Y dS w 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	   _LOG	exceptionbuiltinsr   r'   r|   rh   sysmodulesKeyErrorra   r1   )r*   r   r   r   r0   Z	real_namer   r   r   ry   W  s.   zInspectBuilder.imported_memberNN)r   r   __qualname__r    ri   rs   rr   ry   r   r   r   r   rf     s    
6rf   c                 C   s   t | jj S r   )_CONST_PROXYr-   r   )constr   r   r   _set_proxied  s   r   c            	   
   C   s~  t  } | t}tj D ]8\}}|tdu rtd}||_n|tt	u r-td}||_n|
|jd }|ttttfv rA||_q|t|< qtttj_ttjjtjj}||_|tj_| tjjtj ttdrt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
}|D ]}|j|vrt|j|j}||_| || |||j< qdS )z)astroid bootstrapping the builtins moduleNNoneTypeNotImplementedTyper   AsyncGeneratorType)'rf   rs   r   r   	CONST_CLSrZ   rU   r   r   NotImplementedr	   r   dictlistsettupleZ_proxiedr   propertyr   r   ZConstr6   typesGeneratorTyper    r   	Generatorrr   rq   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  s\   




r   r   )r   N)NNNr   )0r    r   r
   loggingrk   r   r   Zastroidr   r   r   r   ZAstroidManagerro   r   r   r   varsZ	_BUILTINS	getLoggerr   r   r   r   r$   r&   r(   r'   r+   r%   Zhas_underlying_objectr.   r1   r5   r   rE   rH   rB   rM   rQ   rR   rS   rL   re   rf   r   r   r   r   r   r   r   <module>   sP   

	




 }
<