B
    @X:*                 @   s\  d Z ddlZddlZddlZddlZddlZddlZddlmZ dBddZ	dCddZ
dDd	d
ZdEddZdFddZdd Zdd Zdd ZejejB dfddZddlZejd dkse dkre	Ze	ZeZe
Ze	Ze	Ze	ZefZeZ n(e
ZeZe	Ze	ZeZeZeZee!fZe!Z ejd dkrdZ"dGddZ#dZ$ddl%Z&ej'Z'dHdd Z(e)Z*d!d" Z+d#d$ Z,ej-Z-ej.Z.dId%d&Z/e0d'ej1Z2d(d) Z3ed*d+ Z4ed,d- Z5d.d/ Z6ndZ"dJd0dZ#d1Z$ddl7Z&ddlZe0d2Z8dKd3d Z(e*Z*d4d" Z+d5d$ Z,ej9Z-d6d7 Z.d8d+ Z4d9d/ Z6eZ'ed:d- Z5ejd;kr>dLd<d&Z/n
dMd=d&Z/d>d? Z:d@dA Z;dS )Nz=Compatibility tricks for Python 3. Mainly to do with unicode.    N   )DEFAULT_ENCODINGc             C   s   | S )N )xencodingr   r   9lib/python3.7/site-packages/ipython_genutils/py3compat.pyno_code   s    r   c             C   s   |pt }| |dS )Nreplace)r   decode)sr   r   r   r   r
      s    r
   c             C   s   |pt }| |dS )Nr	   )r   encode)ur   r   r   r   r      s    r   c             C   s   t | trt| |S | S )N)
isinstancebytesr
   )r   r   r   r   r   cast_unicode   s    

r   c             C   s   t | tst| |S | S )N)r   r   r   )r   r   r   r   r   
cast_bytes   s    

r   c             C   s(   t | tr|  S t | ts$t| S | S )z+Cast a buffer or memoryview object to bytes)r   
memoryviewtobytesr   )Zbufr   r   r   buffer_to_bytes"   s
    

