From 2d3a078f24309f8d189ea196b54011632e3afb20 Mon Sep 17 00:00:00 2001 From: magnusvmt Date: Sat, 2 Nov 2019 16:17:17 +0100 Subject: [PATCH] Add test for probe timeout and fix for Python2 Fix for Python2 so that timeout is only used as keyword argument if it is provided Added a test for the new timeout argument that will run for Python > 3.3. --- ffmpeg/_probe.py | 5 ++++- ffmpeg/tests/test_ffmpeg.py | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ffmpeg/_probe.py b/ffmpeg/_probe.py index 3b8e3a6..090d7ab 100644 --- a/ffmpeg/_probe.py +++ b/ffmpeg/_probe.py @@ -18,7 +18,10 @@ def probe(filename, cmd='ffprobe', timeout=None, **kwargs): args += [filename] p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = p.communicate(timeout=timeout) + communicate_kwargs = {} + if timeout is not None: + communicate_kwargs['timeout'] = timeout + out, err = p.communicate(**communicate_kwargs) if p.returncode != 0: raise Error('ffprobe', out, err) return json.loads(out.decode('utf-8')) diff --git a/ffmpeg/tests/test_ffmpeg.py b/ffmpeg/tests/test_ffmpeg.py index 279a323..a5a1a2d 100644 --- a/ffmpeg/tests/test_ffmpeg.py +++ b/ffmpeg/tests/test_ffmpeg.py @@ -8,6 +8,7 @@ import pytest import random import re import subprocess +import sys try: import mock # python 2 @@ -706,6 +707,13 @@ def test__probe(): assert data['format']['duration'] == '7.036000' +@pytest.mark.skipif(sys.version_info < (3, 3), reason="requires python3.3 or higher") +def test__probe_timeout(): + with pytest.raises(subprocess.TimeoutExpired) as excinfo: + data = ffmpeg.probe(TEST_INPUT_FILE1, timeout=0) + assert 'timed out after 0 seconds' in str(excinfo.value) + + def test__probe__exception(): with pytest.raises(ffmpeg.Error) as excinfo: ffmpeg.probe(BOGUS_INPUT_FILE)