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

write directly to sys.stdout.buffer to avoid windows io encoding #1382

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/flake8/formatting/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The base class and interface for all formatting plugins."""
import argparse
import os
import sys
from typing import IO
from typing import List
from typing import Optional
Expand Down Expand Up @@ -183,7 +184,7 @@ def _write(self, output: str) -> None:
if self.output_fd is not None:
self.output_fd.write(output + self.newline)
if self.output_fd is None or self.options.tee:
print(output, end=self.newline)
sys.stdout.buffer.write(output.encode() + self.newline.encode())

def write(self, line: Optional[str], source: Optional[str]) -> None:
"""Write the line either to the output file or stdout.
Expand Down
27 changes: 8 additions & 19 deletions tests/unit/test_base_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_show_source_updates_physical_line_appropriately(line1, line2, column):


@pytest.mark.parametrize("tee", [False, True])
def test_write_uses_an_output_file(tee):
def test_write_uses_an_output_file(tee, capsys):
"""Verify that we use the output file when it's present."""
line = "Something to write"
source = "source"
Expand All @@ -111,16 +111,11 @@ def test_write_uses_an_output_file(tee):
formatter = base.BaseFormatter(options(tee=tee))
formatter.output_fd = filemock

with mock.patch("flake8.formatting.base.print") as print_func:
formatter.write(line, source)
if tee:
assert print_func.called
assert print_func.mock_calls == [
mock.call(line, end="\n"),
mock.call(source, end="\n"),
]
else:
assert not print_func.called
formatter.write(line, source)
if tee:
assert capsys.readouterr().out == f"{line}\n{source}\n"
else:
assert capsys.readouterr().out == ""

assert filemock.write.called is True
assert filemock.write.call_count == 2
Expand All @@ -130,21 +125,15 @@ def test_write_uses_an_output_file(tee):
]


@mock.patch("flake8.formatting.base.print")
def test_write_uses_print(print_function):
def test_write_uses_print(capsys):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should rename this too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah good point!

"""Verify that we use the print function without an output file."""
line = "Something to write"
source = "source"

formatter = base.BaseFormatter(options())
formatter.write(line, source)

assert print_function.called is True
assert print_function.call_count == 2
assert print_function.mock_calls == [
mock.call(line, end="\n"),
mock.call(source, end="\n"),
]
assert capsys.readouterr().out == f"{line}\n{source}\n"


class AfterInitFormatter(base.BaseFormatter):
Expand Down