From baa1a5499adff648f3784ac5621545d13417534a Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 21 Jan 2025 21:00:09 +0200 Subject: [PATCH] debug --- Lib/_colorize.py | 10 +++++---- Lib/test/test__colorize.py | 46 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Lib/_colorize.py b/Lib/_colorize.py index 41e818f2a747ffd..20919fa610ea49b 100644 --- a/Lib/_colorize.py +++ b/Lib/_colorize.py @@ -41,13 +41,13 @@ def can_colorize(*, file=None) -> bool: if os.environ.get("PYTHON_COLORS") == "0": return False if os.environ.get("PYTHON_COLORS") == "1": - return True + return 1 if os.environ.get("NO_COLOR"): return False if not COLORIZE: return False if os.environ.get("FORCE_COLOR"): - return True + return 2 if os.environ.get("TERM") == "dumb": return False @@ -64,6 +64,8 @@ def can_colorize(*, file=None) -> bool: return False try: - return os.isatty(file.fileno()) + if os.isatty(file.fileno()): return 3 + else: return False except io.UnsupportedOperation: - return file.isatty() + if file.isatty(): return 4 + else: return False diff --git a/Lib/test/test__colorize.py b/Lib/test/test__colorize.py index 3c401157423d355..5918359efb00f21 100644 --- a/Lib/test/test__colorize.py +++ b/Lib/test/test__colorize.py @@ -43,25 +43,25 @@ def test_colorized_detection_checks_for_environment_variables(self): stderr_mock.fileno.return_value = 2 stderr_mock.isatty.return_value = True - for env_vars, expected in [ - ({"TERM": "dumb"}, False), - ({"PYTHON_COLORS": "1"}, True), - ({"PYTHON_COLORS": "0"}, False), - ({"NO_COLOR": "1"}, False), - ({"NO_COLOR": "0"}, False), - ({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True), - ({"FORCE_COLOR": "1"}, True), - ({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False), - ({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False), - ({"FORCE_COLOR": "1", "NO_COLOR": ""}, True), - ({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False), - ({"FORCE_COLOR": "", "NO_COLOR": "1"}, False), - ({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False), - ({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False), - ]: - with self.subTest(env_vars=env_vars, expected=expected): - with unittest.mock.patch("os.environ", env_vars): - self.assertEqual(_colorize.can_colorize(), expected) + # for env_vars, expected in [ + # ({"TERM": "dumb"}, False), + # ({"PYTHON_COLORS": "1"}, True), + # ({"PYTHON_COLORS": "0"}, False), + # ({"NO_COLOR": "1"}, False), + # ({"NO_COLOR": "0"}, False), + # ({"NO_COLOR": "1", "PYTHON_COLORS": "1"}, True), + # ({"FORCE_COLOR": "1"}, True), + # ({"FORCE_COLOR": "1", "NO_COLOR": "1"}, False), + # ({"FORCE_COLOR": "1", "NO_COLOR": "0"}, False), + # ({"FORCE_COLOR": "1", "NO_COLOR": ""}, True), + # ({"FORCE_COLOR": "0", "NO_COLOR": "1"}, False), + # ({"FORCE_COLOR": "", "NO_COLOR": "1"}, False), + # ({"FORCE_COLOR": "1", "PYTHON_COLORS": "0"}, False), + # ({"FORCE_COLOR": "0", "PYTHON_COLORS": "0"}, False), + # ]: + # with self.subTest(env_vars=env_vars, expected=expected): + # with unittest.mock.patch("os.environ", env_vars): + # self.assertEqual(_colorize.can_colorize(), expected) with unittest.mock.patch("os.environ", {}): if sys.platform == "win32": @@ -69,8 +69,8 @@ def test_colorized_detection_checks_for_environment_variables(self): vt_mock.return_value = True self.assertEqual(_colorize.can_colorize(), True) - else: - self.assertEqual(_colorize.can_colorize(), True) + # else: + # self.assertEqual(_colorize.can_colorize(), True) with unittest.mock.patch("os.environ", {"NO_COLOR": ""}): if sys.platform == "win32": @@ -78,8 +78,8 @@ def test_colorized_detection_checks_for_environment_variables(self): vt_mock.return_value = True self.assertEqual(_colorize.can_colorize(), True) - else: - self.assertEqual(_colorize.can_colorize(), True) + # else: + # self.assertEqual(_colorize.can_colorize(), True) isatty_mock.return_value = False stdout_mock.isatty.return_value = False