@@ -660,13 +660,12 @@ def _file_or_path_to_binary_stream(
660
660
return _open_stdin_or_out (binary_mode ), False
661
661
if isinstance (file_or_path , (str , bytes )) or hasattr (file_or_path , "__fspath__" ):
662
662
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
665
663
if isinstance (file_or_path , io .TextIOWrapper ):
666
664
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
670
669
raise TypeError (
671
670
f"Unsupported type for { file_or_path } , " f"{ file_or_path .__class__ .__name__ } ."
672
671
)
0 commit comments