Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crashing from UnicodeDecodeError #288

Merged
merged 1 commit into from
Mar 23, 2016
Merged

Prevent crashing from UnicodeDecodeError #288

merged 1 commit into from
Mar 23, 2016

Conversation

jmoldow
Copy link
Contributor

@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 non-ASCII unicode string, and a non-ASCII str string, then the getvalue() method will fail with UnicodeDecodeError [1].

If nose2.plugins.buffer (which captures writes to sys.stdout in a StringIO.StringIO) is being used, that combination of writes, followed by a test failure, 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.

This bug is equivalent to nose-devs/nose#816 [2].

[1] https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258
[2] nose-devs/nose#816

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 non-ASCII `unicode` string, and a
non-ASCII `str` string, then the `getvalue()` method will fail
with `UnicodeDecodeError` [1].

If `nose2.plugins.buffer` (which captures writes to `sys.stdout`
in a `StringIO.StringIO`) is being used, that combination of
writes, followed by a test failure, 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.

This bug is equivalent to nose-devs/nose#816 [2].

[1] <https://github.com/python/cpython/blob/2.7/Lib/StringIO.py#L258>
[2] <nose-devs/nose#816>
@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 85.456% when pulling e6daba9 on jmoldow:StringIO into be999d4 on nose-devs:master.

@little-dude little-dude merged commit b7185ad into nose-devs:master Mar 23, 2016
@little-dude
Copy link
Contributor

@jmoldow thank you for the detailled explanation. Is this something you'd like to see in a 0.6 release or can it wait for 0.7?

@jmoldow jmoldow deleted the StringIO branch March 23, 2016 09:09
@jmoldow
Copy link
Contributor Author

jmoldow commented Mar 23, 2016

Doesn't matter to me.

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.

3 participants