@@ -93,6 +93,7 @@ def seek(self, offset: int, whence: int) -> bytes:
9393 decoder = Decoder (source )
9494 assert isinstance (decoder .metadata , _core ._metadata .StreamMetadata )
9595
96+
9697 @pytest .mark .parametrize ("Decoder" , (VideoDecoder , AudioDecoder ))
9798 def test_create_fails (self , Decoder ):
9899 with pytest .raises (TypeError , match = "Unknown source type" ):
@@ -126,6 +127,22 @@ def test_metadata(self, seek_mode):
126127 assert decoder .metadata .height == 270
127128 assert decoder .metadata .width == 480
128129
130+ def test_create_bytes_ownership (self ):
131+ # Note that the bytes object we use to instantiate the decoder does not
132+ # live past the VideoDecoder destructor. That is what we're testing:
133+ # that the VideoDecoder takes ownership of the bytes. If it does not,
134+ # then we will hit errors when we try to actually decode from the bytes
135+ # later on. By the time we actually decode, the reference on the Python
136+ # side has gone away, and if we don't have ownership on the C++ side, we
137+ # will hit runtime errors or segfaults.
138+ with open (NASA_VIDEO .path , "rb" ) as f :
139+ decoder = VideoDecoder (f .read ())
140+
141+ assert decoder [0 ] is not None
142+ assert decoder [len (decoder )// 2 ] is not None
143+ assert decoder [- 1 ] is not None
144+
145+
129146 def test_create_fails (self ):
130147 with pytest .raises (ValueError , match = "Invalid seek mode" ):
131148 VideoDecoder (NASA_VIDEO .path , seek_mode = "blah" )
0 commit comments