diff options
author | John Szakmeister <john@szakmeister.net> | 2015-11-28 07:27:16 -0500 |
---|---|---|
committer | John Szakmeister <john@szakmeister.net> | 2015-11-28 07:27:16 -0500 |
commit | 729c99a68f63b09e3ea05ba9f0c1ce6d51d70018 (patch) | |
tree | 2ea7842b3175f501242f55db85518da99976fb28 | |
parent | 5b02f5a32e5fb13471d497bfd635c328ac32f146 (diff) | |
parent | a8723ecfcf74105db2caabf1d2a4c614fd942396 (diff) | |
download | nose-729c99a68f63b09e3ea05ba9f0c1ce6d51d70018.tar.gz |
Merge an amended version of #630.
Fake stdout should always have an encoding attribute.
-rw-r--r-- | nose/plugins/capture.py | 9 | ||||
-rw-r--r-- | unit_tests/test_capture_plugin.py | 7 |
2 files changed, 16 insertions, 0 deletions
diff --git a/nose/plugins/capture.py b/nose/plugins/capture.py index fa4e5dc..c81f21e 100644 --- a/nose/plugins/capture.py +++ b/nose/plugins/capture.py @@ -95,6 +95,15 @@ class Capture(Plugin): def start(self): self.stdout.append(sys.stdout) self._buf = StringIO() + # Python 3's StringIO objects don't support setting encoding or errors + # directly and they're already set to None. So if the attributes + # already exist, skip adding them. + if (not hasattr(self._buf, 'encoding') and + hasattr(sys.stdout, 'encoding')): + self._buf.encoding = sys.stdout.encoding + if (not hasattr(self._buf, 'errors') and + hasattr(sys.stdout, 'errors')): + self._buf.errors = sys.stdout.errors sys.stdout = self._buf def end(self): diff --git a/unit_tests/test_capture_plugin.py b/unit_tests/test_capture_plugin.py index 2f721f0..edab7de 100644 --- a/unit_tests/test_capture_plugin.py +++ b/unit_tests/test_capture_plugin.py @@ -96,5 +96,12 @@ class TestCapturePlugin(unittest.TestCase): err = sys.exc_info() formatted = c.formatError(d, err) + def test_captured_stdout_has_encoding_attribute(self): + c = Capture() + c.start() + self.assertNotEqual(sys.stdout, sys.__stdout__) + self.assertTrue(hasattr(sys.stdout, 'encoding')) + c.end() + if __name__ == '__main__': unittest.main() |