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