@@ -45,18 +45,48 @@ class TestDecoder:
4545 (AudioDecoder , NASA_AUDIO_MP3 ),
4646 ),
4747 )
48- @pytest .mark .parametrize ("source_kind" , ("str" , "path" , "tensor" , "bytes" ))
48+ @pytest .mark .parametrize (
49+ "source_kind" ,
50+ (
51+ "str" ,
52+ "path" ,
53+ "file_like_rawio" ,
54+ "file_like_bufferedio" ,
55+ "file_like_custom" ,
56+ "bytes" ,
57+ "tensor" ,
58+ ),
59+ )
4960 def test_create (self , Decoder , asset , source_kind ):
5061 if source_kind == "str" :
5162 source = str (asset .path )
5263 elif source_kind == "path" :
5364 source = asset .path
54- elif source_kind == "tensor" :
55- source = asset .to_tensor ()
65+ elif source_kind == "file_like_rawio" :
66+ source = open (asset .path , mode = "rb" , buffering = 0 )
67+ elif source_kind == "file_like_bufferedio" :
68+ source = open (asset .path , mode = "rb" , buffering = 4096 )
69+ elif source_kind == "file_like_custom" :
70+ # This class purposefully does not inherit from io.RawIOBase or
71+ # io.BufferedReader. We are testing the case when users pass an
72+ # object that has the right methods but is an arbitrary type.
73+ class CustomReader :
74+ def __init__ (self , file ):
75+ self ._file = file
76+
77+ def read (self , size : int ) -> bytes :
78+ return self ._file .read (size )
79+
80+ def seek (self , offset : int , whence : int ) -> bytes :
81+ return self ._file .seek (offset , whence )
82+
83+ source = CustomReader (open (asset .path , mode = "rb" , buffering = 0 ))
5684 elif source_kind == "bytes" :
5785 path = str (asset .path )
5886 with open (path , "rb" ) as f :
5987 source = f .read ()
88+ elif source_kind == "tensor" :
89+ source = asset .to_tensor ()
6090 else :
6191 raise ValueError ("Oops, double check the parametrization of this test!" )
6292
@@ -76,6 +106,11 @@ def test_create_fails(self, Decoder):
76106 with pytest .raises (ValueError , match = "No valid stream found" ):
77107 Decoder (NASA_VIDEO .path , stream_index = 2 )
78108
109+ # user mistakenly forgets to specify binary reading when creating a file
110+ # like object from open()
111+ with pytest .raises (TypeError , match = "binary reading?" ):
112+ Decoder (open (NASA_VIDEO .path , "r" ))
113+
79114
80115class TestVideoDecoder :
81116 @pytest .mark .parametrize ("seek_mode" , ("exact" , "approximate" ))
0 commit comments