3
0c                 @   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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 | jdS d S )NZunicode_escape)r   PY3decode)text r   [/var/www/html/my_flask_app/venv/lib/python3.6/site-packages/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rg }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 }xt| D ]\}}t|}d	|  kod
kn  rd|  kodkn  r t|d }|j| ntt | ||d dq|jd}|j| qW 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   
'  