]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-141570: can_colorize: Expect fileno() to raise OSError, as documented ...
authorVictor Stinner <vstinner@python.org>
Wed, 19 Nov 2025 14:28:31 +0000 (15:28 +0100)
committerGitHub <noreply@github.com>
Wed, 19 Nov 2025 14:28:31 +0000 (14:28 +0000)
gh-141570: can_colorize: Expect fileno() to raise OSError, as documented (#141716)

In Fedora, we've been given a slightly incomplete reproducer for a problematic
Python 3.14 color-related change in argparse that leads to an exception when
Python is used from mod_wsgi: https://bugzilla.redhat.com/2414940

mod_wsgi replaces sys.stdout with a custom object that raises OSError on .fileno():

https://github.com/GrahamDumpleton/mod_wsgi/blob/8460dbfcd5c7108892b3cde9fab7cbc1caa27886/src/server/wsgi_logger.c#L434-L440

This should be supported, as the documentation of fileno explicitly says:

> An OSError is raised if the IO object does not use a file descriptor.

https://docs.python.org/3.14/library/io.html#io.IOBase.fileno

The previously expected exception inherits from OSError,
so it is still expected.

Fixes https://github.com/python/cpython/issues/141570

(cherry picked from commit 96f496a949b05054d0d043c3085f00cec2f83bf5)

Co-authored-by: Miro HronĨok <miro@hroncok.cz>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Lib/_colorize.py
Lib/test/test__colorize.py
Misc/NEWS.d/next/Library/2025-11-18-14-39-31.gh-issue-141570.q3n984.rst [new file with mode: 0644]

index 0688ea00aa5bff66aafaf50012c3f07865d88638..8263d2df6ecd991658748b100609d3f0e5810f04 100644 (file)
@@ -1,5 +1,4 @@
 from __future__ import annotations
-import io
 import os
 import sys
 
@@ -116,5 +115,5 @@ def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
 
     try:
         return os.isatty(file.fileno())
-    except io.UnsupportedOperation:
+    except OSError:
         return hasattr(file, "isatty") and file.isatty()
index b2f0bb1386fe5b486368119c3437f9900fdbda76..5c3743677534d4e9419fa3379ad386b4fbce7231 100644 (file)
@@ -129,6 +129,17 @@ class TestColorizeFunction(unittest.TestCase):
                 file.isatty.return_value = False
                 self.assertEqual(_colorize.can_colorize(file=file), False)
 
+            # The documentation for file.fileno says:
+            # > An OSError is raised if the IO object does not use a file descriptor.
+            # gh-141570: Check OSError is caught and handled
+            with unittest.mock.patch("os.isatty", side_effect=ZeroDivisionError):
+                file = unittest.mock.MagicMock()
+                file.fileno.side_effect = OSError
+                file.isatty.return_value = True
+                self.assertEqual(_colorize.can_colorize(file=file), True)
+                file.isatty.return_value = False
+                self.assertEqual(_colorize.can_colorize(file=file), False)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2025-11-18-14-39-31.gh-issue-141570.q3n984.rst b/Misc/NEWS.d/next/Library/2025-11-18-14-39-31.gh-issue-141570.q3n984.rst
new file mode 100644 (file)
index 0000000..8f4641c
--- /dev/null
@@ -0,0 +1,2 @@
+Support :term:`file-like object` raising :exc:`OSError` from :meth:`~io.IOBase.fileno` in color
+detection (``_colorize.can_colorize()``). This can occur when ``sys.stdout`` is redirected.