From a8eeea93a63cce6e89868cf0a176404667ff3492 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 27 Jan 2025 16:24:10 +0200 Subject: [PATCH] gh-129061: Fix `FORCE_COLOR` and `NO_COLOR` when empty strings (GH-129140) (cherry picked from commit 9546fe2ef2db921709f3cb355b33bba977658672) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Lib/_colorize.py | 4 ++-- Lib/test/test__colorize.py | 2 ++ .../Library/2025-01-21-18-52-32.gh-issue-129061.4idD_B.rst | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-01-21-18-52-32.gh-issue-129061.4idD_B.rst diff --git a/Lib/_colorize.py b/Lib/_colorize.py index deb2e854ce1ff8..70acfd4ad0ba8f 100644 --- a/Lib/_colorize.py +++ b/Lib/_colorize.py @@ -40,11 +40,11 @@ def can_colorize(*, file=None) -> bool: return False if os.environ.get("PYTHON_COLORS") == "1": return True - if "NO_COLOR" in os.environ: + if os.environ.get("NO_COLOR"): return False if not COLORIZE: return False - if "FORCE_COLOR" in os.environ: + if os.environ.get("FORCE_COLOR"): return True if os.environ.get("TERM") == "dumb": return False diff --git a/Lib/test/test__colorize.py b/Lib/test/test__colorize.py index ff15d83cb078fe..056a5306ced183 100644 --- a/Lib/test/test__colorize.py +++ b/Lib/test/test__colorize.py @@ -44,8 +44,10 @@ def check(env, fallback, expected): check({'TERM': ''}, fallback, fallback) check({'FORCE_COLOR': '1'}, fallback, True) check({'FORCE_COLOR': '0'}, fallback, True) + check({'FORCE_COLOR': ''}, fallback, fallback) check({'NO_COLOR': '1'}, fallback, False) check({'NO_COLOR': '0'}, fallback, False) + check({'NO_COLOR': ''}, fallback, fallback) check({'TERM': 'dumb', 'FORCE_COLOR': '1'}, False, True) check({'FORCE_COLOR': '1', 'NO_COLOR': '1'}, True, False) diff --git a/Misc/NEWS.d/next/Library/2025-01-21-18-52-32.gh-issue-129061.4idD_B.rst b/Misc/NEWS.d/next/Library/2025-01-21-18-52-32.gh-issue-129061.4idD_B.rst new file mode 100644 index 00000000000000..5c5c05e1161e86 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-21-18-52-32.gh-issue-129061.4idD_B.rst @@ -0,0 +1 @@ +Fix FORCE_COLOR and NO_COLOR when empty strings. Patch by Hugo van Kemenade.