
    X:*                     >   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 d,dZ	d,dZ
d,dZd,dZd,d	Zd
 Zd Zd Zej"                  ej$                  z  dfdZddlZej*                  d   dk\  s ej,                         dk(  re	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\  rtdZ"d-dZ#dZ$ddl%Z&ejN                  Z'd.dZ(e)Z*d Z+d Z,ejZ                  Z-ej\                  Z.d/dZ/ ej`                  dejb                        Z2d Z3ed        Z4ed        Z5d Z6nedZ"d-dZ#dZ$ddl7Z&ddlZ ej`                  d      Z8d.d Z(e*Z*d! Z+d" Z,ejr                  Z-d# Z.d$ Z4d% Z6eZ'ed&        Z5ej(                  d'k(  rd0d(Z/nd0d)Z/d* Z:d+ Z;y)1z=Compatibility tricks for Python 3. Mainly to do with unicode.    N   )DEFAULT_ENCODINGc                     | S N )xencodings     </usr/lib/python3/dist-packages/ipython_genutils/py3compat.pyno_coder      s    H    c                 :    |xs t         }| j                  |d      S Nreplace)r   decodesr	   s     r
   r   r          ++H88Hi((r   c                 :    |xs t         }| j                  |d      S r   )r   encode)ur	   s     r
   r   r      r   r   c                 >    t        | t              rt        | |      S | S r   )
isinstancebytesr   r   s     r
   cast_unicoder      s    !Ua""Hr   c                 >    t        | t              st        | |      S | S r   )r   r   r   r   s     r
   
cast_bytesr      s    aa""Hr   c                 |    t        | t              r| j                         S t        | t              st        |       S | S )z+Cast a buffer or memoryview object to bytes)r   
memoryviewtobytesr   )bufs    r
   buffer_to_bytesr!   "   s1    #z"{{}c5!SzJr   c                 B     t        j                          fd       }|S )Nc                 t    t        | t              rd }| }n| }|j                  } |      }|r	||_        |S |S r   )r   string_types__doc__)func_or_strfuncdocstr_change_funcs      r
   wrapperz)_modify_str_or_docstring.<locals>.wrapper+   sC    k<0DCD,,Cc"DLK
