o
    n§¥_!  ã                
   @   s  d Z ddlZddlZzddlZW n ey   dZY nFw ze ¡ d p(e ¡ d ZW n! eyF Z	 zde	j
v r:dZndZW Y dZ	[	n
dZ	[	w   dZY z	e epRd¡ W n ey`   dZY nw ejdkrheZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdS )ué  
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                   @   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   ú@/usr/lib/python3/dist-packages/docutils/utils/error_reporting.pyÚ__init__M   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>[   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__V   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   €   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__g   s2   
ÿÿý
þ
€òzSafeString.__unicode__)Nr   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r-   r   r   r   r   r   H   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.   Úsuperr2   r$   r   ©r5   r   r   r$   Œ   ó   
ÿzErrorString.__str__c                    r3   r4   )r   r5   r.   r6   r2   r-   r   r7   r   r   r-   �   r8   zErrorString.__unicode__)r.   r/   r0   r1   r$   r-   Ú__classcell__r   r   r7   r   r2   ˆ   s    r2   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
    failsave 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   r@   ¿   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!   rB   r<   ÚcloseÚAttributeErrorr   r   r   r   rD   Ù   s   ÿzErrorOutput.close)NNr   r   )r.   r/   r0   r1   r   r@   rD   r   r   r   r   r:   •   s    
þ#r:   )r1   Úcodecsr!   ÚlocaleÚImportErrorr   Ú	getlocaleÚgetdefaultlocaleÚ
ValueErrorr,   r   ÚlookupÚLookupErrorr"   r   r    Úobjectr   r2   r:   r   r   r   r   Ú<module>   s6   ÿ
€ÿ
@