B
    7r\?                 @   sl  d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZmZ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 G dd de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"G dd deeZ#G dd deZ$G dd deZ%dd d!d"d d#Z&d$d% Z'd&d' Z(d(d) Z)d*d+ Z*e)e d,d- Z+dS ).z$
Imitate the parser representation.
    N)partial)debug)force_unicode	Parameter)underscore_memoizationmemoize_method)AbstractFilterAbstractNameDefinitionContextNameMixin)Context
ContextSet)LazyKnownContext)	_sentinel)evaluator_function_cache)reraise_as_evaluator   )fakec               @   s    e Zd ZdZdd Zdd ZdS )CheckAttributez<Raises an AttributeError if the attribute X isn't available.c             C   s   || _ t|jdd  | _d S )N   )funcr   __name__
check_name)selfr    r   =lib/python3.7/site-packages/jedi/evaluate/compiled/context.py__init__   s    zCheckAttribute.__init__c             C   s@   |d kr| S | j dkr&|j s4tn|j| j  t| j|S )N__iter__)r   access_handleZhas_iterAttributeErrorgetattrr   r   )r   instanceownerr   r   r   __get__   s    

zCheckAttribute.__get__N)r   
__module____qualname____doc__r   r"   r   r   r   r   r      s   r   c                   s&  e Zd Zd9 fdd	Zedd Zedd Zedd	 Zed
d Zedd Z	dd Z
dd Zdd Zd:ddZdd Zdd Zedd Zedd Zedd  Zd;d!d"Zed#d$ Zed%d& Zed'd( Zd)d* Zed+d, Zd-d. Zd/d0 Zefd1d2Zd3d4 Z d5d6 Z!d7d8 Z"  Z#S )<CompiledObjectNc                s"   t t| || || _|| _d S )N)superr&   r   r   	tree_node)r   	evaluatorr   parent_contextZfaked_class)	__class__r   r   r   -   s    zCompiledObject.__init__c             C   s|   | j d k	r<| j jdkr<ddlm} || j| j| j d|S | j rhddl	m
} t|| j| j| |S t| |S d S )NZfuncdefr   )FunctionContext)r*   r(   )CompiledInstance)r(   typeZjedi.evaluate.context.functionr,   r)   r*   
py__call__r   is_classZjedi.evaluate.contextr-   r   from_iterable_execute_function)r   paramsr,   r-   r   r   r   r/   3   s    

zCompiledObject.py__call__c             C   s   t | j| j S )N)create_from_access_pathr)   r   py__class__)r   r   r   r   r5   B   s    zCompiledObject.py__class__c                s"    ft  fdd j D  S )Nc             3   s   | ]}t  j|V  qd S )N)r4   r)   ).0access)r   r   r   	<genexpr>I   s   z+CompiledObject.py__mro__.<locals>.<genexpr>)tupler   Zpy__mro__accesses)r   r   )r   r   	py__mro__F   s    
zCompiledObject.py__mro__c                s   t  fdd j D S )Nc             3   s   | ]}t  j|V  qd S )N)r4   r)   )r6   r7   )r   r   r   r8   P   s   z-CompiledObject.py__bases__.<locals>.<genexpr>)r9   r   py__bases__)r   r   )r   r   r;   M   s    
zCompiledObject.py__bases__c             C   s
   | j  S )N)r   
py__path__)r   r   r   r   r<   T   s    zCompiledObject.py__path__c             C   s
   | j  S )N)r   
py__bool__)r   r   r   r   r=   X   s    zCompiledObject.py__bool__c             C   s
   | j  S )N)r   
py__file__)r   r   r   r   r>   [   s    zCompiledObject.py__file__c             C   s
   | j  S )N)r   r0   )r   r   r   r   r0   ^   s    zCompiledObject.is_classFc             C   s
   | j  S )N)r   	py__doc__)r   Zinclude_call_signaturer   r   r   r?   a   s    zCompiledObject.py__doc__c             c   s   y| j  }W nl tk
rz   |  \}}|d}| j  rJ|dd x*|D ]"}| d}t| |d V  qPW Y nX x|D ]}t	| |V  qW d S )N,r   r   =)
r   Zget_signature_params
ValueError_parse_function_docsplitZismethoddescriptorinsertstripUnresolvableParamNameSignatureParamName)r   Zsignature_paramsZ
params_strrettokensppartssignature_paramr   r   r   get_param_namesd   s    



