Skip to content

Commit 4abca7e

Browse files
authoredMar 26, 2024
pythongh-98966: Handle stdout=subprocess.STDOUT (pythonGH-98967)
Explicitly handle the case where stdout=STDOUT as otherwise the existing error handling gets confused and reports hard to understand errors. Signed-off-by: Paulo Neves <[email protected]>
1 parent 9654daf commit 4abca7e

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed
 

‎Lib/subprocess.py

+3
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,9 @@ def __init__(self, args, bufsize=-1, executable=None,
839839
if not isinstance(bufsize, int):
840840
raise TypeError("bufsize must be an integer")
841841

842+
if stdout is STDOUT:
843+
raise ValueError("STDOUT can only be used for stderr")
844+
842845
if pipesize is None:
843846
pipesize = -1 # Restore default
844847
if not isinstance(pipesize, int):

‎Lib/test/test_subprocess.py

+7
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,13 @@ def test_capture_output(self):
17631763
self.assertIn(b'BDFL', cp.stdout)
17641764
self.assertIn(b'FLUFL', cp.stderr)
17651765

1766+
def test_stdout_stdout(self):
1767+
# run() refuses to accept stdout=STDOUT
1768+
with self.assertRaises(ValueError,
1769+
msg=("STDOUT can only be used for stderr")):
1770+
self.run_python("print('will not be run')",
1771+
stdout=subprocess.STDOUT)
1772+
17661773
def test_stdout_with_capture_output_arg(self):
17671774
# run() refuses to accept 'stdout' with 'capture_output'
17681775
tf = tempfile.TemporaryFile()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :mod:`subprocess`, raise a more informative message when
2+
``stdout=STDOUT``.

0 commit comments

Comments
 (0)