U
    h                     @   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   N/var/www/html/venv/lib/python3.8/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 }z0t| 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 }| 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rg }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rd
krn nFd|  krdkrn nt|d }|| ntt | ||d dq|d}|| qt |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}   sF    
  
   
r9   c                 C   s   |  ttS r   )r   r2   r5   )r6   r   r   r   decodefilename   s    r:   r0   u   [abcÿ]u   [abc]c                   C   s>   t jr
dS z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_errorr5   LookupErrorregister_errorr   r   r   r   r   register_surrogateescape   s    r?   __main__)__doc__r;   sysfuturer   r5   r	   r   r   chrr(   r4   unichrr   	Exceptionr   r   r   r9   r:   r2   r6   r7   lookupnamer?   r    r   r   r   r   <module>   s0   	
'  