changeset: 70922:10ecf8576eb2 parent: 70920:c8192197d23d parent: 70921:b5963fceddad user: Victor Stinner date: Tue Jun 21 17:24:21 2011 +0200 files: Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS description: (merge 3.2) Close #12383: Fix subprocess module with env={}: don't copy the environment variables, start with an empty environment. diff -r c8192197d23d -r 10ecf8576eb2 Lib/subprocess.py --- a/Lib/subprocess.py Mon Jun 20 22:09:20 2011 -0500 +++ b/Lib/subprocess.py Tue Jun 21 17:24:21 2011 +0200 @@ -1250,7 +1250,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 -r c8192197d23d -r 10ecf8576eb2 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Mon Jun 20 22:09:20 2011 -0500 +++ b/Lib/test/test_subprocess.py Tue Jun 21 17:24:21 2011 +0200 @@ -382,13 +382,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 -r c8192197d23d -r 10ecf8576eb2 Misc/NEWS --- a/Misc/NEWS Mon Jun 20 22:09:20 2011 -0500 +++ b/Misc/NEWS Tue Jun 21 17:24:21 2011 +0200 @@ -196,6 +196,9 @@ Library ------- +- Issue #12383: Fix subprocess module with env={}: don't copy the environment + variables, start with an empty environment. + - Issue #11637: Fix support for importing packaging setup hooks from the project directory.