Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import os
import sys

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

try:
return os.isatty(file.fileno())
except io.UnsupportedOperation:
except OSError:
return hasattr(file, "isatty") and file.isatty()


Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test__colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ def test_colorized_detection_checks_for_file(self):
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()
Original file line number Diff line number Diff line change
@@ -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.
Loading