Checks whether a given file-like object is closed. :param obj: The file-like object to check.
(obj: object)
| 7 | |
| 8 | |
| 9 | def is_fp_closed(obj: object) -> bool: |
| 10 | """ |
| 11 | Checks whether a given file-like object is closed. |
| 12 | |
| 13 | :param obj: |
| 14 | The file-like object to check. |
| 15 | """ |
| 16 | |
| 17 | try: |
| 18 | # Check `isclosed()` first, in case Python3 doesn't set `closed`. |
| 19 | # GH Issue #928 |
| 20 | return obj.isclosed() # type: ignore[no-any-return, attr-defined] |
| 21 | except AttributeError: |
| 22 | pass |
| 23 | |
| 24 | try: |
| 25 | # Check via the official file-like-object way. |
| 26 | return obj.closed # type: ignore[no-any-return, attr-defined] |
| 27 | except AttributeError: |
| 28 | pass |
| 29 | |
| 30 | try: |
| 31 | # Check if the object is a container for another file-like object that |
| 32 | # gets released on exhaustion (e.g. HTTPResponse). |
| 33 | return obj.fp is None # type: ignore[attr-defined] |
| 34 | except AttributeError: |
| 35 | pass |
| 36 | |
| 37 | raise ValueError("Unable to determine whether fp is closed.") |
| 38 | |
| 39 | |
| 40 | def assert_header_parsing(headers: httplib.HTTPMessage) -> None: |