B
    \                 @   sr  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Zddlm	Z	 d1ddZ
d2ddZd3d	d
Zd4ddZd5ddZdd Zdd Zdd ZejejB dfddZdZd6ddZdZddlZe
Ze
ZeZeZe
Ze
Ze
Z e!fZ"e!Z#ej$Z$d7ddZ%e&Z'dd  Z(d!d" Z)ej*Z*ej+Z+d8d#d$Z,e-d%ej.Z/d&d' Z0ed(d) Z1d*d+ Z2e Z3e4 d,kZ5d-d. Z6d/d0 Z7dS )9zCompatibility tricks for Python 3. Mainly to do with unicode.

This file is deprecated and will be removed in a future version.
    N   )DEFAULT_ENCODINGc             C   s   | S )N )xencodingr   r   6lib/python3.7/site-packages/IPython/utils/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st| } | S )zCast a buffer object to bytes)r   r   )Zbufr   r   r   buffer_to_bytes&   s    
r   c                s   t   fdd}|S )Nc                s@   t | trd }| }n
| }|j}|d k	r. |}|r<||_|S |S )N)r   string_types__doc__)Z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 )aj  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_checkd   s    z$_shutil_which.<locals>._access_checkNPATHZ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   )r/   r0   )r1   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)r1   r)   r$   r*   ZpathextfilesseendirZnormdirZthefilenamer   )r1   r   _shutil_whichV   s8    






rG   Tr,   c             C   s
   t | S )N)builtin_modinput)promptr   r   r   rI      s    rI   builtinsFc             C   s$   |rt dd | dD S |  S )Nc             s   s   | ]}t |V  qd S )N)isidentifier)r/   ar   r   r   r2      s    zisidentifier.<locals>.<genexpr>.)allr8   rL   )r   Zdottedr   r   r   rL      s    rL   c             C   s   t |  S )N)iteritems)dr   r   r   	iteritems   s    rS   c             C   s   t |  S )N)rP   values)rR   r   r   r   
itervalues   s    rU   c          	   C   sJ   |d k	r|n|}t | d&}|p"t}t|| | d|| W d Q R X d S )Nrbexec)opencompilerW   read)fnameZglobZlocZcompilerfr   r   r   execfile   s    r]   z\bprint (?P<expr>.*)$c             C   s   |  d}d| S )Nexprz	print(%s))groups)matchr^   r   r   r   _print_statement_sub   s    
ra   c             C   s   | j ddS )zh"{u}'abc'" --> "'abc'" (Python 3)

    Accepts a string or a function, so it can be used as a decorator.r,   )r   )format)r   r   r   r   u_format   s    rc   c             C   s   | j S )z"Get a function's closure attribute)__closure__)r\   r   r   r   get_closure   s    re   ZPyPyc                 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__)hasattrrQ   rf   )r\   kv)kwargsr   r   dec   s
    
zannotate.<locals>.dec)
ValueError)rj   rk   r   )rj   r   annotate   s    rm   c             G   s   | d|i S )z%Create a base class with a metaclass.Z_NewBaser   )metabasesr   r   r   with_metaclass   s    rp   )N)N)N)N)N)r,   )F)NN)8r   r   r#   r:   reZshutiltypesr;   r   r   r   r
   r   r   r   r   r   r"   F_OKX_OKrG   ZPY3rI   Zbuiltin_mod_namerK   rH   r   Zunicode_to_strZstr_to_bytesZbytes_to_strZcast_bytes_py2Zcast_unicode_py2Zbuffer_to_bytes_py2r   r   r   ZwhichrL   rangeZxrangerS   rU   getcwd
MethodTyper]   rY   	MULTILINEZ_print_statement_rera   rc   re   ZPY2Zpython_implementationZPYPYrm   rp   r   r   r   r   <module>   s\   




?


#