B
    7r\'                 @   sJ  d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dl	mZ d dl	mZ d dl	m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mZ d dlmZmZmZmZ d dlmZmZm Z  d dl!m"Z" d dl!m#Z# d dlm$Z$ d dl%m&Z& G dd deZ'G dd deZ(G dd deee(Z)G dd deZ*dS )    )tree)use_metaclass)debug)evaluator_method_cacheCachedMetaClass)compiled)	recursion)
docstrings)pep0484)flow_analysis)helpers)AnonymousArguments)ParserTreeFilterFunctionExecutionFilterContextNameAbstractNameDefinition	ParamName)ContextualizedNodeNO_CONTEXTS
ContextSetTreeContext)LazyKnownContextsLazyKnownContextLazyTreeContext)iterable)asynchronous)parser_utils)get_yield_exprsc               @   s0   e Zd ZdZdZdd Zedd Zdd Zd	S )

LambdaNamez<lambda>functionc             C   s   || _ |j| _d S )N)_lambda_contextparent_context)selfZlambda_context r#   =lib/python3.7/site-packages/jedi/evaluate/context/function.py__init__   s    zLambdaName.__init__c             C   s
   | j jjS )N)r    	tree_node	start_pos)r"   r#   r#   r$   r'   !   s    zLambdaName.start_posc             C   s
   t | jS )N)r   r    )r"   r#   r#   r$   infer%   s    zLambdaName.inferN)	__name__
__module____qualname__string_nameapi_typer%   propertyr'   r(   r#   r#   r#   r$   r      s
   r   c               @   sP   e Zd ZdZdddZdd Zedd Zdd	d
Zdd Z	dd Z
dd ZdS )AbstractFunctionr   Nc             c   sD   |rt | j| ||dV  n&|  }x|jd|dD ]
}|V  q2W d S )N)contextuntil_positionorigin_scopeF)search_globalr2   )r   	evaluatorpy__class__get_filters)r"   r3   r1   r2   Zscopefilterr#   r#   r$   r6   ,   s    zAbstractFunction.get_filtersc                s    |     fdd| j D S )Nc                s   g | ]}t  |jqS r#   )r   name).0Zparam)function_executionr#   r$   
<listcomp>;   s   z4AbstractFunction.get_param_names.<locals>.<listcomp>)get_function_executionr&   Z
get_params)r"   r#   )r:   r$   get_param_names9   s    
z AbstractFunction.get_param_namesc             C   s"   | j jdkrt| S t| | j jS )Nlambdef)r&   typer   r   r8   )r"   r#   r#   r$   r8   >   s    zAbstractFunction.namec             C   s   t d S )N)NotImplementedError)r"   	argumentsr#   r#   r$   r<   D   s    z'AbstractFunction.get_function_executionc             C   s   |  |}| |S )N)r<   infer_function_execution)r"   rA   r:   r#   r#   r$   
py__call__G   s    
zAbstractFunction.py__call__c             C   s   | j jjdk}tt| j| j }|rr|rL| jjjdk r:tS t	t
| j|S | jjjdk r^tS t	t
| j|S n|rt	t| j|S | S dS )z4
        Created to be used by inheritance.
        Z
async_stmt)      )rD      N)r&   parentr?   boolr   r4   Zenvironmentversion_infor   r   r   AsyncGenerator	Coroutiner   	Generatorget_return_values)r"   r:   Zis_coroutineZis_generatorr#   r#   r$   rB   K   s    z)AbstractFunction.infer_function_executionc             C   s   | j jS )N)r8   r,   )r"   r#   r#   r$   
py__name__a   s    zAbstractFunction.py__name__)NN)N)r)   r*   r+   r-   r6   r=   r.   r8   r<   rC   rB   rN   r#   r#   r#   r$   r/   )   s   

r/   c               @   s.   e Zd ZdZedd Zd	ddZdd ZdS )
FunctionContextzF
    Needed because of decorators. Decorators are evaluated here.
    c             C   s:   ddl m} x| s t||r(|j}qW | |j||dS )Nr   )AbstractInstanceContext)r!   r&   )jedi.evaluate.contextrP   Zis_class
isinstancer!   r4   )clsr0   r&   rP   r#   r#   r$   from_contexti   s    
zFunctionContext.from_contextNc             C   s    |d krt  }t| j| j| |S )N)r   FunctionExecutionContextr4   r!   )r"   rA   r#   r#   r$   r<   r   s    z&FunctionContext.get_function_executionc             C   s   t | jdS )NZFUNCTION_CLASS)r   Zget_special_objectr4   )r"   r#   r#   r$   r5   x   s    zFunctionContext.py__class__)N)r)   r*   r+   __doc__classmethodrT   r<   r5   r#   r#   r#   r$   rO   e   s   	
rO   c                   sz   e Zd ZdZeZ fddZeede	
 dddZdd	 Ze	j
eg ddd
dZdddZe dd Z  ZS )rU   aM  
    This class is used to evaluate functions and their returns.

    This is the most complicated class, because it contains the logic to
    transfer parameters. It is even more complicated, because there may be
    multiple calls to functions and recursion has to be avoided. But this is
    responsibility of the decorators.
    c                s&   t t| |||j || _|| _d S )N)superrU   r%   r&   function_contextvar_args)r"   r4   r!   rY   rZ   )	__class__r#   r$   r%      s    
z!FunctionExecutionContext.__init__)defaultFc       	   	   C   s  | j }|jdkr | |jd S |r6t}t| j|}n$| }t	| j
}|t	| j
O }x|D ]}t| ||}|tjkrtd| np|r|tdd | |D O }nLy
|j}W n. tk
r   t| jd}|t|O }Y nX || |d O }|tjkr`td| P q`W |S )	Nr>   zReturn unreachable: %sc             s   s   | ]}|  V  qd S )N)r(   )r9   lazy_contextr#   r#   r$   	<genexpr>   s   z=FunctionExecutionContext.get_return_values.<locals>.<genexpr>None   zReturn reachable: %s)r&   r?   Z	eval_nodechildrenr   r   r4   Ziter_return_stmtsr	   Zinfer_return_typesrY   r
   r   Zreachability_checkZUNREACHABLEr   Zdbgr   Z	from_sets_get_yield_lazy_contextAttributeErrorr   builtin_from_nameZ	REACHABLE)	r"   check_yieldsfuncdefZcontext_setZreturnsrZcheckrb   ctxr#   r#   r$   rM      s6    




z*FunctionExecutionContext.get_return_valuesc             c   sz   |j dkr&t| jd}t|V  d S |jd }|j dkrjt| |jd }x*| |D ]
}|V  qZW nt	| |V  d S )Nkeywordr`   ra   Z	yield_arg)
r?   r   re   r4   r   rb   r   r(   iterater   )r"   Z
yield_exprri   Znodecnr^   r#   r#   r$   rc      s    



z0FunctionExecutionContext._get_yield_lazy_contextc             c   s  dd t | j| jD }g }d }x|D ]\}}|j}|jdkrD|j}|jdkr|| jkrt|r||kr~|d d | q|||gf n:|| jkr|d |gf n| jdd}|rt	|V  d S |}q&W x|D ]\}}	|d krx|	D ] }x| 
|D ]}
|
V  q W qW q| }t| |}| |}t|}xl|D ]d}t|jd j| i}t| ||2 x*|	D ]"}x| 
|D ]}
|
V  qW qvW W d Q R X qDW qW d S )	Nc          
   S   s"   g | ]}|t |d dddfqS )for_stmtrg   Z
while_stmtZif_stmt)r   Zsearch_ancestor)r9   yr#   r#   r$   r;      s   zDFunctionExecutionContext.get_yield_lazy_contexts.<locals>.<listcomp>Zsuiterm   r]   ra   T)rf   )r   r4   r&   rG   r?   r   Zfor_stmt_defines_one_nameappendrM   r   rc   Zget_testlistr   r(   rk   liststrrb   valuer   Zpredefine_names)r"   Zis_asyncZfor_parentsZyields_orderZlast_for_stmtZyield_rm   rG   typesZyieldsresultZ
input_noderl   Zorderedr^   ZdctZyield_in_same_for_stmtr#   r#   r$   get_yield_lazy_contexts   sF    








z0FunctionExecutionContext.get_yield_lazy_contextsNc             c   s   | j | j| ||dV  d S )N)r1   r2   )function_execution_filterr4   )r"   r3   r1   r2   r#   r#   r$   r6      s    
z$FunctionExecutionContext.get_filtersc             C   s   | j | S )N)rZ   get_executed_params)r"   r#   r#   r$   rw      s    z,FunctionExecutionContext.get_executed_params)F)F)NN)r)   r*   r+   rV   r   rv   r%   r   r   r   Zexecution_recursion_decoratorrM   rc   iterru   r6   rw   __classcell__r#   r#   )r[   r$   rU   |   s   	$0
rU   N)+Zparso.pythonr   Zjedi._compatibilityr   Zjedir   Zjedi.evaluate.cacher   r   Zjedi.evaluater   r   r	   r
   r   r   Zjedi.evaluate.argumentsr   Zjedi.evaluate.filtersr   r   r   r   r   Zjedi.evaluate.base_contextr   r   r   r   Zjedi.evaluate.lazy_contextr   r   r   rQ   r   r   r   Zjedi.evaluate.parser_cacher   r   r/   rO   rU   r#   r#   r#   r$   <module>   s*   <