mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-06 21:16:54 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
764ecb0e48
commit
421b9e2cfc
2 changed files with 16 additions and 12 deletions
|
|
@ -184,11 +184,13 @@ class BaseFormatter:
|
||||||
try:
|
try:
|
||||||
sys.stdout.write(value)
|
sys.stdout.write(value)
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
byteValue = value.encode(sys.stdout.encoding, 'backslashreplace')
|
byteValue = value.encode(sys.stdout.encoding, "backslashreplace")
|
||||||
if hasattr(sys.stdout, 'buffer'):
|
if hasattr(sys.stdout, "buffer"):
|
||||||
sys.stdout.buffer.write(byteValue)
|
sys.stdout.buffer.write(byteValue)
|
||||||
else:
|
else:
|
||||||
sys.stdout.write(byteValue.decode(sys.stdout.encoding, 'strict'))
|
sys.stdout.write(
|
||||||
|
byteValue.decode(sys.stdout.encoding, "strict")
|
||||||
|
)
|
||||||
|
|
||||||
def _write(self, output: str) -> None:
|
def _write(self, output: str) -> None:
|
||||||
"""Handle logic of whether to use an output file or print()."""
|
"""Handle logic of whether to use an output file or print()."""
|
||||||
|
|
|
||||||
|
|
@ -136,32 +136,34 @@ def test_write_produces_stdout(capsys):
|
||||||
assert capsys.readouterr().out == f"{line}\n{source}\n"
|
assert capsys.readouterr().out == f"{line}\n{source}\n"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('buffered_stdout', [True, False])
|
@pytest.mark.parametrize("buffered_stdout", [True, False])
|
||||||
def test_write_hook_fallbacks(buffered_stdout):
|
def test_write_hook_fallbacks(buffered_stdout):
|
||||||
mock_line = mock.Mock(name='Mock Line')
|
mock_line = mock.Mock(name="Mock Line")
|
||||||
|
|
||||||
stdout_spec = ['write', 'encoding']
|
stdout_spec = ["write", "encoding"]
|
||||||
if buffered_stdout:
|
if buffered_stdout:
|
||||||
stdout_spec.append('buffer')
|
stdout_spec.append("buffer")
|
||||||
|
|
||||||
|
with mock.patch("sys.stdout", spec=stdout_spec) as mock_stdout:
|
||||||
|
|
||||||
with mock.patch('sys.stdout', spec=stdout_spec) as mock_stdout:
|
|
||||||
def _stdoutWriteEffect(value):
|
def _stdoutWriteEffect(value):
|
||||||
if value is mock_line:
|
if value is mock_line:
|
||||||
raise UnicodeEncodeError('unittest-codec', u'', 42, 43, 'NOPE')
|
raise UnicodeEncodeError("unittest-codec", "", 42, 43, "NOPE")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
mock_stdout.write.side_effect = _stdoutWriteEffect
|
mock_stdout.write.side_effect = _stdoutWriteEffect
|
||||||
|
|
||||||
mock_stdout.encoding = 'ascii'
|
mock_stdout.encoding = "ascii"
|
||||||
|
|
||||||
formatter = base.BaseFormatter(options())
|
formatter = base.BaseFormatter(options())
|
||||||
formatter.write(mock_line, None)
|
formatter.write(mock_line, None)
|
||||||
|
|
||||||
mock_line.encode.assert_called_once_with('ascii', 'backslashreplace')
|
mock_line.encode.assert_called_once_with("ascii", "backslashreplace")
|
||||||
byte_mock_line = mock_line.encode.return_value
|
byte_mock_line = mock_line.encode.return_value
|
||||||
if buffered_stdout:
|
if buffered_stdout:
|
||||||
mock_stdout.buffer.write.assert_any_call(byte_mock_line)
|
mock_stdout.buffer.write.assert_any_call(byte_mock_line)
|
||||||
else:
|
else:
|
||||||
byte_mock_line.decode.assert_called_once_with('ascii', 'strict')
|
byte_mock_line.decode.assert_called_once_with("ascii", "strict")
|
||||||
mock_stdout.write.assert_any_call(byte_mock_line.decode.return_value)
|
mock_stdout.write.assert_any_call(byte_mock_line.decode.return_value)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue