]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129061: Fix `FORCE_COLOR` and `NO_COLOR` when empty strings (#129140)
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Mon, 27 Jan 2025 14:24:10 +0000 (16:24 +0200)
committerGitHub <noreply@github.com>
Mon, 27 Jan 2025 14:24:10 +0000 (16:24 +0200)
Lib/_colorize.py
Lib/test/test__colorize.py
Misc/NEWS.d/next/Library/2025-01-21-18-52-32.gh-issue-129061.4idD_B.rst [new file with mode: 0644]

index 5e36e45734a5fb9c431775e6d80bafbc7d472579..41e818f2a747ffde0cbb4a78b0e22eb2fb9ec01e 100644 (file)
@@ -42,11 +42,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
index ff15d83cb078fef474379c16c64d8aaffe32c650..056a5306ced1832217e2c7b29e20e3d4dae6a7c9 100644 (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)
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 (file)
index 0000000..5c5c05e
--- /dev/null
@@ -0,0 +1 @@
+Fix FORCE_COLOR and NO_COLOR when empty strings. Patch by Hugo van Kemenade.