Skip to content

Commit 4fca86e

Browse files
committed
testdir.popen: use kwargs with defaults for stdout/stderr
1 parent 9ad0071 commit 4fca86e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

changelog/5059.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytester's ``Testdir.popen()`` uses ``stdout`` and ``stderr`` via keyword arguments with defaults now (``subprocess.PIPE``).

src/_pytest/pytester.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,14 @@ def collect_by_name(self, modcol, name):
10341034
if colitem.name == name:
10351035
return colitem
10361036

1037-
def popen(self, cmdargs, stdout, stderr, stdin=CLOSE_STDIN, **kw):
1037+
def popen(
1038+
self,
1039+
cmdargs,
1040+
stdout=subprocess.PIPE,
1041+
stderr=subprocess.PIPE,
1042+
stdin=CLOSE_STDIN,
1043+
**kw
1044+
):
10381045
"""Invoke subprocess.Popen.
10391046
10401047
This calls subprocess.Popen making sure the current working directory

testing/test_pytester.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,22 @@ def test_popen_stdin_bytes(testdir):
540540
assert stdout.decode("utf8").splitlines() == ["input", "2ndline"]
541541
assert stderr == b""
542542
assert proc.returncode == 0
543+
544+
545+
def test_popen_default_stdin_stderr_and_stdin_None(testdir):
546+
# stdout, stderr default to pipes,
547+
# stdin can be None to not close the pipe, avoiding
548+
# "ValueError: flush of closed file" with `communicate()`.
549+
p1 = testdir.makepyfile(
550+
"""
551+
import sys
552+
print(sys.stdin.read()) # empty
553+
print('stdout')
554+
sys.stderr.write('stderr')
555+
"""
556+
)
557+
proc = testdir.popen([sys.executable, str(p1)], stdin=None)
558+
stdout, stderr = proc.communicate(b"ignored")
559+
assert stdout.splitlines() == [b"", b"stdout"]
560+
assert stderr.splitlines() == [b"stderr"]
561+
assert proc.returncode == 0

0 commit comments

Comments
 (0)