Skip to content

Commit da5f9ea

Browse files
committed
Simplify the decision tree for returning a binary filelike object
1 parent 15fe876 commit da5f9ea

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/xopen/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,12 @@ def _file_or_path_to_binary_stream(
660660
return _open_stdin_or_out(binary_mode), False
661661
if isinstance(file_or_path, (str, bytes)) or hasattr(file_or_path, "__fspath__"):
662662
return open(os.fspath(file_or_path), binary_mode), True # type: ignore
663-
if isinstance(file_or_path, (io.BufferedReader, io.BufferedWriter)):
664-
return file_or_path, False
665663
if isinstance(file_or_path, io.TextIOWrapper):
666664
return file_or_path.buffer, False
667-
if isinstance(file_or_path, io.IOBase) and not hasattr(file_or_path, "encoding"):
668-
# Text files have encoding attributes. This file is binary:
669-
return file_or_path, False
665+
if hasattr(file_or_path, "readinto") or hasattr(file_or_path, "write"):
666+
# Very lenient fallback for all filelike objects. If the filelike
667+
# object is not binary, this will crash at a later point.
668+
return file_or_path, False # type: ignore
670669
raise TypeError(
671670
f"Unsupported type for {file_or_path}, " f"{file_or_path.__class__.__name__}."
672671
)

0 commit comments

Comments
 (0)