
    dd                         d Z ddlZddlZddlmZ  ej                  ded       ej                  dk\  re	Z
 G d d	      Z G d
 de      Z G d d      Zy)u9  
Deprecated module to handle Exceptions across Python versions.

.. warning::
   This module is deprecated with the end of support for Python 2.7
   and will be removed in Docutils 0.21 or later.

   Replacements:
     | SafeString  -> str
     | ErrorString -> docutils.io.error_string()
     | ErrorOutput -> docutils.io.ErrorOutput

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)_locale_encodingzThe `docutils.utils.error_reporting` module is deprecated and will be removed in Docutils 0.21 or later.
Details with help("docutils.utils.error_reporting").   )
stacklevel   r   c                   (    e Zd ZdZ	 	 ddZd Zd Zy)
SafeStringzG
    A wrapper providing robust conversion to `str` and `unicode`.
    Nc                 r    || _         |xs t        |dd       xs
 t        xs d| _        || _        || _        y )Nencodingascii)datagetattrlocale_encodingr   encoding_errorsdecoding_errors)selfr   r   r   r   s        @/usr/lib/python3/dist-packages/docutils/utils/error_reporting.py__init__zSafeString.__init__C   sC    	! 7WT:t%D 7+7/6 	..    c                     	 t        | j                        S # t        $ r t        | j                  t              rd| j                  j
                  D cg c],  }t        t        || j                  | j                              . nc c}w }}dj                  |      cY S t        | j                  t              rSt        j                  dkD  r| j                  cY S | j                  j                  | j                  | j                        cY S  w xY w)N, r   )strr   UnicodeEncodeError
isinstance	Exceptionargsr	   r   r   joinunicodesysversion_infoencode)r   argr   s      r   __str__zSafeString.__str__K   s    	tyy>!! 	$))Y/ $(99>>3 JsDMM'+';';= > 3 3 3 yy&$))W-##f,99$99++DMM,0,@,@B B	s'    ;D1BD9D0DDc                 ~   	 t        | j                        }t        | j                  t              r|j	                  dd      }|S # t
        $ rm}t        | j                  t              rd| j                  j                  dt        | j                  j                  | j                  | j                        dt        | j                  j                  | j                  | j                        dcY d}~S t        | j                  t              rh| j                  j                  D cg c]-  }t        t        || j                  | j                              / nc c}w }}dj                  |      cY d}~S t        |t              r0t        | 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] 'N)r   r   )r   r   r   EnvironmentErrorreplaceUnicodeErrorerrnor	   strerrorr   r   filenamer   r   r   UnicodeDecodeError)r   uerrorr"   r   s        r   __unicode__zSafeString.__unicode__\   sM   			"A$))%56IIfe,H 	$))%56IIOOtyy114==#335tyy114==#335	6 6 $))Y/ $(99>>3    
$'484H4H!J K 3 3 3 zz$''%!34tyy$--9M9MNN!	sI   AA 
F<BF7.F<42F7&2EF70F<6:F70F<6F77F<)Nbackslashreplacer'   )__name__
__module____qualname____doc__r   r#   r/    r   r   r	   r	   >   s     =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                 n    | j                   j                  j                  dt        t        |          S Nz: )r   	__class__r1   superr7   r#   r   r:   s    r   r#   zErrorString.__str__   s-    99..77 d;=? 	?r   c                 n    | j                   j                  j                  dt        t        |          S r9   )r   r:   r1   r;   r7   r/   r<   s    r   r/   zErrorString.__unicode__   s/     II//88!+t@BD 	Dr   )r1   r2   r3   r4   r#   r/   __classcell__)r:   s   @r   r7   r7      s    ?D Dr   r7   c                   *    e Zd ZdZ	 	 	 ddZd Zd Zy)ErrorOutputz
    Wrapper class for file-like error streams with
    failsafe de- and encoding of `str`, `bytes`, `unicode` and
    `Exception` instances.
    Nc                 \   |t         j                  }n_|sd}nZt        |t              rt	        |d      }n=t        |t
              r-t	        |j                  t        j                               d      }|| _        	 |xs t        |dd      xs
 t        xs d| _        	 || _        	 || _        y)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   rF   r   r   r   s        r   r   zErrorOutput.__init__   s     >ZZFF$&#&F(&--(A(A(CDcJF+! 7WVZ%F 7+7/6 	,.%.%r   c                 P   | j                   du ryt        |t              r5t        t	        || j
                  | j                  | j                              }	 | j                   j                  |       y# t        $ rB | j                   j                  |j                  | j
                  | j                               Y yt        $ r t        |t              rA| j                   j                  |j                  | j
                  | j                               Y y| j                   t        j                  t        j                  fv r'| j                   j                  j                  |       Y y| j                   j                  t        || j
                  | j                               Y y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)rF   r   r   r   r	   r   r   r   writer   r!   	TypeErrorr   rC   stdoutbuffer)r   r   s     r   rH   zErrorOutput.write   s9    ;;%dI&:dDMM&*&:&:&*&:&:< =D	AKKd#! 	PKKdkk$--9M9MNO 		A$(!!$++dmm.2.B.B#D E{{szz3::66""((.!!'$*.*>*>#@ A		As'   A2 2AF%<AF%AF%(:F%$F%c                     | j                   t        j                  t        j                  fv ry	 | j                   j	                          y# t
        $ r Y y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)rF   r   rJ   rC   closeAttributeError)r   s    r   rM   zErrorOutput.close   sG     ;;3::szz22	KK 		s   A
 
	AA)NNr0   r'   )r1   r2   r3   r4   r   rH   rM   r5   r   r   r@   r@      s#     .2!3!*!&FA6r   r@   )r4   r   warningsdocutils.ior   r   warnDeprecationWarningr    r   r   r	   r7   r@   r5   r   r   <module>rS      sk    D   ;  E !Q0 vG? ?D
D* 
DQ Qr   