
BfU                 @   s  d  Z  d d l Z d d l Z d d l m Z d Z d d   Z d d   Z e j rj e	 Z
 d	 d
   Z n e Z
 e	 Z d d   Z Gd d   d e  Z d d   Z d d   Z d d   Z d d   Z d Z e d  Z e d  Z e j e  j Z d d   Z e d k rd S)z
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
    N)utilssurrogateescapec             C   s   t  j r |  S|  j d  Sd  S)NZunicode_escape)r   PY3decode)text r   >/usr/lib/python3/dist-packages/future/utils/surrogateescape.pyu   s    	r	   c             C   s   t  j r |  j d  S|  Sd  S)Nlatin1)r   r   encode)datar   r   r   b   s    	r   c             C   s   t  |  f  S)N)bytes)coder   r   r   <lambda>#   s    r   c             C   s   |  j  |  j |  j  } yF t |  t  r: t |  } n$ t |  t  rX t |  } n |   Wn t k
 ry |   Yn X| |  j f S)z
    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.
    )	objectstartend
isinstanceUnicodeDecodeErrorreplace_surrogate_decodeUnicodeEncodeErrorreplace_surrogate_encodeNotASurrogateError)excmystringdecodedr   r   r   surrogateescape_handler(   s    
r   c               @   s   e  Z d  Z d S)r   N)__name__
__module____qualname__r   r   r   r   r   C   s   r   c             C   s   g  } x |  D] } t  |  } d | k o6 d k n sA t  d | k oX d k n rw | j t | d   q | d k r | j t | d   q t  q Wt   j |  S)z
    Returns a (unicode) string, not the more logical bytes, because the codecs
    register_error functionality expects this.
    i   i  i   i  )ordr   append_unichrr   strjoin)r   r   chr   r   r   r   r   G   s    
r   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)z$
    Returns a (unicode) string
          i      )r   intr!   r"   r#   r   r$   r%   )Zmybytesr   r&   r   r   r   r   r   d   s    	
r   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)Nasciir'   i  i  i      zordinal not in range(128)zutf-8i   i  zsurrogates not allowed)
FS_ENCODING	enumerater!   	bytes_chrr   r"   r   r%   r   	FS_ERRORS)fnencodedindexr&   r   Zch_utf8r   r   r   encodefilename}   s8    	r4   c             C   s   |  j  t t  S)N)r   r-   r0   )r1   r   r   r   decodefilename   s    r5   r+   u   [abcÿ]u   [abc]c               C   sG   t  j r d Sy t j t  Wn" t k
 rB t j t t  Yn Xd S)zH
    Registers the surrogateescape error handler on Python 2 (only)
    N)r   r   codecslookup_errorr0   LookupErrorregister_errorr   r   r   r   r   register_surrogateescape   s    	r:   __main__)__doc__r6   sysZfuturer   r0   r	   r   r   chrr#   r/   Zunichrr   	Exceptionr   r   r   r4   r5   r-   r1   r2   lookupnamer:   r   r   r   r   r   <module>   s0   	'  