Skip to content

Commit

Permalink
Fix error if non-compatible stream wrapped by colorama (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Apr 18, 2020
1 parent e2a337a commit 59207a7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fix the impossibility to ``remove()`` a handler if an exception is raised while the sink' ``stop()`` function is called (`#237 <https://github.com/Delgan/loguru/issues/237>`_).
- Fix file sink left in an unstable state if an exception occurred during ``retention`` or ``compression`` process (`#238 <https://github.com/Delgan/loguru/issues/238>`_).
- Fix situation where changes made to ``record["message"]`` were unexpectedly ignored when ``opt(colors=True)``, causing "out-of-date" ``message`` to be logged due to implementation details (`#221 <https://github.com/Delgan/loguru/issues/221>`_).
- Fix possible exception if a stream having an ``isatty()`` method returning ``True`` but not being compatible with ``colorama`` is used on Windows (`#249 <https://github.com/Delgan/loguru/issues/249>`_).


`0.4.1`_ (2020-01-19)
Expand Down
3 changes: 3 additions & 0 deletions loguru/_colorama.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def should_wrap(stream):
if os.name != "nt":
return False

if stream is not sys.__stdout__ and stream is not sys.__stderr__:
return False

from colorama.win32 import winapi_test

return winapi_test()
Expand Down
29 changes: 21 additions & 8 deletions tests/test_add_option_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ def patch_colorama(monkeypatch):

@pytest.mark.parametrize("colorize", [True, False, None])
@pytest.mark.parametrize("tty", [True, False])
def test_colorize_stream_linux(patch_colorama, monkeypatch, colorize, tty):
monkeypatch.setattr(os, "name", "posix")
@pytest.mark.parametrize("replace_original", [True, False])
def test_colorize_stream(patch_colorama, monkeypatch, colorize, tty, replace_original):
stream = Stream(tty)
logger.add(stream, format="<red>{message}</red>", colorize=colorize)

monkeypatch.delenv("TERM", raising=False)
monkeypatch.delenv("PYCHARM_HOSTED", raising=False)
monkeypatch.setattr(os, "name", "unix")
if replace_original:
monkeypatch.setattr(sys, "__stdout__", stream)

logger.add(stream, format="<red>{message}</red>", colorize=colorize, catch=False)
logger.debug("Message")

winapi_test = patch_colorama.win32.winapi_test
Expand All @@ -90,17 +97,23 @@ def test_colorize_stream_linux(patch_colorama, monkeypatch, colorize, tty):

@pytest.mark.parametrize("colorize", [True, False, None])
@pytest.mark.parametrize("tty", [True, False])
def test_colorize_stream_windows(patch_colorama, monkeypatch, colorize, tty):
monkeypatch.setattr(os, "name", "nt")
@pytest.mark.parametrize("replace_original", [True, False])
def test_colorize_stream_windows(patch_colorama, monkeypatch, colorize, tty, replace_original):
stream = Stream(tty)
logger.add(stream, format="<blue>{message}</blue>", colorize=colorize)

monkeypatch.delenv("TERM", raising=False)
monkeypatch.delenv("PYCHARM_HOSTED", raising=False)
monkeypatch.setattr(os, "name", "nt")
if replace_original:
monkeypatch.setattr(sys, "__stdout__", stream)

logger.add(stream, format="<blue>{message}</blue>", colorize=colorize, catch=False)
logger.debug("Message")
writer = patch_colorama.AnsiToWin32().stream.write.called

winapi_test = patch_colorama.win32.winapi_test
stream_write = patch_colorama.AnsiToWin32().stream.write

if colorize or (colorize is None and tty):
if replace_original and (colorize or (colorize is None and tty)):
assert winapi_test.called
assert stream_write.called
else:
Expand Down

0 comments on commit 59207a7

Please sign in to comment.