mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-08 14:04:16 +00:00
satisfying the linter
This commit is contained in:
parent
764ecb0e48
commit
fab65e5208
2 changed files with 11 additions and 9 deletions
|
|
@ -179,24 +179,25 @@ class BaseFormatter:
|
||||||
# one
|
# one
|
||||||
return f"{error.physical_line}{indent}^"
|
return f"{error.physical_line}{indent}^"
|
||||||
|
|
||||||
def _stdoutWriteHook(self, value: str) -> None:
|
def _stdout_write_hook(self, value: str) -> None:
|
||||||
"""An alternative to print() function that handles output terminal encoding."""
|
"""sys.stdout.write() with fallbacks."""
|
||||||
try:
|
try:
|
||||||
sys.stdout.write(value)
|
sys.stdout.write(value)
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
byteValue = value.encode(sys.stdout.encoding, 'backslashreplace')
|
byte_value = 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(byte_value)
|
||||||
else:
|
else:
|
||||||
sys.stdout.write(byteValue.decode(sys.stdout.encoding, 'strict'))
|
sys.stdout.write(
|
||||||
|
byte_value.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()."""
|
||||||
if self.output_fd is not None:
|
if self.output_fd is not None:
|
||||||
self.output_fd.write(output + self.newline)
|
self.output_fd.write(output + self.newline)
|
||||||
if self.output_fd is None or self.options.tee:
|
if self.output_fd is None or self.options.tee:
|
||||||
self._stdoutWriteHook(output)
|
self._stdout_write_hook(output)
|
||||||
self._stdoutWriteHook(self.newline)
|
self._stdout_write_hook(self.newline)
|
||||||
|
|
||||||
def write(self, line: Optional[str], source: Optional[str]) -> None:
|
def write(self, line: Optional[str], source: Optional[str]) -> None:
|
||||||
"""Write the line either to the output file or stdout.
|
"""Write the line either to the output file or stdout.
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,7 @@ def test_write_produces_stdout(capsys):
|
||||||
|
|
||||||
@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):
|
||||||
|
"""Varify stdout.write() fallbacks."""
|
||||||
mock_line = mock.Mock(name='Mock Line')
|
mock_line = mock.Mock(name='Mock Line')
|
||||||
|
|
||||||
stdout_spec = ['write', 'encoding']
|
stdout_spec = ['write', 'encoding']
|
||||||
|
|
@ -145,11 +146,11 @@ def test_write_hook_fallbacks(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 _stdout_write_effect(value):
|
||||||
if value is mock_line:
|
if value is mock_line:
|
||||||
raise UnicodeEncodeError('unittest-codec', u'', 42, 43, 'NOPE')
|
raise UnicodeEncodeError('unittest-codec', u'', 42, 43, 'NOPE')
|
||||||
return None
|
return None
|
||||||
mock_stdout.write.side_effect = _stdoutWriteEffect
|
mock_stdout.write.side_effect = _stdout_write_effect
|
||||||
|
|
||||||
mock_stdout.encoding = 'ascii'
|
mock_stdout.encoding = 'ascii'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue