Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_vendor/rich/_fileno.py: 30%

10 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:48 +0000

1from __future__ import annotations 

2 

3from typing import IO, Callable 

4 

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 OSError 

21 # Alas, from the issues, there are so many poorly implemented file-like objects, 

22 # that `fileno()` can raise just about anything. 

23 return None 

24 return None