o
    Ri                     @   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r$e	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 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 )Nunicode_escape)r   PY3decode)text r   S/home/air/SG/back/venv/lib/python3.10/site-packages/future/utils/surrogateescape.pyu   s   
r
   c                 C   s   t jr| dS | 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   s`   | j | j| j }zt| trt|}nt| trt|}n| W n	 ty*   | w || 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 ]9}t |}d|  krdkst td|  kr"dkr.n n
|t|d  q|dkr<|t|d  qt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   sx   g }| D ]1}t |tr|}nt|}d|  krdkr(n n
|td|  q|dkr4|t| qtt |S )z$
    Returns a (unicode) string
          r%      )r   intr&   r'   r(   r   r)   r*   )mybytesr   r+   r   r   r   r	   r   d   s   
r   c                 C   s6  t dkrDg }t| D ]3\}}t|}|dk rt|}nd|  kr%dkr.n nt|d }n
tt | ||d d|| q
t |S t dkrg }t| D ]@\}}t|}d	|  kr`d
krn n"d|  krldkrzn nt|d }|| qNtt | ||d d|d}|| qNt |S | t t	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   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 dS  ty   ttt Y dS w )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   rA   __main__)__doc__r=   sysfuturer   r6   r
   r   r   chrr(   r5   unichrr   	Exceptionr   r   r   r;   r<   r3   r7   r8   lookupnamerA   r    r   r   r   r	   <module>   s0    	
'