nose-devs / nose

nose is nicer testing for python
http://readthedocs.org/docs/nose/en/latest/
1.36k stars 396 forks source link

Prevent crashing from UnicodeDecodeError #988

Open jmoldow opened 8 years ago

jmoldow commented 8 years ago

On Python 2, sys.stdout and print can normally handle any combination of str and unicode objects. However, StringIO.StringIO can only safely handle one or the other. If the program writes both a unicode string, and a non-ASCII str string, then the getvalue() method will fail with UnicodeDecodeError [1].

In nose, that causes the script to suddenly abort, with the cryptic UnicodeDecodeError.

This fix catches UnicodeError when trying to get the captured output, and will replace the captured output with a warning message.

Fixes #816

[1] https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258