mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
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():
8460dbfcd5/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
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
e2178743fe
commit
96f496a949
3 changed files with 14 additions and 2 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
@ -330,7 +329,7 @@ def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return os.isatty(file.fileno())
|
return os.isatty(file.fileno())
|
||||||
except io.UnsupportedOperation:
|
except OSError:
|
||||||
return hasattr(file, "isatty") and file.isatty()
|
return hasattr(file, "isatty") and file.isatty()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,17 @@ def test_colorized_detection_checks_for_file(self):
|
||||||
file.isatty.return_value = False
|
file.isatty.return_value = False
|
||||||
self.assertEqual(_colorize.can_colorize(file=file), 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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -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…
Add table
Add a link
Reference in a new issue