B
    7r\u!                 @   s  d 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
 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 G dd deZG dd dejZG dd dejZe dd Zdd Z dd Z!eddd Z"dS )z 
Used only for REPL Completion.
    N)get_cached_code_lines)settings)compiled)underscore_memoization)imports)Context
ContextSet)ModuleContext)evaluator_function_cache)getattr_static)compiled_objects_cache)create_cached_compiled_objectc               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )MixedObjecta  
    A ``MixedObject`` is used in two ways:

    1. It uses the default logic of ``parser.python.tree`` objects,
    2. except for getattr calls. The names dicts are generated in a fashion
       like ``CompiledObject``.

    This combined logic makes it possible to provide more powerful REPL
    completion. It allows side effects that are not noticable with the default
    parser structure to still be completeable.

    The biggest difference from CompiledObject to MixedObject is that we are
    generally dealing with Python code and not with C code. This will generate
    fewer special cases, because we in Python you don't have the same freedoms
    to modify the runtime.
    c             C   s$   || _ || _|| _|| _|j| _d S )N)	evaluatorparent_contextcompiled_object_contextaccess_handle)selfr   r   r   tree_context r   ;lib/python3.7/site-packages/jedi/evaluate/compiled/mixed.py__init__'   s
    zMixedObject.__init__c              O   s   t j| |S )N)r   py__getattribute__)argskwargsr   r   r   r   1   s    zMixedObject.py__getattribute__c             o   s   t | j| V  d S )N)MixedObjectFilterr   )r   r   r   r   r   r   get_filters4   s    zMixedObject.get_filtersc             C   s   dt | j| j f S )Nz<%s: %s>)type__name__r   Zget_repr)r   r   r   r   __repr__7   s    zMixedObject.__repr__c             C   s   t | j|S )N)getattrr   )r   namer   r   r   __getattr__:   s    zMixedObject.__getattr__N)	r   
__module____qualname____doc__r   r   r   r    r#   r   r   r   r   r      s   
r   c               @   sB   e Zd ZdZedd Zejdd Zedd Zedd Z	d	S )
	MixedNamezC
    The ``CompiledName._compiled_object`` is our MixedObject.
    c             C   s    t |  }|sdS |d jjS )N)r   r   r   )listinferr"   	start_pos)r   Zcontextsr   r   r   r*   B   s    zMixedName.start_posc             C   s   d S )Nr   )r   valuer   r   r   r*   J   s    c             C   s.   | j j}|j| jd d}tt| j|| j dS )N)default)r   )r   r   r!   Zstring_namer   _createZ
_evaluator)r   r   r   r   r   r)   O   s    zMixedName.inferc             C   s   t t|  jS )N)nextiterr)   api_type)r   r   r   r   r0   X   s    zMixedName.api_typeN)
r   r$   r%   r&   propertyr*   setterr   r)   r0   r   r   r   r   r'   >   s
   	r'   c                   s"   e Zd ZeZd fdd	Z  ZS )r   Fc                s   t t| ||| || _d S )N)superr   r   Z_mixed_object)r   r   Zmixed_objectZis_instance)	__class__r   r   r   `   s    

zMixedObjectFilter.__init__)F)r   r$   r%   r'   Z
name_classr   __classcell__r   r   )r4   r   r   ]   s   r   c             C   s    | j j|dtjtjd }|S )NT)pathcacheZ
diff_cache
cache_path)grammarparser   Zfast_parserZcache_directoryget_root_node)r   r6   module_noder   r   r   _load_modulei   s    r=   c             C   sn   t | sFt | sFt | sFt | sFt | sFt | sFt | rJ| S y| jS  t	k
rh   t
Y nX dS )z9Check if inspect.getfile has a chance to find the source.N)inspectismoduleZisclassZismethodZ
isfunctionZistracebackZisframeZiscoder4   AttributeError	TypeError)python_objectr   r   r   _get_object_to_checkw   s    






rC   c       
         s*  |j j}yt|}t|}W n tk
r2   d S X |d ksHtj|sLd S t	| |}t
|rxt| j|}||||fS y
|j}W n tk
r   d S X |dkrd S | |g }dd |D }|sd S y|j}|j W n tk
r   Y nX  fdd|D }	|	r|	}t| j|}||d j||fS )Nz<lambda>c             S   s   g | ]}|  r|qS r   )Zis_definition).0nr   r   r   
<listcomp>   s    z*_find_syntax_node_name.<locals>.<listcomp>c                s   g | ]}|j d   kr|qS )r   )r*   )rD   r"   )line_nrr   r   rF      s    )accessZ_objrC   r>   ZgetsourcefilerA   osr6   existsr=   r?   r   r9   r   r@   Zget_used_namesget__code__co_firstlinenoparent)
r   r   rB   r6   r<   
code_linesZname_strnamescodeZ
line_namesr   )rG   r   _find_syntax_node_name   s@    



rS   Zmixed_cachec             G   s   t | ||jd}t| |}|d kr&|S |\}}}}	|j |krJ| }
n2t| |||	d}
|  }|d k	r|t	| ||
 |
j
|ddd}|jdkr| s| \}t| |||dS )N)r   )r6   rP   T)Znode_is_contextZnode_is_objectZclassdef)r   )r   r   rS   	tree_noder;   Zget_root_contextr	   Z
py__name__r   Zadd_module_to_cacheZcreate_contextr   Zis_classZexecute_evaluatedr   )r   r   r   r   r   resultr<   rT   r6   rP   Zmodule_contextr"   r   r   r   r   r-      s6    



r-   )#r&   r>   rJ   Zjedi.parser_utilsr   Zjedir   Zjedi.evaluater   Z
jedi.cacher   r   Zjedi.evaluate.base_contextr   r   Zjedi.evaluate.contextr	   Zjedi.evaluate.cacher
   Z%jedi.evaluate.compiled.getattr_staticr   Zjedi.evaluate.compiled.accessr   Zjedi.evaluate.compiled.contextr   objectr   ZCompiledNamer'   ZCompiledObjectFilterr   r=   rC   rS   r-   r   r   r   r   <module>   s(   (?