From 47639f9a27f4308fdf066375e7f805ff88d47652 Mon Sep 17 00:00:00 2001 From: Pete Grayson Date: Wed, 2 Sep 2020 17:38:59 -0400 Subject: [PATCH] Repair colorama wrapping on non-Windows platforms The wrap_stream_for_windows() function calls colorama.initialise.wrap_stream() function to apply colorama's magic to wrapper to the output stream. Except this wrapper is only applied on Windows platforms that need it, otherwise the original stream is returned as-is. The colorama wrapped stream lacks a detach() method, so a no-op lambda was being assigned to the wrapped stream. The problem is that the no-op lambda was being assigned unconditionally whether or not colorama actually returns a wrapped stream, thus replacing the original TextIOWrapper's detach() method. Replacing the detach() method with a no-op lambda is the root cause of the problem observed in #1664. The solution is to only assign the no-op detach() method if the stream lacks its own detach() method. Repairs #1664 --- src/black/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 3753d5fca8d..645ca0afe9a 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -880,7 +880,8 @@ def wrap_stream_for_windows( # wrap_stream returns a `colorama.AnsiToWin32.AnsiToWin32` object # which does not have a `detach()` method. So we fake one. - f.detach = lambda *args, **kwargs: None # type: ignore + if not hasattr(f, "detach"): + f.detach = lambda: None # type: ignore except ImportError: pass