Skip to content

Conversation

@jmoldow
Copy link

@jmoldow jmoldow commented Mar 23, 2016

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

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 nose-devs#816

[1] <https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnicodeDecodeError with 1.3.1 and python2.7

1 participant