]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-98966: Handle stdout=subprocess.STDOUT (GH-98967)
authorPaulo Neves <ptsneves@users.noreply.github.com>
Tue, 26 Mar 2024 12:37:50 +0000 (13:37 +0100)
committerGitHub <noreply@github.com>
Tue, 26 Mar 2024 12:37:50 +0000 (13:37 +0100)
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 <ptsneves@gmail.com>
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS.d/next/Library/2024-03-26-11-48-39.gh-issue-98966.SayV9y.rst [new file with mode: 0644]

index dbe15277866c995c66f36bc0b847eb2a0fb28889..d7c7b45127104f0dc249a7bfe1e1ab0656d67947 100644 (file)
@@ -839,6 +839,9 @@ class Popen:
         if not isinstance(bufsize, int):
             raise TypeError("bufsize must be an integer")
 
+        if stdout is STDOUT:
+             raise ValueError("STDOUT can only be used for stderr")
+
         if pipesize is None:
             pipesize = -1  # Restore default
         if not isinstance(pipesize, int):
index 70452ca94a6a8a046ff03db9cd487e2c22de8ef1..9ecd8426cb55378bffd4f4e37ae1bd66b1b822fc 100644 (file)
@@ -1763,6 +1763,13 @@ class RunFuncTestCase(BaseTestCase):
         self.assertIn(b'BDFL', cp.stdout)
         self.assertIn(b'FLUFL', cp.stderr)
 
+    def test_stdout_stdout(self):
+        # run() refuses to accept stdout=STDOUT
+        with self.assertRaises(ValueError,
+                msg=("STDOUT can only be used for stderr")):
+            self.run_python("print('will not be run')",
+                            stdout=subprocess.STDOUT)
+
     def test_stdout_with_capture_output_arg(self):
         # run() refuses to accept 'stdout' with 'capture_output'
         tf = tempfile.TemporaryFile()
diff --git a/Misc/NEWS.d/next/Library/2024-03-26-11-48-39.gh-issue-98966.SayV9y.rst b/Misc/NEWS.d/next/Library/2024-03-26-11-48-39.gh-issue-98966.SayV9y.rst
new file mode 100644 (file)
index 0000000..e819a1e
--- /dev/null
@@ -0,0 +1,2 @@
+In :mod:`subprocess`, raise a more informative message when
+``stdout=STDOUT``.