3
‡ŒðiÑ  ã               @   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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ƒS d S )NZunicode_escape)r   ÚPY3Údecode)Útext© r   ú>/tmp/pip-build-6m2fasei/future/future/utils/surrogateescape.pyÚu   s    r	   c             C   s   t jr| j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 }x|| D ]t}t |ƒ}d|  ko(dkn  s2t‚d|  koDdkn  r^|jt|d ƒƒ q
|dkrz|jt|d ƒƒ q
t‚q
W tƒ 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Ú_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o8dkn  rR|jtd| ƒƒ q
|dkrj|jt|ƒƒ q
t‚q
W tƒ 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   sJ  t dkrŒg }xrt| ƒD ]f\}}t|ƒ}|dk r8t|ƒ}n:d|  koJdkn  r^t|d ƒ}ntt | ||d dƒ‚|j|ƒ qW tƒ j|ƒS t dk�r:g }x’t| ƒD ]†\}}t|ƒ}d	|  koÆd
kn  �rd|  koàdkn  �r t|d ƒ}|j|ƒ ntt | ||d dƒ‚q¤|jdƒ}|j|ƒ q¤W tƒ j|ƒS | j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   | j 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jtƒ W n  tk
r8   tj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   ÚsysÚfuturer   r0   r	   r   r   Úchrr#   r/   Úunichrr   Ú	Exceptionr   r   r   r4   r5   r-   r1   r2   ÚlookupÚnamer:   r   r   r   r   r   Ú<module>   s0   
'  