Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/15472.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`pytest.Pytester.popen` and :meth:`pytest.Pytester.run` now pass ``stdin=<bytes>`` 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``.
23 changes: 13 additions & 10 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import shutil
import subprocess
import sys
import tempfile
import traceback
from typing import Any
from typing import Final
Expand Down Expand Up @@ -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

Expand Down
42 changes: 42 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=<bytes>`` 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
Expand Down
Loading