B
    	Q˜[Ñ  ã               @   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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 |  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>#   s    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rŒg }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 }x’t| ƒD ]†\}}t|ƒ}d	|  krÊd
krn nHd|  krädkr n nt|d ƒ}| |¡ ntt | ||d dƒ‚q¤| d¡}| |¡ q¤W 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    



r4   c             C   s   |   tt¡S )N)r   r-   r0   )r1   r   r   r   Údecodefilename¤   s    r5   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_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   
'  