[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 9546fe2ef2)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-01-27 15:48:05 +01:00 committed by GitHub
parent 98c25b8e13
commit c7f9e7446c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 5 additions and 2 deletions

View file

@ -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

View file

@ -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)

View file

@ -0,0 +1 @@
Fix FORCE_COLOR and NO_COLOR when empty strings. Patch by Hugo van Kemenade.