]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-128595: Default to stdout isatty for colour detection instead of stderr...
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Tue, 21 Jan 2025 16:14:24 +0000 (18:14 +0200)
committerGitHub <noreply@github.com>
Tue, 21 Jan 2025 16:14:24 +0000 (18:14 +0200)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Fix `test__colorize` unexpected keyword argument 'file' on buildbots (#129070)

Lib/_colorize.py
Lib/doctest.py
Lib/test/support/__init__.py
Lib/test/test__colorize.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2025-01-07-21-48-32.gh-issue-128498.n6jtlW.rst [new file with mode: 0644]

index 709081e25ec59bf80273f6a91daafcefeac7229b..daecf5b16c196536fb5bbcfde4f6ffb5075f9fd1 100644 (file)
@@ -24,14 +24,17 @@ for attr in dir(NoColors):
         setattr(NoColors, attr, "")
 
 
-def get_colors(colorize: bool = False) -> ANSIColors:
-    if colorize or can_colorize():
+def get_colors(colorize: bool = False, *, file=None) -> ANSIColors:
+    if colorize or can_colorize(file=file):
         return ANSIColors()
     else:
         return NoColors
 
 
-def can_colorize() -> bool:
+def can_colorize(*, file=None) -> bool:
+    if file is None:
+        file = sys.stdout
+
     if not sys.flags.ignore_environment:
         if os.environ.get("PYTHON_COLORS") == "0":
             return False
@@ -47,7 +50,7 @@ def can_colorize() -> bool:
         if os.environ.get("TERM") == "dumb":
             return False
 
-    if not hasattr(sys.stderr, "fileno"):
+    if not hasattr(file, "fileno"):
         return False
 
     if sys.platform == "win32":
@@ -60,6 +63,6 @@ def can_colorize() -> bool:
             return False
 
     try:
-        return os.isatty(sys.stderr.fileno())
+        return os.isatty(file.fileno())
     except io.UnsupportedOperation:
-        return sys.stderr.isatty()
+        return file.isatty()
index c531e3ca6a3d5e3612a4f5f86b6b41ad5e9fc653..dd4d62a210a90254b8635b47974daa12f95b1268 100644 (file)
@@ -1558,7 +1558,7 @@ class DocTestRunner:
         save_displayhook = sys.displayhook
         sys.displayhook = sys.__displayhook__
         saved_can_colorize = _colorize.can_colorize
-        _colorize.can_colorize = lambda: False
+        _colorize.can_colorize = lambda *args, **kwargs: False
         color_variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
         for key in color_variables:
             color_variables[key] = os.environ.pop(key, None)
index 1e711ef32bc7963a5a21ebf73f44c789f5b8c501..e6a8ef1ddcc14dd8d8d04093a3f95136eeca136e 100644 (file)
@@ -2700,7 +2700,7 @@ def no_color():
     from .os_helper import EnvironmentVarGuard
 
     with (
-        swap_attr(_colorize, "can_colorize", lambda: False),
+        swap_attr(_colorize, "can_colorize", lambda file=None: False),
         EnvironmentVarGuard() as env,
     ):
         for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
index 1871775fa205a268db969b95d4ea3319f1de5198..77e74fa3e23c2c821830528cfe20a7502c63415a 100644 (file)
@@ -9,7 +9,7 @@ ORIGINAL_CAN_COLORIZE = _colorize.can_colorize
 
 
 def setUpModule():
-    _colorize.can_colorize = lambda: False
+    _colorize.can_colorize = lambda *args, **kwargs: False
 
 
 def tearDownModule():
@@ -21,6 +21,7 @@ class TestColorizeFunction(unittest.TestCase):
     def test_colorized_detection_checks_for_environment_variables(self):
         flags = unittest.mock.MagicMock(ignore_environment=False)
         with (unittest.mock.patch("os.isatty") as isatty_mock,
+              unittest.mock.patch("sys.stdout") as stdout_mock,
               unittest.mock.patch("sys.stderr") as stderr_mock,
               unittest.mock.patch("sys.flags", flags),
               unittest.mock.patch("_colorize.can_colorize", ORIGINAL_CAN_COLORIZE),
@@ -29,6 +30,8 @@ class TestColorizeFunction(unittest.TestCase):
                contextlib.nullcontext()) as vt_mock):
 
             isatty_mock.return_value = True
+            stdout_mock.fileno.return_value = 1
+            stdout_mock.isatty.return_value = True
             stderr_mock.fileno.return_value = 2
             stderr_mock.isatty.return_value = True
             with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
@@ -61,6 +64,7 @@ class TestColorizeFunction(unittest.TestCase):
                     self.assertEqual(_colorize.can_colorize(), True)
 
                 isatty_mock.return_value = False
+                stdout_mock.isatty.return_value = False
                 stderr_mock.isatty.return_value = False
                 self.assertEqual(_colorize.can_colorize(), False)
 
index f73149271b9bc9f9fc86bc44f96c5143cf771be0..947c3e82b8c00484e3b1fbd51fd3f14b877befe4 100644 (file)
@@ -135,7 +135,7 @@ BUILTIN_EXCEPTION_LIMIT = object()
 
 def _print_exception_bltin(exc, /):
     file = sys.stderr if sys.stderr is not None else sys.__stderr__
-    colorize = _colorize.can_colorize()
+    colorize = _colorize.can_colorize(file=file)
     return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
 
 
diff --git a/Misc/NEWS.d/next/Library/2025-01-07-21-48-32.gh-issue-128498.n6jtlW.rst b/Misc/NEWS.d/next/Library/2025-01-07-21-48-32.gh-issue-128498.n6jtlW.rst
new file mode 100644 (file)
index 0000000..9a241e3
--- /dev/null
@@ -0,0 +1,2 @@
+Default to stdout isatty for color detection instead of stderr. Patch by
+Hugo van Kemenade.