From 11b2f03b284f1d84f1b763da060a05c77a7a0463 Mon Sep 17 00:00:00 2001 From: Andrii Yurchuk Date: Mon, 17 Jul 2023 10:20:11 +0200 Subject: [PATCH] Support arbitrary keyword arguments for subprocess.Popen (#68) --- ffmpy.py | 11 +++++++++-- tests/test_cmd_execution.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ffmpy.py b/ffmpy.py index 3c7d0af..03291fc 100644 --- a/ffmpy.py +++ b/ffmpy.py @@ -60,7 +60,7 @@ def __init__( def __repr__(self): return "<{0!r} {1!r}>".format(self.__class__.__name__, self.cmd) - def run(self, input_data=None, stdout=None, stderr=None, env=None): + def run(self, input_data=None, stdout=None, stderr=None, env=None, **kwargs): """Execute FFmpeg command line. ``input_data`` can contain input for FFmpeg in case ``pipe`` protocol is used for input. @@ -84,6 +84,8 @@ def run(self, input_data=None, stdout=None, stderr=None, env=None): :param stderr: redirect FFmpeg ``stderr`` there (default is `None` which means no redirection) :param env: custom environment for ffmpeg process + :param kwargs: any other keyword arguments to be forwarded to `subprocess.Popen + `_ :return: a 2-tuple containing ``stdout`` and ``stderr`` of the process :rtype: tuple :raise: `FFRuntimeError` in case FFmpeg command exits with a non-zero code; @@ -91,7 +93,12 @@ def run(self, input_data=None, stdout=None, stderr=None, env=None): """ try: self.process = subprocess.Popen( - self._cmd, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, env=env + self._cmd, + stdin=subprocess.PIPE, + stdout=stdout, + stderr=stderr, + env=env, + **kwargs ) except OSError as e: if e.errno == errno.ENOENT: diff --git a/tests/test_cmd_execution.py b/tests/test_cmd_execution.py index eee9597..97da5a2 100644 --- a/tests/test_cmd_execution.py +++ b/tests/test_cmd_execution.py @@ -187,3 +187,21 @@ def test_custom_env(popen_mock): popen_mock.assert_called_with( mock.ANY, stdin=mock.ANY, stdout=mock.ANY, stderr=mock.ANY, env="customenv" ) + + +@mock.patch("ffmpy.subprocess.Popen") +def test_arbitraty_popen_kwargs(popen_mock): + ff = FFmpeg() + popen_mock.return_value.communicate.return_value = ("output", "error") + popen_mock.return_value.returncode = 0 + ff.run(creationflags=42, encoding="foo", text="bar") + popen_mock.assert_called_with( + mock.ANY, + stdin=mock.ANY, + stdout=mock.ANY, + stderr=mock.ANY, + env=None, + creationflags=42, + encoding="foo", + text="bar", + )