
	Q[c           @   s   d  Z  d d l Z d d l Z d d l m Z d Z d   Z d   Z e j ra e	 Z
 d   Z n e Z
 e	 Z d   Z d	 e f d
     YZ d   Z d   Z d   Z d   Z d Z e d  Z e d  Z e j e  j Z d   Z e d k r n  d S(   s   
This is Victor Stinner's pure-Python implementation of PEP 383: the "surrogateescape" error
handler of Python 3.

Source: misc/python/surrogateescape.py in https://bitbucket.org/haypo/misc
iN(   t   utilst   surrogateescapec         C   s   t  j r |  S|  j d  Sd  S(   Nt   unicode_escape(   R    t   PY3t   decode(   t   text(    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   u   s    	c         C   s   t  j r |  j d  S|  Sd  S(   Nt   latin1(   R    R   t   encode(   t   data(    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   b   s    	c         C   s   t  |  f  S(   N(   t   bytes(   t   code(    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   <lambda>#   s    c         C   s   |  j  |  j |  j !} yF t |  t  r7 t |  } n$ t |  t  rU t |  } n |   Wn t k
 ru |   n X| |  j f S(   s   
    Pure Python implementation of the PEP 383: the "surrogateescape" error
    handler of Python 3. Undecodable bytes will be replaced by a Unicode
    character U+DCxx on decoding, and these are translated into the
    original bytes on encoding.
    (	   t   objectt   startt   endt
   isinstancet   UnicodeDecodeErrort   replace_surrogate_decodet   UnicodeEncodeErrort   replace_surrogate_encodet   NotASurrogateError(   t   exct   mystringt   decoded(    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   surrogateescape_handler(   s    

R   c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyR   C   s   c         C   s   g  } x |  D] } t  |  } d | k o6 d k n sD t  n  d | k o[ d k n rz | j t | d   q | d k r | j t | d   q t  q Wt   j |  S(   s   
    Returns a (unicode) string, not the more logical bytes, because the codecs
    register_error functionality expects this.
    i   i  i   i  (   t   ordR   t   appendt   _unichrt   strt   join(   R   R   t   chR   (    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyR   G   s    	
c         C   s   g  } x |  D] } t  | t  r+ | } n t |  } d | k oN d k n rm | j t d |   q | d k r | j t |   q t  q Wt   j |  S(   s$   
    Returns a (unicode) string
    i   i   i   i   (   R   t   intR   R   R   R   R    R!   (   t   mybytesR   R"   R   (    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyR   d   s    	
c         C   s  t  d k r g  } x t |   D] \ } } t |  } | d k  rR t |  } nK d | k oi d k n r t | d  } n t t  |  | | d d   | j |  q Wt   j |  St  d k rg  } x t |   D] \ } } t |  } d	 | k od
 k n rld | k o(d k n rMt | d  } | j |  qt t  |  | | d d   q | j d  } | j |  q Wt   j |  S|  j t  t	  Sd  S(   Nt   asciii   i  i  i   i   s   ordinal not in range(128)s   utf-8i   i  s   surrogates not allowed(
   t   FS_ENCODINGt	   enumerateR   t	   bytes_chrR   R   R   R!   R   t	   FS_ERRORS(   t   fnt   encodedt   indexR"   R   t   ch_utf8(    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   encodefilename}   s8    	c         C   s   |  j  t t  S(   N(   R   R&   R)   (   R*   (    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   decodefilename   s    R%   s   [abc]s   [abc\udcff]c           C   sF   t  j r d Sy t j t  Wn! t k
 rA t j t t  n Xd S(   sH   
    Registers the surrogateescape error handler on Python 2 (only)
    N(   R    R   t   codecst   lookup_errorR)   t   LookupErrort   register_errorR   (    (    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   register_surrogateescape   s    	t   __main__(   t   __doc__R0   t   syst   futureR    R)   R   R
   R   t   chrR   R(   t   unichrR   t	   ExceptionR   R   R   R.   R/   R&   R*   R+   t   lookupt   nameR4   R   (    (    (    s;   lib/python2.7/site-packages/future/utils/surrogateescape.pyt   <module>   s0   							'	  	