Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 21, 2025
1 parent 69c59e2 commit baa1a54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
10 changes: 6 additions & 4 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ def can_colorize(*, file=None) -> bool:
if os.environ.get("PYTHON_COLORS") == "0":
return False
if os.environ.get("PYTHON_COLORS") == "1":
return True
return 1
if os.environ.get("NO_COLOR"):
return False
if not COLORIZE:
return False
if os.environ.get("FORCE_COLOR"):
return True
return 2
if os.environ.get("TERM") == "dumb":
return False

Expand All @@ -64,6 +64,8 @@ def can_colorize(*, file=None) -> bool:
return False

try:
return os.isatty(file.fileno())
if os.isatty(file.fileno()): return 3
else: return False
except io.UnsupportedOperation:
return file.isatty()
if file.isatty(): return 4
else: return False
46 changes: 23 additions & 23 deletions Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,43 @@ def test_colorized_detection_checks_for_environment_variables(self):
stderr_mock.fileno.return_value = 2
stderr_mock.isatty.return_value = True

for env_vars, expected in [
({"TERM": "dumb"}, False),
({"PYTHON_COLORS": "1"}, True),
({"PYTHON_COLORS": "0"}, False),
({"NO_COLOR": "1"}, False),
({"NO_COLOR": "0"}, False),
({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True),
({"FORCE_COLOR": "1"}, True),
({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False),
({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False),
({"FORCE_COLOR": "1", "NO_COLOR": ""}, True),
({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False),
({"FORCE_COLOR": "", "NO_COLOR": "1"}, False),
({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False),
({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False),
]:
with self.subTest(env_vars=env_vars, expected=expected):
with unittest.mock.patch("os.environ", env_vars):
self.assertEqual(_colorize.can_colorize(), expected)
# for env_vars, expected in [
# ({"TERM": "dumb"}, False),
# ({"PYTHON_COLORS": "1"}, True),
# ({"PYTHON_COLORS": "0"}, False),
# ({"NO_COLOR": "1"}, False),
# ({"NO_COLOR": "0"}, False),
# ({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True),
# ({"FORCE_COLOR": "1"}, True),
# ({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False),
# ({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False),
# ({"FORCE_COLOR": "1", "NO_COLOR": ""}, True),
# ({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False),
# ({"FORCE_COLOR": "", "NO_COLOR": "1"}, False),
# ({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False),
# ({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False),
# ]:
# with self.subTest(env_vars=env_vars, expected=expected):
# with unittest.mock.patch("os.environ", env_vars):
# self.assertEqual(_colorize.can_colorize(), expected)

with unittest.mock.patch("os.environ", {}):
if sys.platform == "win32":
self.assertEqual(_colorize.can_colorize(), False)

vt_mock.return_value = True
self.assertEqual(_colorize.can_colorize(), True)
else:
self.assertEqual(_colorize.can_colorize(), True)
# else:
# self.assertEqual(_colorize.can_colorize(), True)

with unittest.mock.patch("os.environ", {"NO_COLOR": ""}):
if sys.platform == "win32":
self.assertEqual(_colorize.can_colorize(), False)

vt_mock.return_value = True
self.assertEqual(_colorize.can_colorize(), True)
else:
self.assertEqual(_colorize.can_colorize(), True)
# else:
# self.assertEqual(_colorize.can_colorize(), True)

isatty_mock.return_value = False
stdout_mock.isatty.return_value = False
Expand Down

0 comments on commit baa1a54

Please sign in to comment.