Skip to content

Commit c959fa9

Browse files
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@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 687f592 commit c959fa9

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
@@ -682,7 +682,6 @@ def is_env_var_to_ignore(n):
682682
# on adding even when the environment in exec is empty.
683683
# Gentoo sandboxes also force LD_PRELOAD and SANDBOX_* to exist.
684684
return ('VERSIONER' in n or '__CF' in n or # MacOS
685-
'__PYVENV_LAUNCHER__' in n or # MacOS framework build
686685
n == 'LD_PRELOAD' or n.startswith('SANDBOX') or # Gentoo
687686
n == 'LC_CTYPE') # Locale coercion triggered
688687

Lib/test/test_venv.py

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

360360

361+
@unittest.skipUnless(sys.platform == 'darwin', 'only relevant on macOS')
362+
def test_macos_env(self):
363+
rmtree(self.env_dir)
364+
builder = venv.EnvBuilder()
365+
builder.create(self.env_dir)
366+
367+
envpy = os.path.join(os.path.realpath(self.env_dir),
368+
self.bindir, self.exe)
369+
out, err = check_output([envpy, '-c',
370+
'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
371+
self.assertEqual(out.strip(), 'False'.encode())
372+
361373
@requireVenvCreate
362374
class EnsurePipTest(BaseTest):
363375
"""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.

Python/initconfig.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,17 @@ config_init_program_name(PyConfig *config)
11441144
if (_PyStatus_EXCEPTION(status)) {
11451145
return status;
11461146
}
1147+
1148+
/*
1149+
* This environment variable is used to communicate between
1150+
* the stub launcher and the real interpreter and isn't needed
1151+
* beyond this point.
1152+
*
1153+
* Clean up to avoid problems when launching other programs
1154+
* later on.
1155+
*/
1156+
(void)unsetenv("__PYVENV_LAUNCHER__");
1157+
11471158
return _PyStatus_OK();
11481159
}
11491160
}

0 commit comments

Comments
 (0)