Skip to content

Commit 88f4b92

Browse files
committed
Only apply ANSI_COLORS_DISABLED when present and not an empty string
1 parent 7a470aa commit 88f4b92

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ In order of precedence:
9898

9999
1. Calling `colored` or `cprint` with a truthy `no_color` disables colour.
100100
2. Calling `colored` or `cprint` with a truthy `force_color` forces colour.
101-
3. Setting the `ANSI_COLORS_DISABLED` environment variable to any value disables colour.
101+
3. Setting the `ANSI_COLORS_DISABLED` environment variable to any non-empty value
102+
disables colour.
102103
4. Setting the [`NO_COLOR`](https://no-color.org/) environment variable to any non-empty
103104
value disables colour.
104105
5. Setting the [`FORCE_COLOR`](https://force-color.org/) environment variable to any

src/termcolor/termcolor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _can_do_colour(
120120
return True
121121

122122
# Then check env vars:
123-
if "ANSI_COLORS_DISABLED" in os.environ:
123+
if os.environ.get("ANSI_COLORS_DISABLED"):
124124
return False
125125
if os.environ.get("NO_COLOR"):
126126
return False

tests/test_termcolor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,14 @@ def test_environment_variables_force_color_empty_string(
215215
[
216216
# Set only env vars
217217
(None, None, ["ANSI_COLORS_DISABLED=1"], False),
218+
(None, None, ["ANSI_COLORS_DISABLED="], False),
218219
(None, None, ["NO_COLOR=1"], False),
219220
(None, None, ["NO_COLOR="], False),
220221
(None, None, ["FORCE_COLOR=1"], True),
221222
(None, None, ["FORCE_COLOR="], False),
222223
(None, None, ["ANSI_COLORS_DISABLED=1", "NO_COLOR=1", "FORCE_COLOR=1"], False),
224+
(None, None, ["ANSI_COLORS_DISABLED=1", "FORCE_COLOR=1"], False),
225+
(None, None, ["ANSI_COLORS_DISABLED=", "FORCE_COLOR=1"], True),
223226
(None, None, ["NO_COLOR=1", "FORCE_COLOR=1"], False),
224227
(None, None, ["NO_COLOR=1", "FORCE_COLOR="], False),
225228
(None, None, ["TERM=dumb"], False),

0 commit comments

Comments
 (0)