B
    kbx                 @   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Zddlm	Z	 d%ddZ
d&ddZd'd	d
Zd(ddZdd Zdd Zdd ZejejB dfddZdZd)ddZdZddlZejZd*ddZejZejZd+ddZedejZ ed d! Z!e Z"e# d"kZ$d,d#d$Z%e% Z&Z'dS )-zCompatibility 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   |pt }| |dS )Nreplace)r   decode)sencoding r   6lib/python3.7/site-packages/IPython/utils/py3compat.pyr      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                sB   t | tfrd }| }n
| }|j}|d k	r0 |}|r>||_|S |S )N)r   str__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   s@   yt | S  tk
r   Y nX y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)r   UnicodeError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_check]   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>z   s    z _shutil_which.<locals>.<genexpr>c                s   g | ]} | qS r   r   )r*   r+   )r,   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)r,   r$   r   r%   ZpathextfilesseendirZnormdirZthefilenamer   )r,   r	   _shutil_whichO   s8    






rB   Tr'   c             C   s
   t | S )N)builtin_modinput)promptr   r   r	   rD      s    rD   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	   r-      s    zisidentifier.<locals>.<genexpr>.)allr3   rG   )r   Zdottedr   r   r	   rG      s    rG   c          	   C   sJ   |d k	r|n|}t | d&}|p"t}t|| | d|| W d Q R X d S )Nrbexec)opencompilerL   read)fnameZglobZlocZcompilerfr   r   r	   execfile   s    rR   z\bprint (?P<expr>.*)$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    rT   ZPyPyc             C   s   | S )Nr   )xr   r   r   r	   no_code   s    rV   )N)N)N)N)r'   )F)NN)N)(r   r   r   r5   reZshutiltypesr6   r   r   r   r
   r   r   r   r   r   F_OKX_OKrB   ZPY3rD   Zbuiltin_mod_namerF   rC   whichrG   getcwd
MethodTyperR   rN   	MULTILINEZ_print_statement_rerT   ZPY2Zpython_implementationZPYPYrV   Zunicode_to_strZcast_bytes_py2r   r   r   r	   <module>   s>   



?



