Skip to content

Commit

Permalink
Repair colorama wrapping on non-Windows platforms
Browse files Browse the repository at this point in the history
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 psf#1664.

The solution is to only assign the no-op detach() method if the stream
lacks its own detach() method.

Repairs psf#1664
  • Loading branch information
jpgrayson committed Sep 8, 2020
1 parent 6284953 commit 47639f9
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/black/__init__.py
Expand Up @@ -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

Expand Down

0 comments on commit 47639f9

Please sign in to comment.