Skip to content

Commit

Permalink
Merge an amended version of #630.
Browse files Browse the repository at this point in the history
Fake stdout should always have an encoding attribute.
  • Loading branch information
jszakmeister committed Nov 28, 2015
2 parents 5b02f5a + a8723ec commit 729c99a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions nose/plugins/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def addCaptureToErr(self, ev, output):
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):
Expand Down
7 changes: 7 additions & 0 deletions unit_tests/test_capture_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,12 @@ class Dummy:
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()

0 comments on commit 729c99a

Please # to comment.