o
    ˷ef                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ e Zd	d
 Zeedej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dS )a  
A module to deal with stuff like `list.append` and `set.add`.

Array modifications
*******************

If the content of an array (``set``/``list``) is requested somewhere, the
current module will be checked for appearances of ``arr.append``,
``arr.insert``, etc.  If the ``arr`` name points to an actual array, the
content will be added

This can be really cpu intensive, as you can imagine. Because |jedi| has to
follow **every** ``append`` and check whether it's the right array. However this
works pretty good, because in *slow* cases, the recursion detector and other
settings will stop this process.

It is important to note that:

1. Array modfications work only in the current module.
2. Jedi only checks Array additions; ``list.pop``, etc are ignored.
    )debug)settings)	recursion)ValueSet	NO_VALUESHelperValueMixinValueWrapper)LazyKnownValues)infer_call_of_leaf)inference_state_method_cachec                 C   s   |j dvrtS t| |S )zI Just a mapper function for the internal _internal_check_array_additions )listset)
array_typer   _internal_check_array_additions)contextsequence r   Z/var/www/ideatree/venv/lib/python3.10/site-packages/jedi/inference/value/dynamic_arrays.pycheck_array_additions"   s   

r   )defaultc              
      s  ddl m  tjd| dd |  }tjr| r#tjddd tS  fdd}tj	d	}t_	|j
jd
k}|r<g dnddg}t }|D ]}z	|j | }	W n	 tyY   Y qEw |	D ]{}
| j}|j|
j  k rn|jk spn q\|
j}|j}|j|}z	|j|d  }W n	 ty   Y q\w |jdks|jd dks|jd dkrq\| |
}t| j| }|rt||
dd}||v r||||jd |O }W d   n1 sw   Y  q\qE|t_	tjd|dd |S )zr
    Checks if a `Array` has "add" (append, insert, extend) statements:

    >>> a = [""]
    >>> a.append(1)
    r   	argumentszDynamic array search for %sMAGENTA)colorzDynamic array search aborted.c                    s   t  | j| | }t }|dv r|dd  }|dv r+|D ]	\}}|| q|S |dv r@|D ]\}}|t|  O }q1|S )N)insert   )appendaddr   )extendupdate)r   TreeArgumentsinference_stateunpackr   r   inferiterate)r   arglistadd_nameparamsresultkey
lazy_valuer   r   r   find_additions<   s   z7_internal_check_array_additions.<locals>.find_additionsFr   )r   r   r   r   r   r   trailer()T)cut_own_trailerNzDynamic array result %s)jedi.inferencer   r   dbgget_root_contextr   dynamic_array_additionsis_compiledr    dynamic_params_for_other_modulesnamestring_namer   	tree_nodeget_used_namesKeyError	start_posend_posparentchildrenindex
IndexErrortypecreate_contextr   execution_allowedr!   r
   )r   r   module_contextr+   temp_param_addis_listsearch_namesadded_typesr&   possible_namesr6   
value_noder,   powertrailer_posexecution_trailerrandom_contextallowedfoundr   r   r   r   +   sn   	

#r   c                 C   s(   t | |}ddlm} |t|ggS )z$Used for set() and list() instances.r   r   )_DynamicArrayAdditionsr0   r   ValuesArgumentsr   )instancer   air   r   r   get_dynamic_array_instance~   s   
rU   c                   @   s4   e Zd ZdZdd Zdd ZdddZdd
dZdS )rQ   aj  
    Used for the usage of set() and list().
    This is definitely a hack, but a good one :-)
    It makes it possible to use set/list conversions.

    This is not a proper context, because it doesn't have to be. It's not used
    in the wild, it's just used within typeshed as an argument to `__init__`
    for set/list and never used in any other place.
    c                 C   s   || _ || _d S N)	_instance
_arguments)selfrS   r   r   r   r   __init__   s   
z_DynamicArrayAdditions.__init__c                 C   s   | j jjd\}|S )Ntuple)rW   r!   builtins_modulepy__getattribute__)rY   tuple_r   r   r   py__class__   s   z"_DynamicArrayAdditions.py__class__Nc                 c   sx    | j }z
t| \}}W n	 ty   Y n
w |  E d H  ddlm} t||r:t	|j
| j}|E d H  d S d S )Nr   )r    )rX   nextr"   StopIterationr#   r$   jedi.inference.argumentsr    
isinstancer   r   rW   )rY   contextualized_noder   _r*   r    	additionsr   r   r   
py__iter__   s   
z!_DynamicArrayAdditions.py__iter__Fc                 C   s
   |  |S rV   )rg   )rY   rd   is_asyncr   r   r   r$      s   
z_DynamicArrayAdditions.iteraterV   )NF)__name__
__module____qualname____doc__rZ   r_   rg   r$   r   r   r   r   rQ      s    	
rQ   c                       s,   e Zd Z fddZdd Zdd Z  ZS )_Modificationc                    s   t  | || _|| _d S rV   )superrZ   _assigned_values_contextualized_key)rY   wrapped_valueassigned_valuescontextualized_key	__class__r   r   rZ      s   
z_Modification.__init__c                 O   s   | j j|i || jB S rV   )_wrapped_valuepy__getitem__ro   )rY   argskwargsr   r   r   rw      s   z_Modification.py__getitem__c                 C   s.   dd | j  D }||v r| jS | j|S )Nc                 S   s   g | ]}| tqS r   )get_safe_value	_sentinel).0vr   r   r   
<listcomp>   s    z6_Modification.py__simple_getitem__.<locals>.<listcomp>)rp   r#   ro   rv   py__simple_getitem__)rY   r?   actualr   r   r   r      s   z"_Modification.py__simple_getitem__)ri   rj   rk   rZ   rw   r   __classcell__r   r   rt   r   rm      s    rm   c                   @   s   e Zd ZdddZdd ZdS )DictModificationNc                 c   s     | j |E d H  | jV  d S rV   )rv   rg   rp   rY   rd   r   r   r   rg      s   zDictModification.py__iter__c                 C   s   | j  | j B S rV   )rv   get_key_valuesrp   r#   )rY   r   r   r   r      s   zDictModification.get_key_valuesrV   )ri   rj   rk   rg   r   r   r   r   r   r      s    
r   c                   @   s   e Zd ZdddZdS )ListModificationNc                 c   s$    | j |E d H  t| jV  d S rV   )rv   rg   r	   ro   r   r   r   r   rg      s   zListModification.py__iter__rV   )ri   rj   rk   rg   r   r   r   r   r      s    r   N)rl   jedir   r   r0   r   jedi.inference.base_valuer   r   r   r   jedi.inference.lazy_valuer	   jedi.inference.helpersr
   jedi.inference.cacher   objectr{   r   increase_indentr   rU   rQ   rm   r   r   r   r   r   r   <module>   s$    	Q$	