B
    7r\,                 @   s  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
 ddl	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mZmZ ddlmZ ddlmZ ddlmZ dZG dd deZ dd Z!dd Z"d=ddZ#e#ddd Z$e#dd>ddZ%e#ddd  Z&G d!d" d"eZ'e#d#d$d%d&d' Z(e#d(d$d$d)d*d+ Z)e#d,d$d-d.d/ Z*d0d1 Z+e#d2d3d4 Z,e%e&e(e)e*d5e,e,d6d7d8 d9d8 d:d;e+id<Z-dS )?a  
Implementations of standard library functions, because it's not possible to
understand them with Jedi.

To add a new implementation, create a function and add it to the
``_implemented`` dict at the bottom of this module.

Note that this module exists only to implement very specific functionality in
the standard library. The usual way to understand the standard library is the
compiled module that returns the types for C-builtins.
    N)force_unicode)debug)ValuesArgumentsrepack_with_argument_clinic)analysis)compiled)AbstractInstanceContextCompiledInstanceBoundMethodInstanceArguments)ContextualizedNodeNO_CONTEXTS
ContextSet)ClassContextModuleContextFunctionExecutionContext)iterable)LazyTreeContext)	is_stringa	  
    def __init__(_cls, {arg_list}):
        'A helper function for namedtuple.'
        self.__iterable = ({arg_list})

    def __iter__(self):
        for i in self.__iterable:
            yield i

    def __getitem__(self, y):
        return self.__iterable[y]

c               @   s   e Zd ZdS )NotInStdLibN)__name__
__module____qualname__ r   r   3lib/python3.7/site-packages/jedi/evaluate/stdlib.pyr   .   s   r   c             C   s   t |trt y|jj}W n tk
r0   Y ndX |j| jkrDd}nt |jtr\|jjj}nd}yt	| | }W n t
k
r   Y nX || ||dS t d S )Nbuiltins )	arguments)
isinstancer
   r   namestring_nameAttributeErrorparent_contextbuiltins_moduler   _implementedKeyError)	evaluatorobjr   Zobj_nameZmodule_namefuncr   r   r   execute2   s"    
r)   c             C   s:   yt | | \}}W n tk
r,   tS X | S d S )N)listunpack
IndexErrorr   infer)r&   r   indexkeylazy_contextr   r   r   _follow_paramL   s
    r1   Fc                s    fdd}|S )zL
    Works like Argument Clinic (PEP 436), to validate function params.
    c                s"   t dd fdd}|S )NT)Zkeep_arguments_paramc                sr   | d}|rttjd| dd t}r6|j|d< rB||d< rN||d<  | f||}tjd|dd |S )Nr   zbuiltin start %sZMAGENTA)Zcolorcontextr'   zbuiltin end: %s)popAssertionErrorr   Zdbgr   r2   )r&   r'   argskwargsr   result)r(   want_argumentswant_contextwant_objr   r   wrapper[   s    

