|
12 | 12 | import logging |
13 | 13 | import os |
14 | 14 | import sys |
| 15 | +import traceback |
15 | 16 | from nose.plugins.base import Plugin |
16 | 17 | from nose.pyversion import exc_to_unicode, force_unicode |
17 | 18 | from nose.util import ln |
@@ -71,26 +72,56 @@ def beforeTest(self, test): |
71 | 72 | def formatError(self, test, err): |
72 | 73 | """Add captured output to error report. |
73 | 74 | """ |
74 | | - test.capturedOutput = output = self.buffer |
| 75 | + test.capturedOutput = output = '' |
| 76 | + output_exc_info = None |
| 77 | + try: |
| 78 | + test.capturedOutput = output = self.buffer |
| 79 | + except UnicodeError: |
| 80 | + # python2's StringIO.StringIO [1] class has this warning: |
| 81 | + # |
| 82 | + # The StringIO object can accept either Unicode or 8-bit strings, |
| 83 | + # but mixing the two may take some care. If both are used, 8-bit |
| 84 | + # strings that cannot be interpreted as 7-bit ASCII (that use the |
| 85 | + # 8th bit) will cause a UnicodeError to be raised when getvalue() |
| 86 | + # is called. |
| 87 | + # |
| 88 | + # This exception handler is a protection against issue #816 [2]. |
| 89 | + # Capturing the exception info allows us to display it back to the |
| 90 | + # user. |
| 91 | + # |
| 92 | + # [1] <https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258> |
| 93 | + # [2] <https://github.com/nose-devs/nose/issues/816> |
| 94 | + output_exc_info = sys.exc_info() |
75 | 95 | self._buf = None |
76 | | - if not output: |
| 96 | + if (not output) and (not output_exc_info): |
77 | 97 | # Don't return None as that will prevent other |
78 | 98 | # formatters from formatting and remove earlier formatters |
79 | 99 | # formats, instead return the err we got |
80 | 100 | return err |
81 | 101 | ec, ev, tb = err |
82 | | - return (ec, self.addCaptureToErr(ev, output), tb) |
| 102 | + return (ec, self.addCaptureToErr(ev, output, output_exc_info=output_exc_info), tb) |
83 | 103 |
|
84 | 104 | def formatFailure(self, test, err): |
85 | 105 | """Add captured output to failure report. |
86 | 106 | """ |
87 | 107 | return self.formatError(test, err) |
88 | 108 |
|
89 | | - def addCaptureToErr(self, ev, output): |
| 109 | + def addCaptureToErr(self, ev, output, output_exc_info=None): |
| 110 | + # If given, output_exc_info should be a 3-tuple from sys.exc_info(), |
| 111 | + # from an exception raised while trying to get the captured output. |
90 | 112 | ev = exc_to_unicode(ev) |
91 | 113 | output = force_unicode(output) |
92 | | - return u'\n'.join([ev, ln(u'>> begin captured stdout <<'), |
93 | | - output, ln(u'>> end captured stdout <<')]) |
| 114 | + error_text = [ev, ln(u'>> begin captured stdout <<'), |
| 115 | + output, ln(u'>> end captured stdout <<')] |
| 116 | + if output_exc_info: |
| 117 | + error_text.extend([u'OUTPUT ERROR: Could not get captured output.', |
| 118 | + # <https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258> |
| 119 | + # <https://github.com/nose-devs/nose/issues/816> |
| 120 | + u"The test might've printed both 'unicode' strings and non-ASCII 8-bit 'str' strings.", |
| 121 | + ln(u'>> begin captured stdout exception traceback <<'), |
| 122 | + u''.join(traceback.format_exception(*output_exc_info)), |
| 123 | + ln(u'>> end captured stdout exception traceback <<')]) |
| 124 | + return u'\n'.join(error_text) |
94 | 125 |
|
95 | 126 | def start(self): |
96 | 127 | self.stdout.append(sys.stdout) |
|
0 commit comments