zCompiledObject.get_param_namesc             C   s   d| j j| j f S )Nz<%s: %s>)r+   r   r   get_repr)r   r   r   r   __repr__s   s    zCompiledObject.__repr__c             C   s   |   }|d krdS t|S )N) rQ   )r?   rC   )r   docr   r   r   rC   v   s    z"CompiledObject._parse_function_docc             C   s
   | j  S )N)r   Zget_api_type)r   r   r   r   api_type~   s    zCompiledObject.api_typec             C   s   | S )z
        We used to limit the lookups for instantiated objects like list(), but
        this is not the case anymore. Python itself
        r   )r   r   r   r   _cls   s    zCompiledObject._clsc             c   s   |  |V  d S )N)_ensure_one_filter)r   Zsearch_globalis_instanceZuntil_positionZorigin_scoper   r   r   get_filters   s    zCompiledObject.get_filtersc             C   s   t | j| |S )z|
        search_global shouldn't change the fact that there's one dict, this way
        there's only one `object`.
        )CompiledObjectFilterr)   )r   rV   r   r   r   rU      s    z!CompiledObject._ensure_one_filterc          	   C   sB   t ttt | j|}W d Q R X |d kr2t S tt| j|S )N)	r   
IndexErrorKeyError	TypeErrorr   py__getitem__r   r4   r)   )r   indexr7   r   r   r   r\      s
    zCompiledObject.py__getitem__c             c   s*   x$| j  D ]}tt| j|V  qW d S )N)r   Zpy__iter__listr   r4   r)   )r   r7   r   r   r   
py__iter__   s    zCompiledObject.py__iter__c             C   s
   | j  S )N)r   
py__name__)r   r   r   r   r_      s    zCompiledObject.py__name__c             C   s$   |   }|d kr| j }t| |S )N)r_   r   rO   CompiledContextName)r   namer   r   r   ra      s    
zCompiledObject.namec          	   c   s   ddl m} ddlm} | jdkr&d S xj|  d  D ]V}y| jjj	
| W n tk
rf   w8Y q8X || j|}x||D ]
}|V  qW q8W x|| D ]
}|V  qW d S )Nr   )
docstrings)builtin_from_nameZfunctionr   )Zjedi.evaluaterb   jedi.evaluate.compiledrc   rS   rC   rD   r)   builtins_moduler   r   r   ZexecuteZinfer_return_types)r   r3   rb   rc   ra   Zbltn_objresultZtype_r   r   r   r2      s    
z CompiledObject._execute_functionc                s   t  fdd j D S )Nc             3   s   | ]}t  j|V  qd S )N)r4   r)   )r6   r7   )r   r   r   r8      s   z-CompiledObject.dict_values.<locals>.<genexpr>)r   r1   r   dict_values)r   r   )r   r   rg      s    
zCompiledObject.dict_valuesc             C   s.   y
| j  S  tk
r(   |tkr$ |S X d S )N)r   get_safe_valuerB   r   )r   defaultr   r   r   rh      s    
zCompiledObject.get_safe_valuec             C   s   t | j| j|j|S )N)r4   r)   r   execute_operation)r   otheroperatorr   r   r   rj      s    z CompiledObject.execute_operationc             C   s   t | j| j S )N)r4   r)   r   negate)r   r   r   r   rm      s    zCompiledObject.negatec             C   s   | j |S )N)r   is_super_class)r   Z	exceptionr   r   r   rn      s    zCompiledObject.is_super_class)NN)F)FFNN)$r   r#   r$   r   r   r/   r5   r:   r;   r<   r=   r>   r0   r?   rN   rP   r   rC   propertyrS   rT   rW   r   rU   r\   r^   r_   ra   r2   rg   r   rh   rj   rm   rn   __classcell__r   r   )r+   r   r&   ,   s8   
	 
	r&   c               @   s4   e Zd Zdd Zdd Zedd Zedd Zd	S )
