From f095e5c280b7fc1347cf77ae326e0d38a99b32a0 Mon Sep 17 00:00:00 2001 From: Roxen-AS Date: Fri, 4 Dec 2026 12:44:47 +0530 Subject: [PATCH] pytester: pass bytes stdin via TemporaryFile in popen Signed-off-by: Roxen-AS --- changelog/15472.bugfix.rst | 1 + src/_pytest/pytester.py | 23 ++++++++++++--------- testing/test_pytester.py | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 changelog/15472.bugfix.rst diff --git a/changelog/15472.bugfix.rst b/changelog/15472.bugfix.rst new file mode 100644 index 00000000000..a447d3a274d --- /dev/null +++ b/changelog/15472.bugfix.rst @@ -0,0 +1 @@ +:meth:`pytest.Pytester.popen` and :meth:`pytest.Pytester.run` now pass ``stdin=`` via an anonymous temporary file instead of writing synchronously to a pipe, preventing ``BrokenPipeError`` and pipe-buffer deadlocks when the child exits early or does not read ``stdin``. diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 39cd7928efd..39b3efd4ed8 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -25,6 +25,7 @@ import shutil import subprocess import sys +import tempfile import traceback from typing import Any from typing import Final @@ -1382,20 +1383,22 @@ def popen( ) kw["env"] = env - if stdin is self.CLOSE_STDIN: - kw["stdin"] = subprocess.PIPE - elif isinstance(stdin, bytes): - kw["stdin"] = subprocess.PIPE - else: - kw["stdin"] = stdin + with contextlib.ExitStack() as stack: + if stdin is self.CLOSE_STDIN: + kw["stdin"] = subprocess.PIPE + elif isinstance(stdin, bytes): + stdin_file = stack.enter_context(tempfile.TemporaryFile()) + stdin_file.write(stdin) + stdin_file.seek(0) + kw["stdin"] = stdin_file + else: + kw["stdin"] = stdin + + popen = subprocess.Popen(cmdargs, stdout=stdout, stderr=stderr, **kw) - popen = subprocess.Popen(cmdargs, stdout=stdout, stderr=stderr, **kw) if stdin is self.CLOSE_STDIN: assert popen.stdin is not None popen.stdin.close() - elif isinstance(stdin, bytes): - assert popen.stdin is not None - popen.stdin.write(stdin) return popen diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 66954ec75a6..54d2a508cf9 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -674,6 +674,48 @@ def test_popen_stdin_bytes(pytester: Pytester) -> None: assert proc.returncode == 0 +def test_run_and_popen_large_stdin_unread(pytester: Pytester) -> None: + """Large or unread ``stdin=`` must neither raise BrokenPipeError nor block.""" + payload = b"x" * 2**20 + + result = pytester.run( + sys.executable, + "-c", + "raise SystemExit(7)", + stdin=payload, + ) + assert result.ret == 7 + + with pytest.raises(pytester.TimeoutExpired): + pytester.run( + sys.executable, + "-c", + "import time; time.sleep(1)", + stdin=payload, + timeout=0.1, + ) + + proc = pytester.popen( + [sys.executable, "-c", "raise SystemExit(7)"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=payload, + ) + proc.communicate() + assert proc.returncode == 7 + + proc_read = pytester.popen( + [sys.executable, "-c", "import sys; print(len(sys.stdin.read()))"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=payload, + ) + stdout, stderr = proc_read.communicate() + assert stdout.decode("utf8").strip() == str(len(payload)) + assert stderr == b"" + assert proc_read.returncode == 0 + + def test_popen_default_stdin_stderr_and_stdin_None(pytester: Pytester) -> None: # stdout, stderr default to pipes, # stdin can be None to not close the pipe, avoiding