Skip to content

Commit 5765aca

Browse files
[3.7] bpo-22490: Remove __PYVENV_LAUNCHER__ from environment during launch (GH-9516) (GH-19111)
* 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>. (cherry picked from commit 044cf94) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com> Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
1 parent 39680fb commit 5765aca

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

Lib/test/test_subprocess.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ def is_env_var_to_ignore(n):
651651
# on adding even when the environment in exec is empty.
652652
# Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
653653
return ('VERSIONER' in n or '__CF' in n or # MacOS
654-
'__PYVENV_LAUNCHER__' in n or # MacOS framework build
655654
n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
656655
n == 'LC_CTYPE') # Locale coercion triggered
657656

Lib/test/test_venv.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ def test_deactivate_with_strict_bash_opts(self):
345345
self.assertEqual(err, "".encode())
346346

347347

348+
@unittest.skipUnless(sys.platform == 'darwin', 'only relevant on macOS')
349+
def test_macos_env(self):
350+
rmtree(self.env_dir)
351+
builder = venv.EnvBuilder()
352+
builder.create(self.env_dir)
353+
354+
envpy = os.path.join(os.path.realpath(self.env_dir),
355+
self.bindir, self.exe)
356+
out, err = check_output([envpy, '-c',
357+
'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
358+
self.assertEqual(out.strip(), 'False'.encode())
359+
348360
@requireVenvCreate
349361
class EnsurePipTest(BaseTest):
350362
"""Test venv module installation of pip."""

Mac/Tools/pythonw.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ main(int argc, char **argv) {
196196
}
197197
}
198198

199+
/*
200+
* The environment variable is used to pass the value of real_path
201+
* to the actual python interpreter, and is read by code in
202+
* Python/coreconfig.c.
203+
*
204+
* This way the real interpreter knows how the user invoked the
205+
* interpreter and can behave as if this launcher is the real
206+
* interpreter (looking for pyvenv configuration, ...)
207+
*/
199208
setenv("__PYVENV_LAUNCHER__", real_path, 1);
200209
}
201210

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Don't leak environment variable ``__PYVENV_LAUNCHER__`` into the interpreter
2+
session on macOS.

Modules/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,17 @@ config_init_program_name(_PyCoreConfig *config)
12081208
"variable", (Py_ssize_t)len);
12091209
}
12101210
config->program_name = program_name;
1211+
1212+
/*
1213+
* This environment variable is used to communicate between
1214+
* the stub launcher and the real interpreter and isn't needed
1215+
* beyond this point.
1216+
*
1217+
* Clean up to avoid problems when launching other programs
1218+
* later on.
1219+
*/
1220+
(void)unsetenv("__PYVENV_LAUNCHER__");
1221+
12111222
return _Py_INIT_OK();
12121223
}
12131224
}

0 commit comments

Comments
 (0)