B \ @sdZddlZddlZy ddlZWnek r8dZYnXyedpRedZWnDek rZ zde j krxdZndZWddZ [ XYndZYnXye 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 rB/tmp/pip-build-uw_ogi45/docutils/docutils/utils/error_reporting.py__init__Is  zSafeString.__init__csy tjStk rztjtrFfddjjD}d|StjtrttjdkrbjSj j j SYnXdS)Ncs g|]}tt|jjqSr)strrrr ).0arg)rrr Wsz&SafeString.__str__..z, )r) rr UnicodeEncodeError isinstance Exceptionargsjoinsys version_infoencoderr )rrr)rr__str__Rs        zSafeString.__str__c sy&tj}tjtr$|dd}|Stk r}ztjtrvdjjtjjj j tjj j j fStjt rfddjj D}d|St|trtjj j SWdd}~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 )rr)rrrr|sz*SafeString.__unicode__..z, N)rr rEnvironmentErrorr UnicodeErrorerrnorstrerrorrr filenamerrrUnicodeDecodeError)ruerrorrr)rr __unicode__cs&             zSafeString.__unicode__)Nrr)__name__ __module__ __qualname____doc__rrr(rrrrrDs  rcs,eZdZdZfddZfddZZS) ErrorStringz3 Safely report exception type and message. csd|jjjtt|fS)Nz%s: %s)r __class__r)superr-r)r)r.rrrs zErrorString.__str__csd|jjjtt|fS)Nz%s: %s)r r.r)r/r-r()r)r.rrr(s zErrorString.__unicode__)r)r*r+r,rr( __classcell__rr)r.rr-s r-c@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) rstderrrropenrgetfilesystemencodingstreamr r rr r )rr6rr r rrrrs     zErrorOutput.__init__c Cs|jdkrdSt|tr0tt||j|j|j}y|j|Wnt k rl|j| |j|jYntt k rt|tr|j| |j|jdS|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)r6rrrrrr r writerr TypeErrorrr3stdoutbuffer)rr rrrr7s$     zErrorOutput.writecCs>|jtjtjfkrdSy|jWntk r8YnXdS)z Close the error-output stream. Ignored if the stream is` sys.stderr` or `sys.stdout` or has no close() method. N)r6rr9r3closeAttributeError)rrrrr;s zErrorOutput.close)NNrr)r)r*r+r,rr7r;rrrrr1s  !r1)r,rcodecslocale ImportErrorr getlocalegetdefaultlocale ValueErrorr'rlookup LookupErrorobjectrr-r1rrrr$s(     @