← mined_oracle

rich_2678

resolved RESOLVED UNSUBMITTED PASS · None tool calls · 0 s · Textualize/rich

Task input

(not found in data/tasks.jsonl)

Tool calls (0)

#ToolArgumentsResult
No trace captured.

Patch

--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,9 +12,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Bumped minimum Python version to 3.7 https://github.com/Textualize/rich/pull/2567
 - Pretty-printing of "tagged" `__repr__` results is now greedy when matching tags https://github.com/Textualize/rich/pull/2565
 
+### Fixed
+
+- Handling of broken `fileno` made more robust. Fixes https://github.com/Textualize/rich/issues/2645
+
 ### Added
+
 - Add type annotation for key_separator of pretty.Node https://github.com/Textualize/rich/issues/2625
 
+
 ## [12.6.0] - 2022-10-02
 
 ### Added
--- /dev/null
+++ b/rich/_fileno.py
@@ -0,0 +1,24 @@
+from __future__ import annotations
+
+from typing import IO, Callable
+
+
+def get_fileno(file_like: IO[str]) -> int | None:
+    """Get fileno() from a file, accounting for poorly implemented file-like objects.
+
+    Args:
+        file_like (IO): A file-like object.
+
+    Returns:
+        int | None: The result of fileno if available, or None if operation failed.
+    """
+    fileno: Callable[[], int] | None = getattr(file_like, "fileno", None)
+    if fileno is not None:
+        try:
+            return fileno()
+        except Exception:
+            # `fileno` is documented as potentially raising a OSError
+            # Alas, from the issues, there are so many poorly implemented file-like objects,
+            # that `fileno()` can raise just about anything.
+            return None
+    return None
--- a/rich/console.py
+++ b/rich/console.py
@@ -1,5 +1,4 @@
 import inspect
-import io
 import os
 import platform
 import sys
@@ -48,6 +47,7 @@ else:
 from . import errors, themes
 from ._emoji_replace import _emoji_replace
 from ._export_format import CONSOLE_HTML_FORMAT, CONSOLE_SVG_FORMAT
+from ._fileno import get_fileno
 from ._log_render import FormatTimeCallable, LogRender
 from .align import Align, AlignMethod
 from .color import ColorSystem, blend_rgb
@@ -2006,12 +2006,11 @@ class Console:
                     if WINDOWS:
                         use_legacy_windows_render = False
                         if self.legacy_windows:
-                            try:
+                            fileno = get_fileno(self.file)
+                            if fileno is not None:
                                 use_legacy_windows_render = (
-                                    self.file.fileno() in _STD_STREAMS_OUTPUT
+                                    fileno in _STD_STREAMS_OUTPUT
                                 )
-                            except (ValueError, io.UnsupportedOperation):
-                                pass
 
                         if use_legacy_windows_render:
                             from rich._win32_console import LegacyWindowsTerm

Test output

show
...                                                                      [100%]
3 passed in 0.01s