[Python-checkins] cpython (3.2): Close #12383: Fix subprocess module with env={}: don't copy the environment

victor.stinner python-checkins at python.org
Tue Jun 21 17:24:17 CEST 2011


http://hg.python.org/cpython/rev/b5963fceddad
changeset:   70921:b5963fceddad
branch:      3.2
parent:      70919:275862274567
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Tue Jun 21 17:18:38 2011 +0200
summary:
  Close #12383: Fix subprocess module with env={}: don't copy the environment
variables, start with an empty environment.

files:
  Lib/subprocess.py           |   2 +-
  Lib/test/test_subprocess.py |  23 ++++++++++++++++-------
  Misc/NEWS                   |   3 +++
  3 files changed, 20 insertions(+), 8 deletions(-)


diff --git a/Lib/subprocess.py b/Lib/subprocess.py
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1169,7 +1169,7 @@
                         # potential deadlocks, thus we do all this here.
                         # and pass it to fork_exec()
 
-                        if env:
+                        if env is not None:
                             env_list = [os.fsencode(k) + b'=' + os.fsencode(v)
                                         for k, v in env.items()]
                         else:
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -331,13 +331,22 @@
     def test_env(self):
         newenv = os.environ.copy()
         newenv["FRUIT"] = "orange"
-        p = subprocess.Popen([sys.executable, "-c",
-                              'import sys,os;'
-                              'sys.stdout.write(os.getenv("FRUIT"))'],
-                             stdout=subprocess.PIPE,
-                             env=newenv)
-        self.addCleanup(p.stdout.close)
-        self.assertEqual(p.stdout.read(), b"orange")
+        with subprocess.Popen([sys.executable, "-c",
+                               'import sys,os;'
+                               'sys.stdout.write(os.getenv("FRUIT"))'],
+                              stdout=subprocess.PIPE,
+                              env=newenv) as p:
+            stdout, stderr = p.communicate()
+            self.assertEqual(stdout, b"orange")
+
+    def test_empty_env(self):
+        with subprocess.Popen([sys.executable, "-c",
+                               'import os; '
+                               'print(len(os.environ))'],
+                              stdout=subprocess.PIPE,
+                              env={}) as p:
+            stdout, stderr = p.communicate()
+            self.assertEqual(stdout.strip(), b"0")
 
     def test_communicate_stdin(self):
         p = subprocess.Popen([sys.executable, "-c",
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@
 Library
 -------
 
+- Issue #12383: Fix subprocess module with env={}: don't copy the environment
+  variables, start with an empty environment.
+
 - Issue #11584: email.header.decode_header no longer fails if the header
   passed to it is a Header object, and Header/make_header no longer fail
   if given binary unknown-8bit input.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list