a
    Wä%bÑ  ã                   @   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 )NÚunicode_escape)r   ÚPY3Údecode)Útext© r   úl/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/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   sd   | j | j| j… }z0t| tƒr(t|ƒ}nt| tƒr<t|ƒ}n| ‚W n tyX   | ‚Y n0 || 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 }| D ]r}t |ƒ}d|  kr(dks.n t‚d|  krBdkrZn n| t|d ƒ¡ q|dkrv| t|d ƒ¡ qt‚qtƒ  |¡S )zƒ
    Returns a (unicode) string, not the more logical bytes, because the codecs
    register_error functionality expects this.
    é Ø  éÿÜ  é Ü  iÜ  )Úordr   ÚappendÚ_unichrÚstrÚjoin)r   r   Úchr   r   r   r	   r   G   s    r   c                 C   sz   g }| D ]d}t |tƒr|}nt|ƒ}d|  kr8dkrPn n| td| ƒ¡ q|dkrh| t|ƒ¡ qt‚qtƒ  |¡S )z$
    Returns a (unicode) string
    é€   éÿ   r&   é   )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 }t| ƒD ]f\}}t|ƒ}|dk r6t|ƒ}n:d|  krJdkr\n nt|d ƒ}ntt | ||d dƒ‚| |¡ qtƒ  |¡S t dkr0g }t| ƒD ]„\}}t|ƒ}d	|  krÄd
krn nFd|  krÜdkrøn nt|d ƒ}| |¡ ntt | ||d dƒ‚qž| d¡}| |¡ qžtƒ  |¡S |  t t	¡S d S )NÚasciir-   i€Ü  r%   r&   é   zordinal not in range(128)zutf-8r$   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}   s<    

þ
þ
r:   c                 C   s   |   tt¡S r   )r   r3   r6   )r7   r   r   r	   Údecodefilename¤   s    r;   r1   u   [abcÃ¿]u   [abcí³¿]c                   C   s<   t jr
dS zt t¡ W n ty6   t tt¡ Y n0 dS )zH
    Registers the surrogateescape error handler on Python 2 (only)
    N)r   r   ÚcodecsÚlookup_errorr6   ÚLookupErrorÚregister_errorr    r   r   r   r	   Úregister_surrogateescape±   s    r@   Ú__main__)Ú__doc__r<   ÚsysÚfuturer   r6   r
   r   r   Úchrr)   r5   Úunichrr    Ú	Exceptionr   r   r   r:   r;   r3   r7   r8   ÚlookupÚnamer@   r!   r   r   r   r	   Ú<module>   s,   	
'