
     Ic!                        d Z ddlZddlZ	 ddlZ	  ej                    d         p ej                    d         Zn$# e$ rZdej	        v rdZndZY dZ[ndZ[w dZY nxY w	  ej
        epd           n# e$ r dZY nw xY w# e$ r dZY nw xY wej        dk    reZ G d d	e          Z G d
 de          Z G d de          ZdS )u  
Provisional module to handle Exceptions across Python versions.

This module will be deprecated with the end of support for Python 2.7
and be removed in Docutils 1.2.

Error reporting should be safe from encoding/decoding errors.
However, implicit conversions of strings and exceptions like

>>> u'%s world: %s' % ('Hällo', Exception(u'Hällo')

fail in some Python versions:

* In Python <= 2.6, ``unicode(<exception instance>)`` uses
  `__str__` and fails with non-ASCII chars in`unicode` arguments.
  (work around http://bugs.python.org/issue2517):

* In Python 2, unicode(<exception instance>) fails, with non-ASCII
  chars in arguments. (Use case: in some locales, the errstr
  argument of IOError contains non-ASCII chars.)

* In Python 2, str(<exception instance>) fails, with non-ASCII chars
  in `unicode` arguments.

The `SafeString`, `ErrorString` and `ErrorOutput` classes handle
common exceptions.
    N   zunknown locale: UTF-8zUTF-8    r   c                   *    e Zd ZdZ	 	 ddZd Zd ZdS )	
SafeStringzG
    A wrapper providing robust conversion to `str` and `unicode`.
    Nbackslashreplacereplacec                 r    || _         |pt          |dd           pt          pd| _        || _        || _        d S )Nencodingascii)datagetattrlocale_encodingr   encoding_errorsdecoding_errors)selfr   r   r   r   s        >lib/python3.11/site-packages/docutils/utils/error_reporting.py__init__zSafeString.__init__R   sL    	! 4WT:t%D%D 4(4,3 	..    c                     	 t           j                  S # t          $ r t           j        t                    r/ fd j        j        D             }d                    |          cY S t           j        t                    r@t          j	        dk    r	 j        cY S  j        
                     j         j                  cY S  w xY w)Nc           	      `    g | ]*}t          t          |j        j                            +S  )strr   r   r   .0argr   s     r   
<listcomp>z&SafeString.__str__.<locals>.<listcomp>`   sK     3 3 3 JsDM(,(<> > ? ? 3 3 3r   , r   )r   r   UnicodeEncodeError
isinstance	Exceptionargsjoinunicodesysversion_infoencoder   r   )r   r#   s   ` r   __str__zSafeString.__str__[   s    	ty>>!! 	 	 	$)Y// '3 3 3 3#'9>3 3 3 yy&&&$)W-- B#f, B9$$$9++DM,0,@B B B B B	s    AC*1C%CCc                     	 t           j                  }t           j        t                    r|                    dd          }|S # t
          $ r}t           j        t                    rad j        j        dt           j        j         j	         j
                  dt           j        j         j	         j
                  dcY d}~S t           j        t                    r2 fd j        j        D             }d                    |          cY d}~S t          |t                    r%t           j         j	         j
                  cY d}~S  d}~ww xY w)	af  
        Return unicode representation of `self.data`.

        Try ``unicode(self.data)``, catch `UnicodeError` and

        * if `self.data` is an Exception instance, work around
          http://bugs.python.org/issue2517 with an emulation of
          Exception.__unicode__,

        * else decode with `self.encoding` and `self.decoding_errors`.
        z: u'z: 'z[Errno z] 'Nc           	      b    g | ]+}t          t          |j        j                             ,S ))r   )r%   r   r   r   r   s     r   r   z*SafeString.__unicode__.<locals>.<listcomp>   sU     3 3 3  
3,0,@!B !B !B C C 3 3 3r   r   )r%   r   r!   EnvironmentErrorr
   UnicodeErrorerrnor   strerrorr   r   filenamer"   r#   r$   UnicodeDecodeError)r   uerrorr#   s   `   r   __unicode__zSafeString.__unicode__l   s   		""A$)%566 -IIfe,,H 	 	 	$)%566 6 626)///ty14=#35 5 5 5ty14=#35 5 5 56 6 6 6 6 6 6
 $)Y// (3 3 3 3#'9>3 3 3 zz$''''''''%!344 Oty$-9MNNNNNNNN	s>   AA	 	EA5E	EAEE4EEEE)Nr	   r
   )__name__
