Skip to content

Commit

Permalink
Use '__IPYTHON__' flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Oct 15, 2023
1 parent c443a85 commit c33b17c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion loguru/_colorama.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
import os
import sys

Expand All @@ -6,7 +7,7 @@ def should_colorize(stream):
if stream is None:
return False

if "IPython" in sys.modules and (stream is sys.stdout or stream is sys.stderr):
if getattr(builtins, "__IPYTHON__", False) and (stream is sys.stdout or stream is sys.stderr):
try:
import ipykernel
import IPython
Expand Down
4 changes: 3 additions & 1 deletion tests/test_colorama.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
import os
import sys
from unittest.mock import MagicMock
Expand Down Expand Up @@ -172,6 +173,7 @@ class Shell:
ipykernel.zmqshell.ZMQInteractiveShell = Shell
ipykernel.iostream.OutStream = out_class

monkeypatch.setattr(builtins, "__IPYTHON__", True, raising=False)
monkeypatch.setitem(sys.modules, "IPython", ipython)
monkeypatch.setitem(sys.modules, "ipykernel", ipykernel)
monkeypatch.setattr(sys, patched, stream, raising=False)
Expand All @@ -183,7 +185,7 @@ def test_jupyter_missing_lib(monkeypatch):
# Missing ipykernal so jupyter block will err, should handle gracefully
stream = StreamIsattyFalse()

monkeypatch.setitem(sys.modules, "IPython", MagicMock())
monkeypatch.setattr(builtins, "__IPYTHON__", True, raising=False)
monkeypatch.setattr(sys, "stdout", stream)

assert should_colorize(stream) is False
Expand Down

0 comments on commit c33b17c

Please sign in to comment.