CompiledNamec             C   s   || _ || _|| _d S )N)
_evaluatorr*   string_name)r   r)   r*   ra   r   r   r   r      s    zCompiledName.__init__c             C   s:   y| j j}W n tk
r$   d }Y nX d| jj|| jf S )Nz<%s: (%s).%s>)r*   ra   r   r+   r   rs   )r   ra   r   r   r   rP      s
    
zCompiledName.__repr__c             C   s   t t|  jS )N)nextiterinferrS   )r   r   r   r   rS      s    zCompiledName.api_typec             C   s   t t| j| j| jS )N)r   create_from_namerr   r*   rs   )r   r   r   r   rv      s    zCompiledName.inferN)	r   r#   r$   r   rP   ro   rS   r   rv   r   r   r   r   rq      s   rq   c               @   s<   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dS )rH   paramc             C   s   |j | _ || _d S )N)r*   _signature_param)r   compiled_objrM   r   r   r   r      s    zSignatureParamName.__init__c             C   s   | j jS )N)ry   ra   )r   r   r   r   rs      s    zSignatureParamName.string_namec             C   s   t t| jjS )N)r   r   ry   Z	kind_name)r   r   r   r   get_kind  s    zSignatureParamName.get_kindc             C   s   | j S )N)ry   )r   r   r   r   is_keyword_param  s    z#SignatureParamName.is_keyword_paramc             C   sL   | j }| jj}t }|jr*tt||j}|jrHt||j}||	 O }|S )N)
ry   r*   r)   r   Zhas_defaultr4   ri   Zhas_annotation
annotationZexecute_evaluated)r   rK   r)   Zcontextsr}   r   r   r   rv     s    zSignatureParamName.inferN)
r   r#   r$   rS   r   ro   rs   r{   r|   rv   r   r   r   r   rH      s   rH   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rG   rx   c             C   s   |j | _ || _d S )N)r*   rs   )r   rz   ra   r   r   r   r     s    zUnresolvableParamName.__init__c             C   s   t jS )N)r   ZPOSITIONAL_ONLY)r   r   r   r   r{     s    zUnresolvableParamName.get_kindc             C   s   t  S )N)r   )r   r   r   r   rv     s    zUnresolvableParamName.inferN)r   r#   r$   rS   r   r{   rv   r   r   r   r   rG     s   rG   c               @   s   e Zd Zdd ZdS )r`   c             C   s   || _ || _|j| _d S )N)rs   Z_contextr*   )r   contextra   r   r   r   r   "  s    zCompiledContextName.__init__N)r   r#   r$   r   r   r   r   r   r`   !  s   r`   c               @   s    e Zd ZdZdd Zdd ZdS )EmptyCompiledNamez
    Accessing some names will raise an exception. To avoid not having any
    completions, just give Jedi the option to return this object. It infers to
    nothing.
    c             C   s   |j | _|| _d S )N)re   r*   rs   )r   r)   ra   r   r   r   r   .  s    zEmptyCompiledName.__init__c             C   s   t  S )N)r   )r   r   r   r   rv   2  s    zEmptyCompiledName.inferN)r   r#   r$   r%   r   rv   r   r   r   r   r   (  s   r   c               @   sJ   e Zd ZeZdddZdd ZdddZeddd	Z	d
d Z
dd ZdS )rX   Fc             C   s   || _ || _|| _d S )N)rr   _compiled_object_is_instance)r   r)   compiled_objectrV   r   r   r   r   9  s    zCompiledObjectFilter.__init__c                s$   j   fddfddddS )Nc                  s   j j S )N)r   r   Zis_allowed_getattrr   )ra   r   r   r   <lambda>A  s    z*CompiledObjectFilter.get.<locals>.<lambda>c                  s    j j S )N)r   r   dirr   )r   r   r   r   B  s    T)check_has_attribute)_get)r   ra   r   )ra   r   r   get>  s
    
zCompiledObjectFilter.getc             C   sV   | \}}|r|sg S t |}|s&|s6| j|ddgS | jrJ|| krJg S | |gS )zU
        To remove quite a few access calls we introduced the callback here.
        T)is_empty)r   _get_cached_namer   )r   ra   Zallowed_getattr_callbackZdir_callbackr   Zhas_attributeZis_descriptorr   r   r   r   F  s    
