From e20747a7a00c97cd3a6bf6bb99c854278320cbda Mon Sep 17 00:00:00 2001 From: Curtis Doty Date: Wed, 17 Sep 2025 08:46:09 -0700 Subject: [PATCH 1/4] Prevent segfault on data stream template --- av/container/output.py | 3 +++ tests/test_streams.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/av/container/output.py b/av/container/output.py index d035b0265..39abbbded 100644 --- a/av/container/output.py +++ b/av/container/output.py @@ -144,6 +144,9 @@ def add_stream_from_template( if opaque is None: opaque = template.type != "video" + if template.codec_context is None: + raise ValueError(f"template stream of type {template.type} has no codec context") + codec_obj: Codec if opaque: # Copy ctx from template. codec_obj = template.codec_context.codec diff --git a/tests/test_streams.py b/tests/test_streams.py index c7b234d48..cc9fa6c8d 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -146,3 +146,18 @@ def test_data_stream(self) -> None: assert repr.startswith("") container.close() + + def test_data_stream_from_template(self) -> None: + """Test that adding a data stream from a template raises ValueError.""" + + # Open an existing container with a data stream + input_container = av.open(fate_suite("mxf/track_01_v02.mxf")) + input_data_stream = input_container.streams.data[0] + + # Create a new container and ensure using a data stream as a template raises ValueError + output_container = av.open("out.mkv", "w") + with pytest.raises(ValueError): + output_container.add_stream_from_template(input_data_stream) + + input_container.close() + output_container.close() From 9e31a496d24371b9a38025ea648b74785cc1548b Mon Sep 17 00:00:00 2001 From: Curtis Doty Date: Wed, 17 Sep 2025 08:56:25 -0700 Subject: [PATCH 2/4] appease ruff --- av/container/output.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/av/container/output.py b/av/container/output.py index 39abbbded..7738086ad 100644 --- a/av/container/output.py +++ b/av/container/output.py @@ -145,7 +145,9 @@ def add_stream_from_template( opaque = template.type != "video" if template.codec_context is None: - raise ValueError(f"template stream of type {template.type} has no codec context") + raise ValueError( + f"template stream of type {template.type} has no codec context" + ) codec_obj: Codec if opaque: # Copy ctx from template. From bd5e00e1e06688f372bf0f0f6b8293ee4c157925 Mon Sep 17 00:00:00 2001 From: Curtis Doty Date: Wed, 17 Sep 2025 09:04:44 -0700 Subject: [PATCH 3/4] Remove b0rk unit test --- tests/test_streams.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_streams.py b/tests/test_streams.py index cc9fa6c8d..4d941e4c7 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -147,6 +147,7 @@ def test_data_stream(self) -> None: container.close() +''' Thought this was helpful, but it now breaks add_stream_from_template() -> _StreamT def test_data_stream_from_template(self) -> None: """Test that adding a data stream from a template raises ValueError.""" @@ -161,3 +162,4 @@ def test_data_stream_from_template(self) -> None: input_container.close() output_container.close() +''' From 53af705c7181a9928e0e51da1dfac570d5ee89e8 Mon Sep 17 00:00:00 2001 From: Curtis Doty Date: Wed, 17 Sep 2025 09:21:43 -0700 Subject: [PATCH 4/4] good idea or AI spaghetticode? --- tests/test_streams.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_streams.py b/tests/test_streams.py index 4d941e4c7..d07085de4 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -1,5 +1,6 @@ import os from fractions import Fraction +from typing import Any, cast import pytest @@ -147,7 +148,6 @@ def test_data_stream(self) -> None: container.close() -''' Thought this was helpful, but it now breaks add_stream_from_template() -> _StreamT def test_data_stream_from_template(self) -> None: """Test that adding a data stream from a template raises ValueError.""" @@ -158,8 +158,12 @@ def test_data_stream_from_template(self) -> None: # Create a new container and ensure using a data stream as a template raises ValueError output_container = av.open("out.mkv", "w") with pytest.raises(ValueError): - output_container.add_stream_from_template(input_data_stream) + # input_data_stream is a DataStream at runtime; the test asserts that + # using it as a template raises ValueError. The static type stubs + # intentionally restrict which Stream subclasses are valid templates, + # so cast to Any here to keep the runtime check while satisfying + # the type checker. + output_container.add_stream_from_template(cast(Any, input_data_stream)) input_container.close() output_container.close() -'''