9ad190
diff -up nose-1.3.7/AUTHORS.unicode nose-1.3.7/AUTHORS
9ad190
diff -up nose-1.3.7/CHANGELOG.unicode nose-1.3.7/CHANGELOG
9ad190
diff -up nose-1.3.7/nose/plugins/capture.py.unicode nose-1.3.7/nose/plugins/capture.py
9ad190
--- nose-1.3.7/nose/plugins/capture.py.unicode	2015-04-04 02:52:52.000000000 -0600
9ad190
+++ nose-1.3.7/nose/plugins/capture.py	2016-11-15 13:58:18.713025335 -0700
9ad190
@@ -12,6 +12,7 @@ the options ``-s`` or ``--nocapture``.
9ad190
 import logging
9ad190
 import os
9ad190
 import sys
9ad190
+import traceback
9ad190
 from nose.plugins.base import Plugin
9ad190
 from nose.pyversion import exc_to_unicode, force_unicode
9ad190
 from nose.util import ln
9ad190
@@ -71,26 +72,56 @@ class Capture(Plugin):
9ad190
     def formatError(self, test, err):
9ad190
         """Add captured output to error report.
9ad190
         """
9ad190
-        test.capturedOutput = output = self.buffer
9ad190
+        test.capturedOutput = output = ''
9ad190
+        output_exc_info = None
9ad190
+        try:
9ad190
+            test.capturedOutput = output = self.buffer
9ad190
+        except UnicodeError:
9ad190
+            # python2's StringIO.StringIO [1] class has this warning:
9ad190
+            #
9ad190
+            #     The StringIO object can accept either Unicode or 8-bit strings,
9ad190
+            #     but mixing the two may take some care. If both are used, 8-bit
9ad190
+            #     strings that cannot be interpreted as 7-bit ASCII (that use the
9ad190
+            #     8th bit) will cause a UnicodeError to be raised when getvalue()
9ad190
+            #     is called.
9ad190
+            #
9ad190
+            # This exception handler is a protection against issue #816 [2].
9ad190
+            # Capturing the exception info allows us to display it back to the
9ad190
+            # user.
9ad190
+            #
9ad190
+            # [1] <https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258>
9ad190
+            # [2] <https://github.com/nose-devs/nose/issues/816>
9ad190
+            output_exc_info = sys.exc_info()
9ad190
         self._buf = None
9ad190
-        if not output:
9ad190
+        if (not output) and (not output_exc_info):
9ad190
             # Don't return None as that will prevent other
9ad190
             # formatters from formatting and remove earlier formatters
9ad190
             # formats, instead return the err we got
9ad190
             return err
9ad190
         ec, ev, tb = err
9ad190
-        return (ec, self.addCaptureToErr(ev, output), tb)
9ad190
+        return (ec, self.addCaptureToErr(ev, output, output_exc_info=output_exc_info), tb)
9ad190
 
9ad190
     def formatFailure(self, test, err):
9ad190
         """Add captured output to failure report.
9ad190
         """
9ad190
         return self.formatError(test, err)
9ad190
 
9ad190
-    def addCaptureToErr(self, ev, output):
9ad190
+    def addCaptureToErr(self, ev, output, output_exc_info=None):
9ad190
+        # If given, output_exc_info should be a 3-tuple from sys.exc_info(),
9ad190
+        # from an exception raised while trying to get the captured output.
9ad190
         ev = exc_to_unicode(ev)
9ad190
         output = force_unicode(output)
9ad190
-        return u'\n'.join([ev, ln(u'>> begin captured stdout <<'),
9ad190
-                           output, ln(u'>> end captured stdout <<')])
9ad190
+        error_text = [ev, ln(u'>> begin captured stdout <<'),
9ad190
+                      output, ln(u'>> end captured stdout <<')]
9ad190
+        if output_exc_info:
9ad190
+            error_text.extend([u'OUTPUT ERROR: Could not get captured output.',
9ad190
+                               # <https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258>
9ad190
+                               # <https://github.com/nose-devs/nose/issues/816>
9ad190
+                               u"The test might've printed both 'unicode' strings and non-ASCII 8-bit 'str' strings.",
9ad190
+                               ln(u'>> begin captured stdout exception traceback <<'),
9ad190
+                               u''.join(traceback.format_exception(*output_exc_info)),
9ad190
+                               ln(u'>> end captured stdout exception traceback <<')])
9ad190
+        return u'\n'.join(error_text)
9ad190
 
9ad190
     def start(self):
9ad190
         self.stdout.append(sys.stdout)
9ad190
diff -up nose-1.3.7/unit_tests/test_capture_plugin.py.unicode nose-1.3.7/unit_tests/test_capture_plugin.py
9ad190
--- nose-1.3.7/unit_tests/test_capture_plugin.py.unicode	2012-09-29 02:18:54.000000000 -0600
9ad190
+++ nose-1.3.7/unit_tests/test_capture_plugin.py	2016-11-15 13:58:18.714025330 -0700
9ad190
@@ -4,6 +4,12 @@ import unittest
9ad190
 from optparse import OptionParser
9ad190
 from nose.config import Config
9ad190
 from nose.plugins.capture import Capture
9ad190
+from nose.pyversion import force_unicode
9ad190
+
9ad190
+if sys.version_info[0] == 2:
9ad190
+    py2 = True
9ad190
+else:
9ad190
+    py2 = False
9ad190
 
9ad190
 class TestCapturePlugin(unittest.TestCase):
9ad190
 
9ad190
@@ -62,6 +68,35 @@ class TestCapturePlugin(unittest.TestCas
9ad190
         c.end()
9ad190
         self.assertEqual(c.buffer, "test 日本\n")
9ad190
 
9ad190
+    def test_does_not_crash_with_mixed_unicode_and_nonascii_str(self):
9ad190
+        class Dummy:
9ad190
+            pass
9ad190
+        d = Dummy()
9ad190
+        c = Capture()
9ad190
+        c.start()
9ad190
+        printed_nonascii_str = force_unicode("test 日本").encode('utf-8')
9ad190
+        printed_unicode = force_unicode("Hello")
9ad190
+        print printed_nonascii_str
9ad190
+        print printed_unicode
9ad190
+        try:
9ad190
+            raise Exception("boom")
9ad190
+        except:
9ad190
+            err = sys.exc_info()
9ad190
+        formatted = c.formatError(d, err)
9ad190
+        _, fev, _ = formatted
9ad190
+
9ad190
+        if py2:
9ad190
+            for string in [force_unicode(printed_nonascii_str, encoding='utf-8'), printed_unicode]:
9ad190
+                assert string not in fev, "Output unexpectedly found in error message"
9ad190
+            assert d.capturedOutput == '', "capturedOutput unexpectedly non-empty"
9ad190
+            assert "OUTPUT ERROR" in fev
9ad190
+            assert "captured stdout exception traceback" in fev
9ad190
+            assert "UnicodeDecodeError" in fev
9ad190
+        else:
9ad190
+            for string in [repr(printed_nonascii_str), printed_unicode]:
9ad190
+                assert string in fev, "Output not found in error message"
9ad190
+                assert string in d.capturedOutput, "Output not attached to test"
9ad190
+
9ad190
     def test_format_error(self):
9ad190
         class Dummy:
9ad190
             pass