zCompiledObjectFilter._getc             C   s   |rt | j|S | |S d S )N)r   rr   _create_name)r   ra   r   r   r   r   r   X  s    z%CompiledObjectFilter._get_cached_namec          	      s   ddl m} g }| jj \} x0 D ](||  fdd fdd7 }q&W | js|rx$|| jd D ]}||	 7 }qnW |S )Nr   )rc   c                  s     S )Nr   r   )	dir_infosra   r   r   r   f  s    z-CompiledObjectFilter.values.<locals>.<lambda>c                  s      S )N)keysr   )r   r   r   r   g  s    r.   )
rd   rc   r   r   Zget_dir_infosr   r   rr   rW   values)r   rc   namesZneeds_type_completionsfilterr   )r   ra   r   r   _  s    

zCompiledObjectFilter.valuesc             C   s   |  | j| j|S )N)
name_classrr   r   )r   ra   r   r   r   r   p  s    z!CompiledObjectFilter._create_nameN)F)F)F)r   r#   r$   rq   r   r   r   r   r   r   r   r   r   r   r   r   rX   6  s   

rX   floatstrintdict)zfloating point number	characterZintegerZ
dictionarystringc          	   C   sT  t | } ytd}| d}xPt| |d D ]<\}}|dkrD|d7 }n|dkrT|d8 }|dkr*|| }P q*W | |d | }W n* ttfk
r   td d}d}Y n,X dd	 }x td
||\}}|dkrP qW |	dd}t
d| ||d  }	|	dkrd}
nF||	  }td}|| |d }tdd|}t||}
||
fS )z
    Takes a function and returns the params and return value as a tuple.
    This is nothing more than a docstring parser.

    TODO docstrings like utime(path, (atime, mtime)) and a(b [, b]) -> None
    TODO docstrings like 'tuple of integers'
    r   (Nr   )zno brackets found - no paramrQ   c             S   sL   |  dd}x0t|D ]$\}}|rd|kr||  d7  < qW d|S )Nr   r@   rA   z=None)grouprD   	enumeratejoin)margsiar   r   r   change_options  s
    z+_parse_function_doc.<locals>.change_optionsz ?\[([^\[\]]+)\]-_z-[>-]*    z(,\n|[^\n-])+z[nN]ew (.*)z\1())r   r]   r   rB   UnboundLocalErrorr   Zdbgresubnreplacesearchendcompilematchr   rF   subdocstr_defaultsr   )rR   countstartr   sr   Z	param_strr   ZchangesrrI   r]   patternZret_strr   r   r   rC   }  sB    





rC   c             C   s^   d }yt ||}W n t jk
r*   Y nX |jj|d d}|}| rN|j}t| |||dS )N)ri   )r*   faked)r   get_faked_with_parent_contextFakeDoesNotExistr   r   r0   r*   create_cached_compiled_object)r)   r   ra   r   r7   r*   r   r   r   rw     s    rw   c                s   d fdd	}|S )z5The cache doesn't care about keyword vs. normal args.Nc                s    | |||S )Nr   )r)   objr*   r   )r   r   r   wrapper  s    z'_normalize_create_args.<locals>.wrapper)NNr   )r   r   r   )r   r   _normalize_create_args  s    r   c          	   C   sv   d }xl|j D ]b\}}y0|d kr6t| |j d d }nt||}W n tjk
r^   d }Y nX t| |||}qW |S )Nr   )Zaccessesr   Zget_faked_moduler   r   r   )r)   Zaccess_pathr*   ra   r7   r   r   r   r   r4     s    
r4   c             C   s   t | |||S )N)r&   )r)   r   r*   r   r   r   r   r     s    r   ),r%   r   	functoolsr   Zjedir   Zjedi._compatibilityr   r   Z
jedi.cacher   r   Zjedi.evaluate.filtersr   r	   r
   Zjedi.evaluate.base_contextr   r   Zjedi.evaluate.lazy_contextr   Zjedi.evaluate.compiled.accessr   Zjedi.evaluate.cacher   Zjedi.evaluate.helpersr   rQ   r   objectr   r&   rq   rH   rG   r`   r   rX   r   rC   rw   r   r4   r   r   r   r   r   <module>   s@    3?=