__module____qualname____doc__r   r)   r5   r   r   r   r   r   M   sY          =O!*/ / / /  "    r   r   c                   ,     e Zd ZdZ fdZ fdZ xZS )ErrorStringz3
    Safely report exception type and message.
    c                 z    | j         j        j        dt          t          |                                           S Nz: )r   	__class__r6   superr;   r)   r   r>   s    r   r)   zErrorString.__str__   s<    9.777!+t44<<>>>@ 	@r   c                 z    | j         j        j        dt          t          |                                           S r=   )r   r>   r6   r?   r;   r5   r@   s    r   r5   zErrorString.__unicode__   s<     I/888!+t44@@BBBD 	Dr   )r6   r7   r8   r9   r)   r5   __classcell__)r>   s   @r   r;   r;      si         @ @ @ @ @D D D D D D D D Dr   r;   c                   ,    e Zd ZdZ	 	 	 ddZd Zd ZdS )	ErrorOutputz
    Wrapper class for file-like error streams with
    failsafe de- and encoding of `str`, `bytes`, `unicode` and
    `Exception` instances.
    Nr	   r
   c                 ~   |t           j        }nt|sd}not          |t                    rt	          |d          }nIt          |t
                    r4t	          |                    t          j                              d          }|| _        	 |pt          |dd          pt          pd| _        	 || _        	 || _        dS )a  
        :Parameters:
            - `stream`: a file-like object,
                        a string (path to a file),
                        `None` (write to `sys.stderr`, default), or
                        evaluating to `False` (write() requests are ignored).
            - `encoding`: `stream` text encoding. Guessed if None.
            - `encoding_errors`: how to treat encoding errors.
        NFwr   r   )r&   stderrr!   r   openr%   r(   getfilesystemencodingstreamr   r   r   r   r   )r   rJ   r   r   r   s        r   r   zErrorOutput.__init__   s      	KZFF 	KFF$$ 	K&#&&FF(( 	K&--(A(C(CDDcJJF+! 4WVZ%F%F 4(4,3 	,.%.%%r   c                 
   | j         du rdS t          |t                    r.t          t	          || j        | j        | j                            }	 | j                             |           dS # t          $ r< | j                             |
                    | j        | j                             Y dS t          $ r t          |t                    r;| j                             |
                    | j        | j                             Y dS | j         t          j        t          j        fv r"| j         j                            |           Y dS | j                             t          || j        | j                             Y dS w xY w)z
        Write `data` to self.stream. Ignore, if self.stream is False.

        `data` can be a `string`, `unicode`, or `Exception` instance.
        FN)rJ   r!   r"   r%   r   r   r   r   writer    r(   	TypeErrorr&   rG   stdoutbuffer)r   r   s     r   rL   zErrorOutput.write   s    ;% 	FdI&& 	O:dDM"&"68LN N O OD	AKd#####! 	P 	P 	PKdkk$-9MNNOOOOOO 		A 		A 		A$(( !!$++dm.2.B#D #D E E E{sz3:66 A"((......!!'$*.*>#@ #@ A A A A A A		As&   A, ,AF1AF
>F3FFc                     | j         t          j        t          j        fv rdS 	 | j                                          dS # t
          $ r Y dS w xY w)z
        Close the error-output stream.

        Ignored if the stream is` sys.stderr` or `sys.stdout` or has no
        close() method.
        N)rJ   r&   rN   rG   closeAttributeError)r   s    r   rQ   zErrorOutput.close   sb     ;3:sz22 	F	K 	 	 	DD	s   > 
AA)NNr	   r
   )r6   r7   r8   r9   r   rL   rQ   r   r   r   rD   rD      s_          .2!3!*!& !& !& !&FA A A4    r   rD   )r9   codecsr&   locale	getlocalegetdefaultlocaler   
ValueErrorr4   r#   lookupLookupErrorImportErrorr'   r   r%   objectr   r;   rD   r   r   r   <module>r\      s   8  



MMM*&*,,Q/O3J63J3L3LQ3O  # # # #ej0 	#%OO"Oo+,,,,   '    OOO. v G> > > > > > > >@
D 
D 
D 
D 
D* 
D 
D 
DP P P P P& P P P P Ps8   B *< AAA!A4 4A>=A>B
B