Skip to content

Commit

Permalink
fake stdout should always have encoding attribute
Browse files Browse the repository at this point in the history
Some programs expect this attribute, and use it to encode strings before writing to stdout. If it's not there, they fail with an AttributeError.
  • Loading branch information
glyphobet authored and jszakmeister committed Nov 28, 2015
1 parent 5b02f5a commit a02d4bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nose/plugins/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def addCaptureToErr(self, ev, output):
def start(self):
self.stdout.append(sys.stdout)
self._buf = StringIO()
if not hasattr(self._buf, 'encoding'):
self._buf.encoding = sys.__stdout__.encoding
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 a02d4bf

Please # to comment.