U
    [Sh                     @   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   Q/root/rtd-docs/venv/lib/python3.8/site-packages/docutils/utils/error_reporting.py__init__C   s    zSafeString.__init__c                    s   zt  jW S  tk
r   t jtrL fdd jjD }d| Y S t jtrtj	dkrl j Y S  j
 j j Y S  Y nX d S )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k
r } zt jtrd jjt jj j	 j
t jj j	 j
f  W Y `S t jtr fdd jjD }d| W Y ,S t|trt  j j	 j
 W Y S  W 5 d}~X Y nX dS )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'c                    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   N)r!   r   r   EnvironmentErrorr	   UnicodeErrorerrnor   strerrorr
   r   filenamer   r   r    UnicodeDecodeError)r   uerrorr   r   r   r   __unicode__\   s0    


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                    s   d| j jjtt|  f S Nz%s: %s)r   	__class__r/   superr3   r%   r   r5   r   r   r%      s    
zErrorString.__str__c                    s   d| j jjtt|  f S r4   )r   r5   r/   r6   r3   r.   r   r7   r   r   r.      s    
zErrorString.__unicode__)r/   r0   r1   r2   r%   r.   __classcell__r   r   r7   r   r3      s   r3   c                   @   s*   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krt j}n>|sd}n4t|tr0t|d}nt|trNt|t  d}|| _|pjt	|ddpjt
pjd| _|| _|| _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krdS t|tr0tt|| j| j| j}z| j | W n t	k
rl   | j |
| j| j Y nv tk
r   t|tr| j |
| j| j Y dS | j tjtjfkr| j j| n| j t|| j| j Y nX dS )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   r?      s,    



zErrorOutput.writec                 C   s>   | j tjtjfkrdS z| j   W n tk
r8   Y nX dS )z
        Close the error-output stream.

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