Skip to content

Commit 8c79688

Browse files
miss-islingtonhroncokcmaloneyvstinner
authored
[3.14] gh-141570: can_colorize: Expect fileno() to raise OSError, as documented (GH-141716) (#141747)
gh-141570: can_colorize: Expect fileno() to raise OSError, as documented (GH-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 #141570 (cherry picked from commit 96f496a) Co-authored-by: Miro Hrončok <[email protected]> Co-authored-by: Cody Maloney <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 562e23f commit 8c79688

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/_colorize.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import os
32
import sys
43

@@ -312,7 +311,7 @@ def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
312311

313312
try:
314313
return os.isatty(file.fileno())
315-
except io.UnsupportedOperation:
314+
except OSError:
316315
return hasattr(file, "isatty") and file.isatty()
317316

318317

Lib/test/test__colorize.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ def test_colorized_detection_checks_for_file(self):
165165
file.isatty.return_value = False
166166
self.assertEqual(_colorize.can_colorize(file=file), False)
167167

168+
# The documentation for file.fileno says:
169+
# > An OSError is raised if the IO object does not use a file descriptor.
170+
# gh-141570: Check OSError is caught and handled
171+
with unittest.mock.patch("os.isatty", side_effect=ZeroDivisionError):
172+
file = unittest.mock.MagicMock()
173+
file.fileno.side_effect = OSError
174+
file.isatty.return_value = True
175+
self.assertEqual(_colorize.can_colorize(file=file), True)
176+
file.isatty.return_value = False
177+
self.assertEqual(_colorize.can_colorize(file=file), False)
178+
168179

169180
if __name__ == "__main__":
170181
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Support :term:`file-like object` raising :exc:`OSError` from :meth:`~io.IOBase.fileno` in color
2+
detection (``_colorize.can_colorize()``). This can occur when ``sys.stdout`` is redirected.

0 commit comments

Comments
 (0)