o
    Ni                     @   sn   d Z ddlZddlZddlmZ ejdedd ejdkre	Z
G dd	 d	ZG d
d deZG dd dZdS )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                   @   s.   e Zd ZdZ		dddZdd Zd	d
 ZdS )
SafeStringzG
    A wrapper providing robust conversion to `str` and `unicode`.
    Nbackslashreplacereplacec                 C   s0   || _ |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    r   R/home/ubuntu/.local/lib/python3.10/site-packages/docutils/utils/error_reporting.py__init__C   s   
zSafeString.__init__c                    s   zt  jW S  tyA   t jtr% fdd jjD }d| Y S t jtr@tj	dkr5 j Y S  j
 j j Y S  w )Nc                    s    g | ]}t t| j jqS r   )strr   r
   r   .0argr   r   r   
<listcomp>P   s
    

z&SafeString.__str__.<locals>.<listcomp>, r   )r   r   UnicodeEncodeError
isinstance	Exceptionargsjoinunicodesysversion_infoencoder
   r   )r   r   r   r   r   __str__K   s    



zSafeString.__str__c              
      s   zt  j}t jtr|dd}|W S  tyv } zVt jtrAd jjt jj j	 j
t jj j	 j
f W  Y d}~S t jtr] fdd jjD }d|W  Y d}~S t|trqt  j j	 j
W  Y d}~S  d}~w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 %s] %s: '%s'Nc                    s"   g | ]}t t| j jd qS ))r   )r!   r   r
   r   r   r   r   r   r   v   s    z*SafeString.__unicode__.<locals>.<listcomp>r   )r!   r   r   EnvironmentErrorr	   UnicodeErrorerrnor   strerrorr
   r   filenamer   r   r    UnicodeDecodeError)r   uerrorr   r   r   r   __unicode__\   s4   


zSafeString.__unicode__)Nr   r	   )__name__
__module____qualname____doc__r   r%   r.   r   r   r   r   r   >   s    
r   c                       s,   e Zd ZdZ fddZ fddZ  ZS )ErrorStringz3
    Safely report exception type and message.
    c                       d| j jjtt|  f S Nz%s: %s)r   	__class__r/   superr3   r%   r   r6   r   r   r%         
zErrorString.__str__c                    r4   r5   )r   r6   r/   r7   r3   r.   r   r8   r   r   r.      r9   zErrorString.__unicode__)r/   r0   r1   r2   r%   r.   __classcell__r   r   r8   r   r3      s    r3   c                   @   s0   e Zd ZdZ			dddZdd Zd	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                 C   s   |du rt j}n|sd}nt|trt|d}nt|tr't|t  d}|| _	 |p6t	|ddp6t
p6d| _	 || _	 || _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   r@   r
   r   r   r   r   r   r      s(   

zErrorOutput.__init__c              	   C   s   | j du rdS t|trtt|| j| j| j}z	| j | W dS  t	y7   | j |
| j| j Y dS  tys   t|trQ| j |
| j| j Y dS | j tjtjfv rd| j j| Y dS | j t|| j| j Y dS w )z
        Write `data` to self.stream. Ignore, if self.stream is False.

        `data` can be a `string`, `unicode`, or `Exception` instance.
        FN)r@   r   r   r!   r   r
   r   r   writer   r$   	TypeErrorr"   r=   stdoutbuffer)r   r   r   r   r   rA      s.   



zErrorOutput.writec                 C   s<   | j tjtjfv rdS z| j   W dS  ty   Y dS w )z
        Close the error-output stream.

        Ignored if the stream is` sys.stderr` or `sys.stdout` or has no
        close() method.
        N)r@   r"   rC   r=   closeAttributeErrorr   r   r   r   rE      s   zErrorOutput.close)NNr   r	   )r/   r0   r1   r2   r   rA   rE   r   r   r   r   r;      s    
#r;   )r2   r"   warningsdocutils.ior   r   warnDeprecationWarningr#   r   r!   r   r3   r;   r   r   r   r   <module>   s   "
B