From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 27 Jan 2025 14:48:05 +0000 (+0100) Subject: [3.13] gh-129061: Fix `FORCE_COLOR` and `NO_COLOR` when empty strings (GH-129140... X-Git-Tag: v3.13.2~36 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c7f9e7446ce790cc0b133c18802bf4b030d3814f;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-129061: Fix `FORCE_COLOR` and `NO_COLOR` when empty strings (GH-129140) (#129360) 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> --- diff --git a/Lib/_colorize.py b/Lib/_colorize.py index deb2e854ce1f..70acfd4ad0ba 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 ff15d83cb078..056a5306ced1 100644 --- a/Lib/test/test__colorize.py +++ b/Lib/test/test__colorize.py @@ -44,8 +44,10 @@ class TestColorizeFunction(unittest.TestCase): 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 000000000000..5c5c05e1161e --- /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.