
    h                     "   d Z ddlZddlZddlZej                  dkD  Z ej                         dk(  ZdZere	Z
d ZneZ
e	Zd Z G d d	e      Zd
 Zd Zd Zd Z ej(                         ZeresdZ ej,                  e      j.                  Zd Zy)a  
This is Victor Stinner's pure-Python implementation of PEP 383: the "surrogateescape" error
handler of Python 3.

Scissored from the python-future module to avoid 4.4MB of additional dependencies:
https://github.com/PythonCharmers/python-future/blob/e12549c42ed3a38ece45b9d88c75f5f3ee4d658d/src/future/utils/surrogateescape.py

Original source: misc/python/surrogateescape.py in https://bitbucket.org/haypo/misc
    N)   Windowssurrogateescapec                     t        | f      S N)bytes)codes    U/jellyfin/media/venv/lib/python3.12/site-packages/copyparty/stolen/surrogateescape.py<lambda>r      s    UD7^     c                     | j                   | j                  | j                   }	 t        | t              rt        |      }nt        | t              rt        |      }n| || j                  fS # t        $ r | w xY w)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decodeds      r
   surrogateescape_handlerr      sy     zz#))cgg.Hc-..x8G/0 /x8GI SWW  	s   :A- -A9c                       e Zd Zy)r   N)__name__
__module____qualname__ r   r
   r   r   :   s    r   r   c                 4   g }| D ]y  }t        |      }d|cxk  r
dk  st         t        d|cxk  rdk  r!n n|j                  t        |dz
               R|dk  r|j                  t        |dz
               ut         t	               j                  |      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	   s       r
   r   r   >   s    
 G %2w ''$$ ($$
 T#V#NN74&=12V^NN74&=12$$!%" 5::gr   c                 $   g }| D ]q  }t        |t              r|}nt        |      }d|cxk  rdk  r!n n|j                  t	        d|z                M|dk  r|j                  t	        |             mt
         t               j                  |      S )z$
    Returns a (unicode) string
          r#      )r   intr$   r%   r&   r   r'   r(   )mybytesr   r)   r	   s       r
   r   r   X   s     G % b#Dr7D44NN76D=12T\NN74=)$$% 5::gr   c                    t         dk(  rg }t        |       D ]e  \  }}t        |      }|dk  rt        |      }n3d|cxk  rdk  rn nt        |dz
        }nt	        t         | ||dz   d      |j                  |       g t               j                  |      S t         dk(  rg }t        |       D ]  \  }}t        |      }d	|cxk  rd
k  rGn nDd|cxk  rdk  r#n n t        |dz
        }|j                  |       Mt	        t         | ||dz   d      |j                  d      }|j                  |        t               j                  |      S | j                  t         t              S )Nasciir+   i  r"   r#      zordinal not in range(128)utf-8r!   i  zsurrogates not allowed)
FS_ENCODING	enumerater$   	bytes_chrr   r%   r   r(   encode	FS_ERRORS)fnencodedindexr)   r	   ch_utf8s         r
   encodefilenamer=   m   sZ   g "2 
	IE2r7Dczt_4)6)tf}-(UEAI7R  NN2
	 w||G$$		 "2 	(IE2r7D''T+V+"4&=1BNN2&,#R	;S  ))G,w'	( w||G$$yyi00r   c                 6    | j                  t        t              S r   )decoder4   r8   )r9   s    r
   decodefilenamer@      s    99[),,r   r3   c                      t         ry	 t        j                  t               y# t        $ r! t        j
                  t        t               Y yw xY w)zH
    Registers the surrogateescape error handler on Python 2 (only)
    N)PY3codecslookup_errorr8   LookupErrorregister_errorr   r   r   r
   register_surrogateescaperG      s@     BI& Bi)@ABs   # 'AA)__doc__rC   platformsysversion_inforB   systemWINDOWSr8   chrr&   r6   unichrr   	Exceptionr   r   r   r=   r@   getfilesystemencodingr4   lookupnamerG   r   r   r
   <module>rT      s      
	
(//
y
(	G+IGI6	 	4*%1P- (c'') 3K
 fmmK(--	Br   