o n_! @sdZddlZddlZzddlZWn eydZYnFwzedp(edZWn!eyFZ zde j vr:dZndZWYdZ [ n dZ [ wdZYz e epRdWn e y`dZYnwej dkrheZGdd d eZGd d d eZGd d d eZdS)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()`` uses `__str__` and fails with non-ASCII chars in`unicode` arguments. (work around http://bugs.python.org/issue2517): * In Python 2, unicode() 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() fails, with non-ASCII chars in `unicode` arguments. The `SafeString`, `ErrorString` and `ErrorOutput` classes handle common exceptions. Nzunknown locale: UTF-8zUTF-8rc@s.eZdZdZ  d ddZddZd d ZdS) SafeStringzG A wrapper providing robust conversion to `str` and `unicode`. NbackslashreplacereplacecCs0||_|pt|ddptpd|_||_||_dS)Nencodingascii)datagetattrlocale_encodingr encoding_errorsdecoding_errors)selfr r rrr@/usr/lib/python3/dist-packages/docutils/utils/error_reporting.py__init__Ms zSafeString.__init__csztjWStyAtjtr%fddjjD}d|YStjtr@tj dkr5jYSj j j YSw)Ncs g|] }tt|jjqSr)strrr r.0argrrr [s   z&SafeString.__str__.., r) rr UnicodeEncodeError isinstance Exceptionargsjoinunicodesys version_infoencoder r)rrrrr__str__Vs         zSafeString.__str__c sztj}tjtr|dd}|WStyv}zVtjtrAdjjtjjj j tjj j j fWYd}~Stjt r]fddjj D}d|WYd}~St|trqtjj j WYd}~Sd}~ww)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'Ncs"g|] }tt|jjdqS))r)r rr rrrrrrs   z*SafeString.__unicode__..r)r r rEnvironmentErrorr UnicodeErrorerrnorstrerrorr rfilenamerrrUnicodeDecodeError)ruerrorrrrr __unicode__gs2         zSafeString.__unicode__)Nrr)__name__ __module__ __qualname____doc__rr$r-rrrrrHs  rcs,eZdZdZfddZfddZZS) ErrorStringz3 Safely report exception type and message. cd|jjjtt|fSNz%s: %s)r __class__r.superr2r$rr5rrr$  zErrorString.__str__cr3r4)r r5r.r6r2r-rr7rrr-r8zErrorString.__unicode__)r.r/r0r1r$r- __classcell__rrr7rr2s r2c@s0eZdZdZ   d ddZddZd d ZdS) ErrorOutputz Wrapper class for file-like error streams with failsave de- and encoding of `str`, `bytes`, `unicode` and `Exception` instances. NrrcCs|durtj}n|s d}nt|trt|d}nt|tr't|td}||_ |p6t |ddp6t p6d|_ ||_ ||_ dS)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. NFwr r )r!stderrrropenr r#getfilesystemencodingstreamr r r rr)rr?r rrrrrrs(    zErrorOutput.__init__c Cs|jdurdSt|trtt||j|j|j}z |j|WdSt y7|j| |j|jYdSt yst|trQ|j| |j|jYdS|jt j t jfvrd|jj|YdS|jt||j|jYdSw)z Write `data` to self.stream. Ignore, if self.stream is False. `data` can be a `string`, `unicode`, or `Exception` instance. FN)r?rrr rr rrwriterr# TypeErrorr!r<stdoutbuffer)rr rrrr@s,       zErrorOutput.writecCs<|jtjtjfvr dSz|jWdStyYdSw)z Close the error-output stream. Ignored if the stream is` sys.stderr` or `sys.stdout` or has no close() method. N)r?r!rBr<closeAttributeErrorrrrrrDs zErrorOutput.close)NNrr)r.r/r0r1rr@rDrrrrr:s # r:)r1codecsr!locale ImportErrorr getlocalegetdefaultlocale ValueErrorr,rlookup LookupErrorr"rr objectrr2r:rrrrs6     @