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

* 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@gmail.com>

* Run make patchcheck

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>
This commit is contained in:
Ronald Oussoren 2020-03-22 19:31:46 +01:00 committed by GitHub
parent 05e4a296ec
commit 044cf94f61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 1 deletions

View File

@ -682,7 +682,6 @@ class ProcessTestCase(BaseTestCase):
# on adding even when the environment in exec is empty. # on adding even when the environment in exec is empty.
# Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist. # Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
return ('VERSIONER' in n or '__CF' in n or # MacOS 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 == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
n == 'LC_CTYPE') # Locale coercion triggered n == 'LC_CTYPE') # Locale coercion triggered

View File

@ -391,6 +391,18 @@ class BasicTest(BaseTest):
self.assertEqual(err, "".encode()) 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 @requireVenvCreate
class EnsurePipTest(BaseTest): class EnsurePipTest(BaseTest):
"""Test venv module installation of pip.""" """Test venv module installation of pip."""

View File

@ -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); setenv("__PYVENV_LAUNCHER__", real_path, 1);
} }

View File

@ -0,0 +1,2 @@
Don't leak environment variable ``__PYVENV_LAUNCHER__`` into the interpreter
session on macOS.

View File

@ -1139,6 +1139,17 @@ config_init_program_name(PyConfig *config)
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return 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(); return _PyStatus_OK();
} }
} }