summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>2024-12-12 21:11:20 (GMT)
committerGitHub <noreply@github.com>2024-12-12 21:11:20 (GMT)
commita8ffe661548e16ad02dbe6cb8a89513d7ed2a42c (patch)
treea5d8ea0221db8939f00540d5b65c3241369aecaf
parented037d229f64db90aea00f397e9ce1b2f4a22d3f (diff)
downloadcpython-a8ffe661548e16ad02dbe6cb8a89513d7ed2a42c.zip
cpython-a8ffe661548e16ad02dbe6cb8a89513d7ed2a42c.tar.gz
cpython-a8ffe661548e16ad02dbe6cb8a89513d7ed2a42c.tar.bz2
Revert "gh-127353: Allow to force color output on Windows (#127354)" (#127889)
This reverts commit 365451e28368db46ae89a3a990d85c10c2284aa2.
-rw-r--r--Lib/_colorize.py17
-rw-r--r--Lib/test/test__colorize.py37
-rw-r--r--Misc/NEWS.d/next/Windows/2024-11-28-15-55-48.gh-issue-127353.i-XOXg.rst2
3 files changed, 8 insertions, 48 deletions
diff --git a/Lib/_colorize.py b/Lib/_colorize.py
index 709081e..845fb57 100644
--- a/Lib/_colorize.py
+++ b/Lib/_colorize.py
@@ -32,6 +32,14 @@ def get_colors(colorize: bool = False) -> ANSIColors:
def can_colorize() -> bool:
+ if sys.platform == "win32":
+ try:
+ import nt
+
+ if not nt._supports_virtual_terminal():
+ return False
+ except (ImportError, AttributeError):
+ return False
if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
@@ -50,15 +58,6 @@ def can_colorize() -> bool:
if not hasattr(sys.stderr, "fileno"):
return False
- if sys.platform == "win32":
- try:
- import nt
-
- if not nt._supports_virtual_terminal():
- return False
- except (ImportError, AttributeError):
- return False
-
try:
return os.isatty(sys.stderr.fileno())
except io.UnsupportedOperation:
diff --git a/Lib/test/test__colorize.py b/Lib/test/test__colorize.py
index 7a65d63..d55b97a 100644
--- a/Lib/test/test__colorize.py
+++ b/Lib/test/test__colorize.py
@@ -50,47 +50,10 @@ class TestColorizeFunction(unittest.TestCase):
with unittest.mock.patch("os.environ",
{'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
self.assertEqual(_colorize.can_colorize(), False)
- with unittest.mock.patch("os.environ", {}):
- self.assertEqual(_colorize.can_colorize(), True)
-
isatty_mock.return_value = False
with unittest.mock.patch("os.environ", {}):
self.assertEqual(_colorize.can_colorize(), False)
- @force_not_colorized
- @unittest.skipUnless(sys.platform == "win32", "Windows only")
- def test_colorized_detection_checks_for_environment_variables_no_vt(self):
- with (unittest.mock.patch("nt._supports_virtual_terminal", return_value=False),
- unittest.mock.patch("os.isatty") as isatty_mock,
- unittest.mock.patch("sys.flags", unittest.mock.MagicMock(ignore_environment=False)),
- unittest.mock.patch("_colorize.can_colorize", ORIGINAL_CAN_COLORIZE)):
- isatty_mock.return_value = True
- with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
- self.assertEqual(_colorize.can_colorize(), False)
- with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}):
- self.assertEqual(_colorize.can_colorize(), True)
- with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}):
- self.assertEqual(_colorize.can_colorize(), False)
- with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
- self.assertEqual(_colorize.can_colorize(), False)
- with unittest.mock.patch("os.environ",
- {'NO_COLOR': '1', "PYTHON_COLORS": '1'}):
- self.assertEqual(_colorize.can_colorize(), True)
- with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
- self.assertEqual(_colorize.can_colorize(), True)
- with unittest.mock.patch("os.environ",
- {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
- self.assertEqual(_colorize.can_colorize(), False)
- with unittest.mock.patch("os.environ",
- {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}):
- self.assertEqual(_colorize.can_colorize(), False)
- with unittest.mock.patch("os.environ", {}):
- self.assertEqual(_colorize.can_colorize(), False)
-
- isatty_mock.return_value = False
- with unittest.mock.patch("os.environ", {}):
- self.assertEqual(_colorize.can_colorize(), False)
-
if __name__ == "__main__":
unittest.main()
diff --git a/Misc/NEWS.d/next/Windows/2024-11-28-15-55-48.gh-issue-127353.i-XOXg.rst b/Misc/NEWS.d/next/Windows/2024-11-28-15-55-48.gh-issue-127353.i-XOXg.rst
deleted file mode 100644
index 88661b9..0000000
--- a/Misc/NEWS.d/next/Windows/2024-11-28-15-55-48.gh-issue-127353.i-XOXg.rst
+++ /dev/null
@@ -1,2 +0,0 @@
-Allow to force color output on Windows using environment variables. Patch by
-Andrey Efremov.