]> granicus.if.org Git - python/commitdiff
Close #12383: Fix subprocess module with env={}: don't copy the environment
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 21 Jun 2011 15:18:38 +0000 (17:18 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 21 Jun 2011 15:18:38 +0000 (17:18 +0200)
variables, start with an empty environment.

Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS

index 4bcf159864fa1d97a88c92024d066319d05b571b..06285e9462cb99aee13e1db1fd4cc7e81ca9c71c 100644 (file)
@@ -1169,7 +1169,7 @@ class Popen(object):
                         # 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:
index e6e8b3aaf2ffc37b9482d8fd96cf570c5e6589e6..3ee2e6edf20299a635de314e5b62967b18a3e0ae 100644 (file)
@@ -331,13 +331,22 @@ class ProcessTestCase(BaseTestCase):
     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",
index 0ec55b6a9aa4bbbeef5231a2c8f0eb6ec2097c39..60405dd746483f7fa20954e89d167a96a400f0bb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@ Core and Builtins
 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.