[Python-checkins] bpo-22490: Remove __PYVENV_LAUNCHER__ from environment during launch (GH-9516) (GH-19110)

Miss Islington (bot) webhook-mailer at python.org
Sun Mar 22 14:56:31 EDT 2020


https://github.com/python/cpython/commit/c959fa9353b92ce95dd7fe3f25fe65bacbe22338
commit: c959fa9353b92ce95dd7fe3f25fe65bacbe22338
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-22T14:56:26-04:00
summary:

bpo-22490: Remove __PYVENV_LAUNCHER__ from environment during launch (GH-9516) (GH-19110)

* bpo-22490: Remove "__PYVENV_LAUNCHER__" from the shell environment on macOS

This changeset removes the environment varialbe "__PYVENV_LAUNCHER__"
during interpreter launch as it is only needed to communicate between
the stub executable in framework installs and the actual interpreter.

Leaving the environment variable present may lead to misbehaviour when
launching other scripts.

* Actually commit the changes for issue 22490...

* Correct typo

Co-Authored-By: Nicola Soranzo <nicola.soranzo at gmail.com>

* Run make patchcheck

Co-authored-by: Jason R. Coombs <jaraco at jaraco.com>
Co-authored-by: Nicola Soranzo <nicola.soranzo at gmail.com>
(cherry picked from commit 044cf94f610e831464a69a8e713dad89878824ce)

Co-authored-by: Ronald Oussoren <ronaldoussoren at mac.com>

Co-authored-by: Ronald Oussoren <ronaldoussoren at mac.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2018-09-23-16-32-58.bpo-22490.8e0YDf.rst
M Lib/test/test_subprocess.py
M Lib/test/test_venv.py
M Mac/Tools/pythonw.c
M Python/initconfig.c

diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 80acb06e4f920..f8fee3687136f 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -682,7 +682,6 @@ def is_env_var_to_ignore(n):
             # on adding even when the environment in exec is empty.
             # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
             return ('VERSIONER' in n or '__CF' in n or  # MacOS
-                    '__PYVENV_LAUNCHER__' in n or # MacOS framework build
                     n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
                     n == 'LC_CTYPE') # Locale coercion triggered
 
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index ede65935b650b..bc4e95f2b1ecb 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -358,6 +358,18 @@ def test_deactivate_with_strict_bash_opts(self):
         self.assertEqual(err, "".encode())
 
 
+    @unittest.skipUnless(sys.platform == 'darwin', 'only relevant on macOS')
+    def test_macos_env(self):
+        rmtree(self.env_dir)
+        builder = venv.EnvBuilder()
+        builder.create(self.env_dir)
+
+        envpy = os.path.join(os.path.realpath(self.env_dir),
+                             self.bindir, self.exe)
+        out, err = check_output([envpy, '-c',
+            'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
+        self.assertEqual(out.strip(), 'False'.encode())
+
 @requireVenvCreate
 class EnsurePipTest(BaseTest):
     """Test venv module installation of pip."""
diff --git a/Mac/Tools/pythonw.c b/Mac/Tools/pythonw.c
index 1d2db383f943c..c8bd3ba8d68c1 100644
--- a/Mac/Tools/pythonw.c
+++ b/Mac/Tools/pythonw.c
@@ -196,6 +196,15 @@ main(int argc, char **argv) {
             }
         }
 
+        /*
+         * The environment variable is used to pass the value of real_path
+         * to the actual python interpreter, and is read by code in
+         * Python/coreconfig.c.
+         *
+         * This way the real interpreter knows how the user invoked the
+         * interpreter and can behave as if this launcher is the real
+         * interpreter (looking for pyvenv configuration, ...)
+         */
         setenv("__PYVENV_LAUNCHER__", real_path, 1);
     }
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-09-23-16-32-58.bpo-22490.8e0YDf.rst b/Misc/NEWS.d/next/Core and Builtins/2018-09-23-16-32-58.bpo-22490.8e0YDf.rst
new file mode 100644
index 0000000000000..a281f024249f5
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-09-23-16-32-58.bpo-22490.8e0YDf.rst	
@@ -0,0 +1,2 @@
+Don't leak environment variable ``__PYVENV_LAUNCHER__`` into the interpreter
+session on macOS.
diff --git a/Python/initconfig.c b/Python/initconfig.c
index a0b2691bcd75c..a930b5d38fa26 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -1144,6 +1144,17 @@ config_init_program_name(PyConfig *config)
             if (_PyStatus_EXCEPTION(status)) {
                 return status;
             }
+
+            /*
+             * This environment variable is used to communicate between
+             * the stub launcher and the real interpreter and isn't needed
+             * beyond this point.
+             *
+             * Clean up to avoid problems when launching other programs
+             * later on.
+             */
+            (void)unsetenv("__PYVENV_LAUNCHER__");
+
             return _PyStatus_OK();
         }
     }



More information about the Python-checkins mailing list