B
    1Ib                 @   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rHe	Z
d	d
 ZneZ
e	Zdd ZG d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ejZdd Zedkrd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 | dS d S )NZunicode_escape)r   PY3decode)text r   ;lib/python3.7/site-packages/future/utils/surrogateescape.pyu   s    r	   c             C   s   t jr| dS | S d S )Nlatin1)r   r   encode)datar   r   r   b   s    
r   c             C   s
   t | fS )N)bytes)coder   r   r   <lambda>#       r   c             C   sf   | j | j| j }y0t| tr(t|}nt| tr<t|}n| W n tk
rZ   | Y n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 }xz| D ]r}t |}d|  kr*dks0n td|  krDdkr\n n|t|d  q
|dkrx|t|d  q
tq
W t |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_unichrstrjoin)r   r   chr   r   r   r   r   G   s    
r   c             C   s~   g }xl| D ]d}t |tr|}nt|}d|  kr:dkrRn n|td|  q
|dkrj|t| q
tq
W t |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   sJ  t dkrg }xrt| D ]f\}}t|}|dk r8t|}n:d|  krLdkr^n nt|d }ntt | ||d d|| qW t |S t dkr:g }xt| D ]\}}t|}d	|  krd
krn nHd|  krdkr n nt|d }|| ntt | ||d dq|d}|| qW t |S | t t	S d 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    



r5   c             C   s   |  ttS )N)r   r.   r1   )r2   r   r   r   decodefilename   s    r6   r,   u   [abcÿ]u   [abc]c               C   s>   t jr
dS ytt W n  tk
r8   ttt Y nX dS )zH
    Registers the surrogateescape error handler on Python 2 (only)
    N)r   r   codecslookup_errorr1   LookupErrorregister_errorr   r   r   r   r   register_surrogateescape   s    r;   __main__)__doc__r7   sysZfuturer   r1   r	   r   r   chrr$   r0   Zunichrr   	Exceptionr   r   r   r5   r6   r.   r2   r3   lookupnamer;   r   r   r   r   r   <module>   s0   
'  