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

Fix printing of emoji on Windows when stdout is redirected #3374

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

<!-- Changes to Black's terminal output and error messages -->

- Fix emoji rendering on Windows 10+ when standard output is redirected or captured by
forcing UTF-8 encoding in this case (#3374)
- Verbose logging now shows the values of `pyproject.toml` configuration variables
(#3392)

Expand Down
16 changes: 16 additions & 0 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,22 @@ def patch_click() -> None:


def patched_main() -> None:
#: Fixes errors with emoji in Windows terminals when output is redirected
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#: Fixes errors with emoji in Windows terminals when output is redirected
#: Fixes errors with emoji in Windows terminals when output is redirected

# (i.e. pre-commit): https://github.com/psf/black/issues/3156
# To be as safe as possible, we only enable this for "Windows 10 October
# 2018 Update (version 1809)" and up.
if (
"pytest" not in sys.modules
and platform.system() == "Windows"
and tuple(map(int, platform.version().split("."))) >= (10, 0, 1809)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't decide if I love or hate that last line. Advantages:

  • it's correct (as it uses int comparisons);
  • it performs well (as the and short circuits for non-Windows users);
  • fits in a single line.

The disadvantage is that it reads pretty clumsily ("make a tuple out of an int map of the string that platform.version() returns split by the period... and then compare it to (10, 0, 1809)"). Especially that int map bit.

But I guess the advantages outweigh my personal preference here.

Copy link
Author

Choose a reason for hiding this comment

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

Pretty much sums up my feelings when writing it!

):
sys.stdout = io.TextIOWrapper(
sys.stdout.buffer, encoding="utf-8"
) # pragma: nocover
sys.stderr = io.TextIOWrapper(
sys.stderr.buffer, encoding="utf-8"
) # pragma: nocover

# PyInstaller patches multiprocessing to need freeze_support() even in non-Windows
# environments so just assume we always need to call it if frozen.
if getattr(sys, "frozen", False):
Expand Down