r   )	functoolswraps)r)   r*   s   ` r
   _modify_str_or_docstringr-   *   s%    ___% & Nr   c                     	 t        |       S # t        $ r Y nw xY w	 t        t        |             S # t        $ r Y nw xY w	 t        t	        |             S # t        $ r Y yw xY w)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)es    r
   safe_unicoder5   <   sr    A c!f%% d1g&& *s*   
 	1 	==A 	A! A!c                 h    d }t         j                  j                         r | |      r S y|.t         j                  j	                  dt         j
                        }|sy|j                  t         j                        }t        j                  dk(  rt         j                  |vr |j                  dt         j                         t         j                  j	                  dd      j                  t         j                        }t         fd|D              r g}n|D cg c]  } |z   	 }}n g}t               }|D ]m  }t         j                  j                  |      }	|	|vs'|j                  |	       |D ]1  }
t         j                  j!                  ||
      } |||      s-|c c S  o yc c}w )	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                     t         j                  j                  |       xr8 t        j                  | |      xr  t         j                  j	                  |        S r   )ospathexistsaccessisdir)fnmodes     r
   _access_checkz$_shutil_which.<locals>._access_check`   sA    r" *ryyT': *b))	+r   NPATHwin32r   PATHEXT c              3   x   K   | ]1  }j                         j                  |j                                3 y wr   )lowerendswith).0extcmds     r
   	<genexpr>z _shutil_which.<locals>.<genexpr>}   s(     DSsyy{##CIIK0Ds   7:)r8   r9   dirnameenvirongetdefpathsplitpathsepsysplatformcurdirinsertanysetnormcaseaddjoin)rI   r>   r9   r?   pathextfilesrH   seendirnormdirthefilenames   `           r
   _shutil_whichra   R   s`   + 
wwsd#J|zz~~fbjj1::bjj!D
||wyyD KK299% **..B/55bjjA
 DGDDEE*123S3Y2E2 5D  ''""3'$HHW   ww||C1 t,K 	   3s    F/   
IronPythonTc                 ,    t        j                  |       S r   )builtin_modinputprompts    r
   rf   rf      s      ((r   builtinsFc                 h    |r!t        d | j                  d      D              S | j                         S )Nc              3   2   K   | ]  }t        |        y wr   isidentifierrG   as     r
   rJ   zisidentifier.<locals>.<genexpr>        =1|A=   .)allrO   rm   r   dotteds     r
   rm   rm      s+    ====~~r   c                 4    t        | j                               S r   )iteritemsds    r
   	iteritemsr{      s    T!'')_,r   c                 4    t        | j                               S r   )rw   valuesry   s    r
   
itervaluesr~      s    d188:..r   c                     ||n|}t        | d      5 }|xs t        }t         ||j                         | d      ||       d d d        y # 1 sw Y   y xY w)Nrbexec)opencompiler   read)fnameglobloccompilerfs        r
   execfiler      sS    ocD% 	?!*7H!&&(E62D#>	? 	? 	?s   .A

Az\bprint (?P<expr>.*)$c                 .    | j                  d      }d|z  S )Nexprz	print(%s))groups)matchr   s     r
   _print_statement_subr      s    ||F#T!!r   c                 6    t         j                  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subr   )r(   s    r
   doctest_refactor_printr      s     #&&';SAAr   c                 &    | j                  d      S )zt"{u}'abc'" --> "'abc'" (Python 3)
        
        Accepts a string or a function, so it can be used as a decorator.rC   r   formatr   s    r
   u_formatr      s    
 xx"x~r   c                     | j                   S z"Get a function's closure attribute)__closure__r   s    r
   get_closurer      s    }}r   c                 ,    t        j                  |       S r   )re   	raw_inputrg   s    r
   rf   rf      s    $$V,,r   __builtin__z[a-zA-Z_][a-zA-Z0-9_]*$c                     |r!t        d | j                  d      D              S t        t        j	                  |             S )Nc              3   2   K   | ]  }t        |        y wr   rl   rn   s     r
   rJ   zisidentifier.<locals>.<genexpr>   rp   rq   rr   )rs   rO   bool_name_rer   rt   s     r
   rm   rm      s2    ====HNN1%&&r   c                 "    | j                         S r   )r{   ry   s    r
   r{   r{      s    Q[[]*r   c                 "    | j                         S r   )r~   ry   s    r
   r~   r~      s    alln,r   c                 B    t        j                  | |t        |            S r   )types
MethodTypetype)r'   instances     r
   r   r      s    hX??r   c                     | S r   r   )r&   s    r
   r   r      s    r   c                     | j                   S r   )func_closurer   s    r
   r   r      s    ~~r   c                 &    | j                  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   r   r   s    r
   r   r     s    
 xx#xr   rA   c                     ||n|}t        j                  |       j                         dz   }t        | t              rt        |       }n| }|xs t        }t         |||d      ||       y N
r   )re   r   r   r   unicodeunicode_to_strr   r   )r   r   r   r   
scripttextfilenames         r
   r   r     se    /#C$))%0557=J %))%0 *7H*h7sCr   c                 f   t        | t              r$| j                  t        j                               }n| }||fD cg c]  }||	 }}|t        j                  |g|  y t        j                  |       j                         j                         dz   }t         |||d      ||       y c c}w r   )r   r   r   rQ   getfilesystemencodingre   r   r   r   rstripr   )r   r   r   r   r   nswherer   s           r
   r   r     s    %) <<(A(A(CD #'+@BR@E@$$X66(--e499;BBDtK
Xj(F;T3G As   B.B.c                  ,      st        d       fd}|S )z5Python 3 compatible function annotation for Python 2.z1annotations must be provided as keyword argumentsc                     t        | d      r)j                         D ]  \  }}|| j                  |<    | S | _        | S )N__annotations__)hasattrrx   r   )r   kvkwargss      r
   deczannotate.<locals>.dec,  sM    1'( )1'(!!!$)  !'Ar   )
ValueError)r   r   s   ` r
   annotater   (  s    LMM Jr   c                      | d|i       S )z%Create a base class with a metaclass._NewBaser   )metabasess     r
   with_metaclassr   K  s    
E2&&r   r   )rC   )F)NN)NNN)<r%   r+   r8   rQ   reshutilr   r	   r   r   r   r   r   r   r!   r-   r5   F_OKX_OKra   rR   version_infopython_implementationr1   r   str_to_bytesbytes_to_strcast_bytes_py2cast_unicode_py2buffer_to_bytes_py2r2   r$   r/   r   PY3rf   builtin_mod_nameri   re   whichrm   rangexranger{   r~   getcwdr   r   r   	MULTILINEr   r   r   r   r   r   r   getcwdur   r   r   r   r
   <module>r      s.   C  	 
 	   &))


$+, GGbgg-D =~ A!=x==?<ONNLLN!6LLNNLLN#)>LLA!
C) ""LLE 
 F,.YYF!!J? %"**%=r||L" B B  
 C- %%rzz45H'
 F*,ZZF@ E   ||w
	D
	HF'r   