diff --git a/CHANGES.md b/CHANGES.md index 2da0fb4720c..050b4d17c7e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -61,6 +61,8 @@ +- 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) diff --git a/src/black/__init__.py b/src/black/__init__.py index 9f44722bfae..fcebe64e810 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1411,6 +1411,22 @@ def patch_click() -> None: def patched_main() -> None: + #: 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) + ): + 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):