-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Allow IOErrors when attempting to retrieve default client encoding. #21531
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
Changes from 9 commits
6c2987d
c31b914
510f4b1
1afda5f
b37cb75
0b1ca1d
bf26adb
77a95d2
2598595
59e0993
8b51b34
984da5c
4790e10
6156825
fb1dc2e
7cedfdc
35c0b3b
49ee7b5
40b3588
5e3c8af
f485223
ab67024
81547da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
from functools import partial | ||
|
||
from pandas.io.formats.console import detect_console_encoding, locale | ||
|
||
|
||
def mock_raises_exception(error=Exception): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this for? |
||
raise error | ||
|
||
|
||
@pytest.mark.parametrize('empty,filled', [ | ||
['stdin', 'stdout'], | ||
['stdout', 'stdin'] | ||
]) | ||
def test_detect_console_encoding_from_stdout_stdin(monkeypatch, empty, filled): | ||
# Ensures that when sys.stdout.encoding or sys.stdin.encoding is used when | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can add a reference to the issue for this and the other new tests, i.e. |
||
# they have values filled. | ||
class MockEncoding(object): | ||
def __init__(self, encoding): | ||
super(MockEncoding, self).__init__() | ||
self.encoding = encoding | ||
|
||
monkeypatch.setattr('sys.{}'.format(empty), MockEncoding('')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any chance you saw the context manager for monkeypatch? Seems to be preferred for patching standard library items (may require a version bump for pytest dependency). |
||
monkeypatch.setattr('sys.{}'.format(filled), MockEncoding(filled)) | ||
assert detect_console_encoding() == filled | ||
|
||
|
||
@pytest.mark.parametrize('stdEncoding', [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8 so |
||
partial(mock_raises_exception, AttributeError), | ||
partial(mock_raises_exception, IOError), | ||
lambda: 'ascii' | ||
]) | ||
def test_detect_console_encoding_fallback_to_locale(monkeypatch, stdEncoding): | ||
monkeypatch.setattr('locale.getpreferredencoding', lambda: 'foo') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can put this in the context manager, no? |
||
monkeypatch.setattr('sys.stdout', stdEncoding) | ||
assert detect_console_encoding() == 'foo' | ||
|
||
|
||
@pytest.mark.parametrize('std,locale', [ | ||
['ascii', 'ascii'], | ||
['ascii', mock_raises_exception], | ||
[mock_raises_exception, 'ascii'], | ||
[mock_raises_exception, mock_raises_exception] | ||
]) | ||
def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale): | ||
# When both the stdout/stdin encoding and locale preferred encoding checks | ||
# fail (or return 'ascii', we should default to the sys default encoding. | ||
monkeypatch.setattr('sys.stdout', std) | ||
monkeypatch.setattr('locale.getpreferredencoding', locale) | ||
monkeypatch.setattr('sys.getdefaultencoding', lambda: 'sysDefaultEncoding') | ||
assert detect_console_encoding() == 'sysDefaultEncoding' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove from here