ó
@×ÆXc           @   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 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 Bd 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 rDe# Z$ d d „ Z% d Z& d d l' Z( e j) Z) e* d „ Z+ e, Z- d „  Z. d „  Z/ e j0 Z0 e j1 Z1 d d d „ Z2 e j3 d e j4 ƒ Z5 d „  Z6 e d „  ƒ Z7 e d „  ƒ Z8 d „  Z9 nÒ e* Z$ d d „ Z% d Z& d d l: Z( d d l Z e j3 d ƒ Z; e* d  „ Z+ e- Z- d! „  Z. d" „  Z/ e j< Z0 d# „  Z1 d$ „  Z7 d% „  Z9 e Z) e d& „  ƒ Z8 e j d' k rd d d d( „ Z2 n d d d d) „ Z2 d* „  Z= d+ „  Z> d S(,   s=   Compatibility tricks for Python 3. Mainly to do with unicode.iÿÿÿÿNi   (   t   DEFAULT_ENCODINGc         C   s   |  S(   N(    (   t   xt   encoding(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   no_code   s    c         C   s   | p	 t  } |  j | d ƒ S(   Nt   replace(   R    t   decode(   t   sR   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyR      s    c         C   s   | p	 t  } |  j | d ƒ S(   NR   (   R    t   encode(   t   uR   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyR      s    c         C   s    t  |  t ƒ r t |  | ƒ S|  S(   N(   t
   isinstancet   bytesR   (   R   R   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   cast_unicode   s    c         C   s    t  |  t ƒ s t |  | ƒ S|  S(   N(   R	   R
   R   (   R   R   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt
   cast_bytes   s    c         C   s6   t  |  t ƒ r |  j ƒ  St  |  t ƒ s2 t |  ƒ S|  S(   s+   Cast a buffer or memoryview object to bytes(   R	   t
   memoryviewt   tobytesR
   (   t   buf(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   buffer_to_bytes"   s
    

c            s"   t  j ˆ  ƒ ‡  f d †  ƒ } | S(   Nc            sP   t  |  t ƒ r d  } |  } n |  } | j } ˆ  | ƒ } | rL | | _ | S| S(   N(   R	   t   string_typest   Nonet   __doc__(   t   func_or_strt   funct   doc(   t   str_change_func(    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   wrapper+   s    			(   t	   functoolst   wraps(   R   R   (    (   R   s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   _modify_str_or_docstring*   s    c         C   sv   y t  |  ƒ SWn t k
 r! n Xy t t |  ƒ ƒ SWn t k
 rI n Xy t t |  ƒ ƒ SWn t k
 rq n Xd S(   sl   unicode(e) with various fallbacks. Used for exceptions, which may not be
    safe to call unicode() on.
    u   Unrecoverably corrupt evalue(   t   unicode_typet   UnicodeErrort   str_to_unicodet   strt   repr(   t   e(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   safe_unicode<   s    c            s   d „  } t  j j ˆ  ƒ r2 | ˆ  | ƒ r. ˆ  Sd S| d k rY t  j j d t  j ƒ } n  | sc d S| j t  j ƒ } t	 j
 d k rt  j | k r© | j d t  j ƒ n  t  j j d d ƒ j t  j ƒ } t ‡  f d †  | Dƒ ƒ rò ˆ  g } qg  | D] } ˆ  | ^ qù } n	 ˆ  g } t ƒ  } xu | D]m } t  j j | ƒ }	 |	 | k r+| j |	 ƒ x9 | D]. }
 t  j j | |
 ƒ } | | | ƒ rc| SqcWq+q+Wd S(	   sn  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   s5   t  j j |  ƒ o4 t  j |  | ƒ o4 t  j j |  ƒ S(   N(   t   ost   patht   existst   accesst   isdir(   t   fnt   mode(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   _access_check`   s    $t   PATHt   win32i    t   PATHEXTt    c         3   s*   |  ]  } ˆ  j  ƒ  j | j  ƒ  ƒ Vq d  S(   N(   t   lowert   endswith(   t   .0t   ext(   t   cmd(    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pys	   <genexpr>}   s    N(   R#   R$   t   dirnameR   t   environt   gett   defpatht   splitt   pathsept   syst   platformt   curdirt   insertt   anyt   sett   normcaset   addt   join(   R3   R)   R$   R*   t   pathextt   filesR2   t   seent   dirt   normdirt   thefilet   name(    (   R3   s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   _shutil_whichR   s8    	! 		i    i   t
   IronPythonR.   c         C   s   t  j |  ƒ S(   N(   t   builtin_modt   input(   t   prompt(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRM   ®   s    t   builtinsc         C   s-   | r# t  d „  |  j d ƒ Dƒ ƒ S|  j ƒ  S(   Nc         s   s   |  ] } t  | ƒ Vq d  S(   N(   t   isidentifier(   R1   t   a(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pys	   <genexpr>¸   s    t   .(   t   allR8   RP   (   R   t   dotted(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRP   ¶   s    c         C   s   t  |  j ƒ  ƒ S(   N(   t   itert   items(   t   d(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt	   iteritems¼   s    c         C   s   t  |  j ƒ  ƒ S(   N(   RU   t   values(   RW   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt
   itervalues½   s    c         B   s\   | d  k	 r | n | } e |  d ƒ / } | p3 e } | | j ƒ  |  d ƒ | | UWd  QXd  S(   Nt   rbt   exec(   R   t   opent   compilet   read(   t   fnamet   globt   loct   compilert   f(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   execfileÂ   s    s   \bprint (?P<expr>.*)$c         C   s   |  j  d ƒ } d | S(   Nt   exprs	   print(%s)(   t   groups(   t   matchRf   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   _print_statement_subÊ   s    c         C   s   t  j t |  ƒ S(   sÏ   Refactor '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.(   t   _print_statement_ret   subRi   (   R   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   doctest_refactor_printÎ   s    c         C   s   |  j  d d ƒ S(   st   "{u}'abc'" --> "'abc'" (Python 3)
        
        Accepts a string or a function, so it can be used as a decorator.R   R.   (   t   format(   R   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   u_format×   s    c         C   s   |  j  S(   s"   Get a function's closure attribute(   t   __closure__(   Rd   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   get_closureÞ   s    c         C   s   t  j |  ƒ S(   N(   RL   t	   raw_input(   RN   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRM   ç   s    t   __builtin__s   [a-zA-Z_][a-zA-Z0-9_]*$c         C   s6   | r# t  d „  |  j d ƒ Dƒ ƒ St t j |  ƒ ƒ S(   Nc         s   s   |  ] } t  | ƒ Vq d  S(   N(   RP   (   R1   RQ   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pys	   <genexpr>ñ   s    RR   (   RS   R8   t   boolt   _name_reRh   (   R   RT   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRP   ï   s    c         C   s
   |  j  ƒ  S(   N(   RX   (   RW   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRX   õ   s    c         C   s
   |  j  ƒ  S(   N(   RZ   (   RW   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRZ   ö   s    c         C   s   t  j |  | t | ƒ ƒ S(   N(   t   typest
   MethodTypet   type(   R   t   instance(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRv   ù   s    c         C   s   |  S(   N(    (   R   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRl   ü   s    c         C   s   |  j  S(   s"   Get a function's closure attribute(   t   func_closure(   Rd   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRp   ÿ   s    c         C   s   |  j  d d ƒ S(   su   "{u}'abc'" --> "u'abc'" (Python 2)
        
        Accepts a string or a function, so it can be used as a decorator.R   (   Rm   (   R   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRn     s    R,   c         B   s{   | d  k	 r | n | } e j |  ƒ j ƒ  d } e |  e ƒ rO e |  ƒ } n |  } | p^ e } | | | d ƒ | | Ud  S(   Ns   
R\   (   R   RL   R]   R_   R	   t   unicodet   unicode_to_strR^   (   R`   Ra   Rb   Rc   t
   scripttextt   filename(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRe     s    c         B   s°   e  |  e ƒ r' |  j e j ƒ  ƒ } n |  } g  | | g D] } | d  k	 r: | ^ q: } | d  k rw e j | | Œ n5 e j |  ƒ j	 ƒ  j
 ƒ  d } | | | d ƒ | | Ud  S(   Ns   
R\   (   R	   Rz   R   R:   t   getfilesystemencodingR   RL   Re   R]   R_   t   rstrip(   R`   Ra   Rb   Rc   R}   t   nst   whereR|   (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyRe     s    +c             s(   ˆ  s t  d ƒ ‚ n  ‡  f d †  } | S(   s5   Python 3 compatible function annotation for Python 2.s1   annotations must be provided as keyword argumentsc            sI   t  |  d ƒ r< x3 ˆ  j ƒ  D] \ } } | |  j | <q Wn	 ˆ  |  _ |  S(   Nt   __annotations__(   t   hasattrRV   R‚   (   Rd   t   kt   v(   t   kwargs(    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   dec,  s
    	(   t
   ValueError(   R†   R‡   (    (   R†   s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   annotate(  s    c         G   s   |  d | i  ƒ S(   s%   Create a base class with a metaclass.t   _NewBase(    (   t   metat   bases(    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   with_metaclassK  s    (?   R   R   R#   R:   t   ret   shutilRu   R   R    R   R   R   R   R   R   R   R   R"   t   F_OKt   X_OKRJ   R;   t   version_infot   python_implementationR   R{   t   str_to_bytest   bytes_to_strt   cast_bytes_py2t   cast_unicode_py2t   buffer_to_bytes_py2R   R   R   Rz   t   Truet   PY3RM   t   builtin_mod_nameRO   RL   t   whicht   FalseRP   t   ranget   xrangeRX   RZ   t   getcwdRv   Re   R^   t	   MULTILINERj   Ri   Rl   Rn   Rp   Rr   Rt   t   getcwduR‰   R   (    (    (    s9   lib/python2.7/site-packages/ipython_genutils/py3compat.pyt   <module>   s–   			?%																#