Skip to content

Commit 2dfa0c0

Browse files
committed
Remove CodecContext.close()
Use `del` instead. This fixes many null pointer dereferences.
1 parent 49b7c14 commit 2dfa0c0

File tree

7 files changed

+3
-28
lines changed

7 files changed

+3
-28
lines changed

av/audio/stream.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ class AudioStream(Stream):
3030
type: Literal["audio"]
3131
format: _Format
3232
layout: _Layout
33-
34-
def close(self, strict: bool = True) -> None: ...

av/codec/context.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ cdef class CodecContext:
3030

3131
# Public API.
3232
cpdef open(self, bint strict=?)
33-
cpdef close(self, bint strict=?)
3433

3534
cdef _set_default_time_base(self)
3635

av/codec/context.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class CodecContext:
9999
delay: bool
100100

101101
def open(self, strict: bool = True) -> None: ...
102-
def close(self, strict: bool = True) -> None: ...
103102
@staticmethod
104103
def create(
105104
codec: str | Codec, mode: Literal["r", "w"] | None = None

av/codec/context.pyx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,6 @@ cdef class CodecContext:
254254
self.ptr.time_base.num = 1
255255
self.ptr.time_base.den = lib.AV_TIME_BASE
256256

257-
cpdef close(self, bint strict=True):
258-
if not self._is_open:
259-
if strict:
260-
raise ValueError("CodecContext is already closed.")
261-
return
262-
self._is_open = False
263-
lib.avcodec_free_context(&self.ptr)
264257

265258
def __dealloc__(self):
266259
if self.ptr and self.extradata_set:

av/container/output.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ cdef class OutputContainer(Container):
246246

247247
def close(self):
248248
for stream in self.streams:
249-
if stream.codec_context:
250-
stream.codec_context.close(strict=False)
249+
del stream
251250

252251
close_output(self)
253252

av/video/stream.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,3 @@ class VideoStream(Stream):
4040
color_trc: int
4141
colorspace: int
4242
type: Literal["video"]
43-
44-
def close(self, strict: bool = True) -> None: ...

tests/test_streams.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ def test_selection(self) -> None:
3535
data = container.streams.data[0]
3636
assert data == container.streams.best("data")
3737

38-
def test_printing_closed_video_stream(self) -> None:
38+
def test_printing_video_stream(self) -> None:
3939
input_ = av.open(
4040
fate_suite("amv/MTV_high_res_320x240_sample_Penguin_Joke_MTV_from_WMV.amv")
4141
)
4242
container = av.open("out.mkv", "w")
4343

4444
video_stream = container.add_stream("h264", rate=30)
45-
# encoder = video_stream.codec.name + ""
45+
encoder = video_stream.codec.name
4646

4747
video_stream.width = input_.streams.video[0].width
4848
video_stream.height = input_.streams.video[0].height
@@ -52,21 +52,10 @@ def test_printing_closed_video_stream(self) -> None:
5252
container.mux(video_stream.encode(frame))
5353
break
5454

55-
encoder = "libx264"
5655
repr = f"{video_stream}"
5756
assert repr.startswith(f"<av.VideoStream #0 {encoder}, yuv420p 160x120 at ")
5857
assert repr.endswith(">")
5958

60-
# repr = f"{video_stream}"
61-
# assert repr.startswith(f"<av.VideoStream #0 {encoder}, yuv420p 160x120 at ")
62-
# assert repr.endswith(">")
63-
64-
video_stream.close()
65-
66-
repr = f"{video_stream}"
67-
assert repr.startswith(f"<av.VideoStream #0 {encoder}, yuv420p 0x0 at ")
68-
assert repr.endswith(">")
69-
7059
container.close()
7160
input_.close()
7261

0 commit comments

Comments
 (0)