z+argument_clinic.<locals>.f.<locals>.wrapper)r   )r(   r;   )stringr8   r9   r:   )r(   r   fZ   s    zargument_clinic.<locals>.fr   )r<   r:   r9   r8   r=   r   )r<   r8   r9   r:   r   argument_clinicU   s    r>   ziterator[, default], /c                sd   | j jjdkrd nd t}x:|D ]2}t|tr"t fdd|jddD 	 }q"W |r`|S |S )z
    TODO this function is currently not used. It's a stab at implementing next
    in a different way than fake objects. This would be a bit more flexible.
       next__next__c             3   s&   | ]}|  D ]}| V  qqd S )N)getr-   ).0filtern)r   r   r   	<genexpr>~   s   z builtins_next.<locals>.<genexpr>T)Zinclude_self_names)
Zenvironmentversion_infomajorr   r   r   r   	from_setsZget_filtersexecute_evaluated)r&   Z	iteratorsdefaultsZcontext_setiteratorr   )r   r   builtins_nexto   s    


rM   zobject, name[, default], /c             C   sF   x@|D ]8}x2|D ]*}t |r.|t| S td qqW qW tS )Nzgetattr called without str)r   py__getattribute__r   get_safe_valuer   Zwarningr   )r&   objectsnamesrK   r'   r   r   r   r   builtins_getattr   s    



rR   zobject[, bases, dict], /c             C   s   |s|rt S | S d S )N)r   py__class__)r&   rP   basesZdictsr   r   r   builtins_type   s    rU   c                   s    e Zd ZdZ fddZ  ZS )SuperInstancez-To be used like the object ``super`` returns.c                s*   |  d }t ||r |d p"|  d S )N   r   )Zpy_mrosuper__init__)selfr&   clssu)	__class__r   r   rY      s    zSuperInstance.__init__)r   r   r   __doc__rY   __classcell__r   r   )r]   r   rV      s   rV   z[type[, obj]], /T)r9   c             C   s:   t |tr6t |jtr6|jj  }|d   S t	S )Nr   )
r   r   Zvar_argsr   instancerS   Zpy__bases__r-   rJ   r   )r&   typesrP   r2   r\   r   r   r   builtins_super   s
    
rb   zsequence, /)r:   r8   c       
      C   sv   t | \}}d }t|tr,t|j|j}t||}tt	|}t
| d|}	tt|	g}tt| | j||S )Nr*   )r@   r+   r   r   r   _contextdatar*   iteratereversedr   ZFakeSequencer   r   r	   r#   )
r&   	sequencesr'   r   r/   r0   ZcnZorderedZrevseqr   r   r   builtins_reversed   s    
ri   zobj, type, /)r8   c          	      s  t  }x|D ]}| }y
|j}W n" tk
rD   t ddg}P Y nX | x|D ]}| rn||k qR|jjdkr|  j	krt
dd | D }	|tfdd|	D  qRt| d \}
}t|trR|j}d| }t|jd	|| qRW qW t
 fd
d|D S )NTFtuplec             s   s   | ]}|  V  qd S )N)r-   )rC   r0   r   r   r   rF      s   z&builtins_isinstance.<locals>.<genexpr>c             3   s   | ]}| kV  qd S )Nr   )rC   r[   )mror   r   rF      s    rW   z[TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types, not %s.ztype-error-isinstancec             3   s"   | ]}t  tt|V  qd S )N)r   Zbuiltin_from_namer   str)rC   b)r&   r   r   rF      s   )setrS   Z	py__mro__r!   Zis_classaddr   r    Zget_root_contextr#   r   rI   re   anyr*   r+   r   r   rd   r   rc   from_iterable)r&   rP   ra   r   Zbool_resultsor[   Zmro_funcZ
cls_or_tupclasses_r0   Znodemessager   )r&   rk   r   builtins_isinstance   s6    




rv   c                sZ  |j   d}|stS tt| |dd  }tt| |dd }t|tjrf| 	dd
 }n$t|tjrdd | D }ntS  fdd	tt| }|t7 }|j|t|t|tt|	d
d	dddd dfdd|D dfddt|D d}| j|}	t|	 }
t| |	dtj|ddd}tt| ||
S )z
    Implementation of the namedtuple function.

    This has to be done by processing the namedtuple class template and
    evaluating the result.

    Z_class_templater   rW   , c             S   s*   g | ]"}|  D ]}t|r| qqS r   )r-   r   rO   )rC   r0   vr   r   r   
<listcomp>  s   z*collections_namedtuple.<locals>.<listcomp>c                s     | \}| S )N)rN   rO   )r   x)collections_contextr   r   get_var  s    z'collections_namedtuple.<locals>.get_varzu'r   'z, c             3   s   | ]} d j |dV  qdS )Z_repr_template)r   N)format)rC   r   )r}   r   r   rF     s    z)collections_namedtuple.<locals>.<genexpr>
c             3   s$   | ]\}} d j ||dV  qdS )Z_field_template)r.   r   N)r   )rC   r.   r   )r}   r   r   rF     s   )typenamefield_names
num_fieldsarg_listrepr_fmtZ
field_defsNT)keepends)Z
code_lines)r"   rN   r   r*   r1   rO   r   r   ZCompiledObjectreplacesplitr   SequenceZ
py__iter__r@   iter_NAMEDTUPLE_INITr   rj   lenreprjoin	enumerateZgrammarparseZiter_classdefsr   parsoZsplit_linesr   r   )r&   r'   r   Z_class_template_setr   _fieldsZfieldsbasecodemoduleZgenerated_classr"   r   )r|   r}   r   collections_namedtuple   s:    
"r   zfirst, /c             C   s   |S )Nr   )r&   Zfirstsr   r   r   _return_first_param)  s    r   )getattrtyperX   rf   r   )copydeepcopyc             C   s   t S )N)r   )r&   r'   r   r   r   r   <lambda>;  s    r   c             C   s   t S )N)r   )r&   r'   r   r   r   r   r   <  s    )loadloads
namedtuple)r   r   Zjsoncollections)FFF)N).r^   r   Zjedi._compatibilityr   Zjedir   Zjedi.evaluate.argumentsr   r   Zjedi.evaluater   r   Zjedi.evaluate.context.instancer   r	   r
   r   Zjedi.evaluate.base_contextr   r   r   Zjedi.evaluate.contextr   r   r   r   Zjedi.evaluate.lazy_contextr   Zjedi.evaluate.syntax_treer   r   LookupErrorr   r)   r1   r>   rM   rR   rU   rV   rb   ri   rv   r   r   r$   r   r   r   r   <module>   sJ   	
	*:
