Skip to content

Commit b67ac03

Browse files
committed
Fix FORCE_COLOR and NO_COLOR when empty strings
1 parent f5abd6e commit b67ac03

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/_colorize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def can_colorize(*, file=None) -> bool:
4242
return False
4343
if os.environ.get("PYTHON_COLORS") == "1":
4444
return True
45-
if "NO_COLOR" in os.environ:
45+
if os.environ.get("NO_COLOR"):
4646
return False
4747
if not COLORIZE:
4848
return False
49-
if "FORCE_COLOR" in os.environ:
49+
if os.environ.get("FORCE_COLOR"):
5050
return True
5151
if os.environ.get("TERM") == "dumb":
5252
return False

Lib/test/test__colorize.py

+7
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ def test_colorized_detection_checks_for_environment_variables(self):
4848
({"PYTHON_COLORS": "1"}, True),
4949
({"PYTHON_COLORS": "0"}, False),
5050
({"NO_COLOR": "1"}, False),
51+
({"NO_COLOR": "0"}, False),
52+
({"NO_COLOR": ""}, True),
5153
({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True),
5254
({"FORCE_COLOR": "1"}, True),
5355
({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False),
56+
({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False),
57+
({"FORCE_COLOR": "1", "NO_COLOR": ""}, True),
58+
({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False),
59+
({"FORCE_COLOR": "", "NO_COLOR": "1"}, False),
5460
({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False),
61+
({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False),
5562
]:
5663
with self.subTest(env_vars=env_vars, expected=expected):
5764
with unittest.mock.patch("os.environ", env_vars):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix FORCE_COLOR and NO_COLOR when empty strings. Patch by Hugo van Kemenade.

0 commit comments

Comments
 (0)