codekingpro/portable-devtools
114k
1from __future__ import annotations2 3from typing import IO, Callable4 5 6def get_fileno(file_like: IO[str]) -> int | None:7 """Get fileno() from a file, accounting for poorly implemented file-like objects.8 9 Args:10 file_like (IO): A file-like object.11 12 Returns:13 int | None: The result of fileno if available, or None if operation failed.14 """15 fileno: Callable[[], int] | None = getattr(file_like, "fileno", None)16 if fileno is not None:17 try:18 return fileno()19 except Exception:20 # `fileno` is documented as potentially raising a OSError21 # Alas, from the issues, there are so many poorly implemented file-like objects,22 # that `fileno()` can raise just about anything.23 return None24 return None25 