r   c                s   t   fdd}|S )Nc                s8   t | trd }| }n
| }|j} |}|r4||_|S |S )N)r   string_types__doc__)func_or_strfuncdoc)str_change_funcr   r   wrapper+   s    
z)_modify_str_or_docstring.<locals>.wrapper)	functoolswraps)r   r   r   )r   r   _modify_str_or_docstring*   s    r   c             C   sf   yt | S  tk
r   Y nX ytt| S  tk
r>   Y nX ytt| S  tk
r`   Y nX dS )zlunicode(e) with various fallbacks. Used for exceptions, which may not be
    safe to call unicode() on.
    zUnrecoverably corrupt evalue)unicode_typeUnicodeErrorstr_to_unicodestrrepr)er   r   r   safe_unicode<   s    r%   c                s"  dd }t j r&| |r" S dS |dkr>t jdt j}|sFdS |t j}tj	dkrt j
|krt|dt j
 t jddt j}t fd	d
|D r g}q fdd|D }n g}t }xT|D ]L}t j|}||kr|| x(|D ] }	t j||	}
||
|r|
S qW qW dS )an  Given a command, mode, and a PATH string, return the path which
    conforms to the given mode on the PATH, or None if there is no such
    file.

    `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
    of os.environ.get("PATH"), or can be overridden with a custom search
    path.
    
    This is a backport of shutil.which from Python 3.4
    c             S   s&   t j| o$t | |o$t j|  S )N)ospathexistsaccessisdir)fnmoder   r   r   _access_check`   s    z$_shutil_which.<locals>._access_checkNPATHwin32r   ZPATHEXT c             3   s    | ]}   |  V  qd S )N)lowerendswith).0ext)cmdr   r   	<genexpr>}   s    z _shutil_which.<locals>.<genexpr>c                s   g | ]} | qS r   r   )r3   r4   )r5   r   r   
<listcomp>   s    z!_shutil_which.<locals>.<listcomp>)r&   r'   dirnameenvirongetdefpathsplitpathsepsysplatformcurdirinsertanysetnormcaseaddjoin)r5   r,   r'   r-   ZpathextfilesseendirZnormdirZthefilenamer   )r5   r   _shutil_whichR   s8    






rK      Z
IronPythonTr0   c             C   s
   t | S )N)builtin_modinput)promptr   r   r   rN      s    rN   builtinsFc             C   s$   |rt dd | dD S |  S )Nc             s   s   | ]}t |V  qd S )N)isidentifier)r3   ar   r   r   r6      s    zisidentifier.<locals>.<genexpr>.)allr<   rQ   )r   dottedr   r   r   rQ      s    rQ   c             C   s   t |  S )N)iteritems)dr   r   r   	iteritems   s    rY   c             C   s   t |  S )N)rV   values)rX   r   r   r   
itervalues   s    r[   c          	   C   sJ   |d k	r|n|}t | d&}|p"t}t|| | d|| W d Q R X d S )Nrbexec)opencompiler]   read)fnameglobloccompilerfr   r   r   execfile   s    rf   z\bprint (?P<expr>.*)$c             C   s   |  d}d| S )Nexprz	print(%s))groups)matchrg   r   r   r   _print_statement_sub   s    
rj   c             C   s   t t| S )zRefactor 'print x' statements in a doctest to print(x) style. 2to3
        unfortunately doesn't pick up on our doctests.
        
        Can accept a string or a function, so it can be used as a decorator.)_print_statement_resubrj   )r   r   r   r   doctest_refactor_print   s    rm   c             C   s   | j ddS )zt"{u}'abc'" --> "'abc'" (Python 3)
        
        Accepts a string or a function, so it can be used as a decorator.r0   )r   )format)r   r   r   r   u_format   s    ro   c             C   s   | j S )z"Get a function's closure attribute)__closure__)re   r   r   r   get_closure   s    rq   c             C   s
   t | S )N)rM   Z	raw_input)rO   r   r   r   rN      s    __builtin__z[a-zA-Z_][a-zA-Z0-9_]*$c             C   s*   |rt dd | dD S tt| S )Nc             s   s   | ]}t |V  qd S )N)rQ   )r3   rR   r   r   r   r6      s    zisidentifier.<locals>.<genexpr>rS   )rT   r<   bool_name_reri   )r   rU   r   r   r   rQ      s    c             C   s   |   S )N)rY   )rX   r   r   r   rY      s    c             C   s   |   S )N)r[   )rX   r   r   r   r[      s    c             C   s   t | |t|S )N)types
MethodTypetype)r   instancer   r   r   rv      s    rv   c             C   s   | S )Nr   )r   r   r   r   rm      s    c             C   s   | j S )z"Get a function's closure attribute)Zfunc_closure)re   r   r   r   rq      s    c             C   s   | j ddS )zu"{u}'abc'" --> "u'abc'" (Python 2)
        
        Accepts a string or a function, so it can be used as a decorator.r   )r   )rn   )r   r   r   r   ro     s    r/   c             C   sZ   |d k	r|n|}t |  d }t| tr6t| }n| }|p@t}t|||d|| d S )N
r]   )rM   r^   r`   r   unicodeunicode_to_strr_   r]   )ra   rb   rc   rd   
scripttextfilenamer   r   r   rf     s    

c             C   sx   t | tr| t }n| }dd ||gD }|d krJtj|f|  n*t|  	 d }t
|||d|| d S )Nc             S   s   g | ]}|d k	r|qS )Nr   )r3   nsr   r   r   r7      s    zexecfile.<locals>.<listcomp>ry   r]   )r   rz   r   r>   getfilesystemencodingrM   rf   r^   r`   rstripr]   )ra   rb   rc   rd   r}   wherer|   r   r   r   rf     s    
c                 s    st d fdd}|S )z5Python 3 compatible function annotation for Python 2.z1annotations must be provided as keyword argumentsc                s6   t | dr,x&  D ]\}}|| j|< qW n | _| S )N__annotations__)hasattrrW   r   )re   kv)kwargsr   r   dec,  s
    
zannotate.<locals>.dec)
ValueError)r   r   r   )r   r   annotate(  s    r   c             G   s   | d|i S )z%Create a base class with a metaclass.Z_NewBaser   )metabasesr   r   r   with_metaclassK  s    r   )N)N)N)N)N)r0   )F)NN)r0   )F)NNN)NNN)<r   r   r&   r>   reZshutilru   r   r   r   r
   r   r   r   r   r   r%   F_OKX_OKrK   r?   version_infoZpython_implementationr!   r{   Zstr_to_bytesZbytes_to_strZcast_bytes_py2Zcast_unicode_py2Zbuffer_to_bytes_py2r"   r   r   rz   ZPY3rN   Zbuiltin_mod_namerP   rM   ZwhichrQ   rangeZxrangerY   r[   getcwdrv   rf   r_   	MULTILINErk   rj   rm   ro   rq   rr   rt   Zgetcwdur   r   r   r   r   r   <module>   s   




?


	




#