@@ -993,6 +993,78 @@ def seek(self, offset: int, whence: int) -> bytes:
993
993
994
994
assert file_counter .num_seeks > last_frame_seeks
995
995
996
+ def test_file_like_method_check_fails (self ):
997
+ class ReadMethodMissing :
998
+ def seek (self , offset : int , whence : int ) -> bytes :
999
+ return bytes ()
1000
+
1001
+ with pytest .raises (RuntimeError , match = "must implement a read method" ):
1002
+ create_from_file_like (ReadMethodMissing (), "approximate" )
1003
+
1004
+ class SeekMethodMissing :
1005
+ def read (self , size : int ) -> bytes :
1006
+ return bytes ()
1007
+
1008
+ with pytest .raises (RuntimeError , match = "must implement a seek method" ):
1009
+ create_from_file_like (SeekMethodMissing (), "approximate" )
1010
+
1011
+ class ReadMethodWrongSignature :
1012
+ def __init__ (self , file : io .RawIOBase ):
1013
+ self ._file = file
1014
+
1015
+ # io.RawIOBase says we should accept a single int; wrong signature on purpose
1016
+ def read (self ) -> bytes :
1017
+ return bytes ()
1018
+
1019
+ def seek (self , offset : int , whence : int ) -> bytes :
1020
+ return self ._file .seeK (offset , whence )
1021
+
1022
+ with pytest .raises (
1023
+ TypeError , match = "takes 1 positional argument but 2 were given"
1024
+ ):
1025
+ create_from_file_like (
1026
+ ReadMethodWrongSignature (open (NASA_VIDEO .path , mode = "rb" , buffering = 0 )),
1027
+ "approximate" ,
1028
+ )
1029
+
1030
+ class SeekMethodWrongSignature :
1031
+ def __init__ (self , file : io .RawIOBase ):
1032
+ self ._file = file
1033
+
1034
+ def read (self , size : int ) -> bytes :
1035
+ return self ._file .read (size )
1036
+
1037
+ # io.RawIOBase says we should accept two ints; wrong signature on purpose
1038
+ def seek (self , offset : int ) -> bytes :
1039
+ return bytes ()
1040
+
1041
+ with pytest .raises (
1042
+ TypeError , match = "takes 2 positional arguments but 3 were given"
1043
+ ):
1044
+ create_from_file_like (
1045
+ SeekMethodWrongSignature (open (NASA_VIDEO .path , mode = "rb" , buffering = 0 )),
1046
+ "approximate" ,
1047
+ )
1048
+
1049
+ def test_file_like_read_fails (self ):
1050
+ class BadReader (io .RawIOBase ):
1051
+
1052
+ def __init__ (self , file : io .RawIOBase ):
1053
+ self ._file = file
1054
+
1055
+ def read (self , size : int ) -> bytes :
1056
+ # We intentionally read more than requested.
1057
+ return self ._file .read (size + 10 )
1058
+
1059
+ def seek (self , offset : int , whence : int ) -> bytes :
1060
+ return self ._file .seek (offset , whence )
1061
+
1062
+ with pytest .raises (RuntimeError , match = "does not conform to read protocol" ):
1063
+ create_from_file_like (
1064
+ BadReader (open (NASA_VIDEO .path , mode = "rb" , buffering = 0 )),
1065
+ "approximate" ,
1066
+ )
1067
+
996
1068
997
1069
if __name__ == "__main__" :
998
1070
pytest .main ()
0 commit comments