B
    7r\                 @   s
  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 d dlmZ dd Zd	d
 Zd*ddZdd Zdd Zdd Zedd Zdd Zdd Zdd Zdd Zdd Zd d! ZG d"d# d#eZG d$d% d%eZG d&d' d'eZed(d) ZdS )+    N)chain)contextmanager)tree)unicode)get_parent_scopec             C   s@   d| ksd| krdS t jtjdd}ttt|d | S )Nzdist-packageszsite-packagesFlibZpythonz\d.\d)	ospathjoinsysprefixboolrematchescape)r	   	base_path r   4lib/python3.7/site-packages/jedi/evaluate/helpers.pyis_stdlib_path   s    r   c             C   s^   t  | }g }xD| jD ]:}t|tjr8t  |}||_nt|}||_|| qW ||_|S )zO
    Much, much faster than copy.deepcopy, but just for parser tree nodes.
    )copychildren
isinstancer   ZLeafparentdeep_ast_copyappend)objZnew_objZnew_childrenZchild	new_childr   r   r   r      s    

r   Fc             C   sB  |j }|jdkr(ddlm} || jS |jdksJ||jd |jd fkrh|jdkr^| |S | |S |j }|j|}|r|}n|d }|jdkr|}x"|d8 }|j| }	|	jdkrP qW |j|d |d  }
n|jd }	|jd| }
|	d	kr|
d }	|
dd
 }
| |	}ddl	m
} x|
D ]}|| ||}q(W |S )a  
    Creates a "call" node that consist of all ``trailer`` and ``power``
    objects.  E.g. if you call it with ``append``::

        list([]).append(3) or None

    You would get a node with the content ``list([]).append`` back.

    This generates a copy of the original ast node.

    If you're using the leaf, e.g. the bracket `)` it will return ``list([])``.

    We use this function for two purposes. Given an expression ``bar.foo``,
    we may want to
      - infer the type of ``foo`` to offer completions after foo
      - infer the type of ``bar`` to be able to jump to the definition of foo
    The option ``cut_own_trailer`` must be set to true for the second purpose.
    Zfstringr   )compiledtraileratom   
error_nodeawaitN)eval_trailer)r   typeZjedi.evaluater   Zget_string_context_set	evaluatorr   Z	eval_nodeindexZjedi.evaluate.syntax_treer$   )contextleafZcut_own_trailerr   r   powerr'   ZcutstartbaseZtrailersvaluesr$   r   r   r   evaluate_call_of_leaf/   s>    
"









r.   c             C   s   | j }|jdks(| |jd |jd fkr:|jdkr6|S | S |j }|j|}t|}t|j|_g |j|d d< |jdkr|}x|d8 }|j| jdkrP qW td|j|d }|j |_ |S |S )	ap  
    Creates a "call" node that consist of all ``trailer`` and ``power``
    objects.  E.g. if you call it with ``append``::

        list([]).append(3) or None

    You would get a node with the content ``list([]).append`` back.

    This generates a copy of the original ast node.

    If you're using the leaf, e.g. the bracket `)` it will return ``list([])``.
    r   r   r   r    r!   Nr"   r*   )r   r%   r   r'   r   listr   ZNode)r)   r   r*   r'   Z	new_powerr+   Ztransformedr   r   r   call_of_leafm   s(    "


r0   c             C   sP   y
| j }W n( tk
r2   | jdkr*| gS g S Y nX ttdd |D S d S )Nnamec             s   s   | ]}t |V  qd S )N)get_names_of_node).0cr   r   r   	<genexpr>   s    z$get_names_of_node.<locals>.<genexpr>)r   AttributeErrorr%   r/   r   from_iterable)Znoder   r   r   r   r2      s    


r2   c                s,   t    }|s( fdd|D }|S )zZ
    Returns a dictionary with name parts as keys and their call paths as
    values.
    c                s"   g | ]}t |j d fkr|qS )N)r   r   )r3   n)moduler   r   
<listcomp>   s    z$get_module_names.<locals>.<listcomp>)r   r7   Zget_used_namesr-   )r9   Z
all_scopesnamesr   )r9   r   get_module_names   s    r<   c             c   s&   | j }|||< z
d V  W d ||= X d S )N)Zpredefined_names)r(   Z
flow_scopeZdctZ
predefinedr   r   r   predefine_names   s
    
r=   c             C   s   ddl m} t| |S )Nr   )CompiledObject)Zjedi.evaluate.compiledr>   r   )r(   r>   r   r   r   is_compiled   s    r?   c             C   s:   | j jjjdkrttf}ntf}t| o8t| jd d|S )N   )default)	r&   Zenvironmentversion_infomajorr   bytesr?   r   get_safe_value)r(   Zstr_classesr   r   r   	is_string   s    
rF   c             C   s   t | pt| S )N)	is_numberrF   )r(   r   r   r   
is_literal   s    rH   c             C   s&   t | r"| jd d}t||r"|S d S )N)rA   )r?   rE   r   )r(   Zacceptvaluer   r   r   _get_safe_value_or_none   s    
rJ   c             C   s
   t | tS )N)rJ   int)r(   r   r   r   get_int_or_none   s    rL   c             C   s   t | ttfd k	S )N)rJ   rK   float)r(   r   r   r   rG      s    rG   c               @   s   e Zd ZdS )EvaluatorTypeErrorN)__name__
__module____qualname__r   r   r   r   rN      s   rN   c               @   s   e Zd ZdS )EvaluatorIndexErrorN)rO   rP   rQ   r   r   r   r   rR      s   rR   c               @   s   e Zd ZdS )EvaluatorKeyErrorN)rO   rP   rQ   r   r   r   r   rS      s   rS   c           
   g   sL   y
d V  W n< | k
rF } zt  d|jj  }||W d d }~X Y nX d S )NZ	Evaluator)globals	__class__rO   )Zexception_classeseZnew_exc_clsr   r   r   reraise_as_evaluator   s
    
rW   )F) r   r   r   r   	itertoolsr   
contextlibr   Zparso.pythonr   Zjedi._compatibilityr   Zjedi.parser_utilsr   r   r   r.   r0   r2   r<   r=   r?   rF   rH   rJ   rL   rG   	ExceptionrN   rR   rS   rW   r   r   r   r   <module>   s2   
>+
