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