U C^ @sdZddlZddlZz ddlZWnek r8dZYnXzedpRedZWnDek rZ zde j krxdZndZW5dZ [ XYndZYnXze epdWne k rdZYnXGddde ZGd d d eZGd d d e ZdS) 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-8c@s*eZdZdZd ddZddZd d ZdS) SafeStringzG A wrapper providing robust conversion to `str` and `unicode`. NbackslashreplacereplacecCs0||_|pt|ddptpd|_||_||_dS)Nencodingascii)datagetattrlocale_encodingrencoding_errorsdecoding_errors)selfr rr r rD/tmp/pip-install-6_kvzl1k/docutils/docutils/utils/error_reporting.py__init__IszSafeString.__init__csz tjWStk rtjtrLfddjjD}d|YStjtrtjdkrljYSj j j YSYnXdS)Ncs g|]}tt|jjqSr)strrrr .0argrrr Ws z&SafeString.__str__.., )r) rr UnicodeEncodeError isinstance Exceptionargsjoinsys version_infoencoderr )rrrrr__str__Rs       zSafeString.__str__c sz(tj}tjtr$|dd}|WStk r}ztjtrdjjtjjj j tjj j j fWY`Stjt rfddjj D}d|WY,St|trtjj j WYSW5d}~XYnXdS)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'cs"g|]}tt|jjdqS))r )rrrr rrrrr|s z*SafeString.__unicode__..rN)rr rEnvironmentErrorr UnicodeErrorerrnorstrerrorrr filenamerrrUnicodeDecodeError)ruerrorrrrr __unicode__cs.          zSafeString.__unicode__)Nrr)__name__ __module__ __qualname____doc__rr"r+rrrrrDs  rcs,eZdZdZfddZfddZZS) ErrorStringz3 Safely report exception type and message. csd|jjjtt|fSNz%s: %s)r __class__r,superr0r"rr2rrr"s  zErrorString.__str__csd|jjjtt|fSr1)r r2r,r3r0r+rr4rrr+s  zErrorString.__unicode__)r,r-r.r/r"r+ __classcell__rrr4rr0s r0c@s*eZdZdZd ddZddZd d ZdS) ErrorOutputz Wrapper class for file-like error streams with failsave de- and encoding of `str`, `bytes`, `unicode` and `Exception` instances. NrrcCs~|dkrtj}n>|sd}n4t|tr0t|d}nt|trNt|td}||_|pjt|ddpjt pjd|_ ||_ ||_ 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. NFwrr) rstderrrropenr!getfilesystemencodingstreamr r rr r )rr;rr r rrrrs"    zErrorOutput.__init__c Cs|jdkrdSt|tr0tt||j|j|j}z|j|Wnt k rl|j| |j|jYnvt k rt|tr|j| |j|jYdS|jt j t jfkr|jj|n|jt||j|jYnXdS)z Write `data` to self.stream. Ignore, if self.stream is False. `data` can be a `string`, `unicode`, or `Exception` instance. FN)r;rrrrrr r writerr! TypeErrorrr8stdoutbuffer)rr rrrr<s,    zErrorOutput.writecCs>|jtjtjfkrdSz|jWntk r8YnXdS)z Close the error-output stream. Ignored if the stream is` sys.stderr` or `sys.stdout` or has no close() method. N)r;rr>r8closeAttributeErrorrrrrr@s zErrorOutput.close)NNrr)r,r-r.r/rr<r@rrrrr6s #r6)r/rcodecslocale ImportErrorr getlocalegetdefaultlocale ValueErrorr*rlookup LookupErrorobjectrr0r6rrrrs(     @