diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a2d3ecf38..1572d74d2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,7 @@ - Fix the impossibility to ``remove()`` a handler if an exception is raised while the sink' ``stop()`` function is called (`#237 `_). - Fix file sink left in an unstable state if an exception occurred during ``retention`` or ``compression`` process (`#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 `_). +- Fix possible exception if a stream having an ``isatty()`` method returning ``True`` but not being compatible with ``colorama`` is used on Windows (`#249 `_). `0.4.1`_ (2020-01-19) diff --git a/loguru/_colorama.py b/loguru/_colorama.py index ac9e80fa0..4662ab159 100644 --- a/loguru/_colorama.py +++ b/loguru/_colorama.py @@ -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() diff --git a/tests/test_add_option_colorize.py b/tests/test_add_option_colorize.py index c01ae1da7..7152fbca0 100644 --- a/tests/test_add_option_colorize.py +++ b/tests/test_add_option_colorize.py @@ -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="{message}", 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="{message}", colorize=colorize, catch=False) logger.debug("Message") winapi_test = patch_colorama.win32.winapi_test @@ -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="{message}", 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="{message}", 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: