[Python-checkins] bpo-43423 Fix IndexError in subprocess _communicate function (GH-24777)

miss-islington webhook-mailer at python.org
Thu Mar 11 20:53:00 EST 2021

commit: 1a5001c606b55226c03fa1046aa8f5e1db2fa67d
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-03-11T17:52:50-08:00

bpo-43423 Fix IndexError in subprocess _communicate function (GH-24777)

Check to make sure stdout and stderr are not empty before selecting an item from them in Windows subprocess._communicate.

Co-authored-by: Gregory P. Smith <greg at krypto.org>
(cherry picked from commit b4fc44bb2d209182390b4f9fdf074a46b0165a2f)

Co-authored-by: Chris Griffith <chris at cdgriffith.com>

A Misc/NEWS.d/next/Library/2021-03-11-15-44-18.bpo-43423.rRomRD.rst
M Lib/subprocess.py

diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index eecb1e7f253fb..d4d04a5c3436a 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1416,10 +1416,8 @@ def _communicate(self, input, endtime, orig_timeout):
             # All data exchanged.  Translate lists into strings.
-            if stdout is not None:
-                stdout = stdout[0]
-            if stderr is not None:
-                stderr = stderr[0]
+            stdout = stdout[0] if stdout else None
+            stderr = stderr[0] if stderr else None
             return (stdout, stderr)
diff --git a/Misc/NEWS.d/next/Library/2021-03-11-15-44-18.bpo-43423.rRomRD.rst b/Misc/NEWS.d/next/Library/2021-03-11-15-44-18.bpo-43423.rRomRD.rst
new file mode 100644
index 0000000000000..290d7fbd91800
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-03-11-15-44-18.bpo-43423.rRomRD.rst
@@ -0,0 +1,2 @@
+:func:`subprocess.communicate` no longer raises an IndexError when there is an
+empty stdout or stderr IO buffer during a timeout on Windows.

More information about the Python-checkins mailing list