Skip to content

Commit 044cf94

Browse files
ronaldoussorenjaraconsoranzo
authored
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>
1 parent 05e4a29 commit 044cf94

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
@@ -391,6 +391,18 @@ def test_deactivate_with_strict_bash_opts(self):
391391
self.assertEqual(err, "".encode())
392392

393393

394+
@unittest.skipUnless(sys.platform == 'darwin', 'only relevant on macOS')
395+
def test_macos_env(self):
396+
rmtree(self.env_dir)
397+
builder = venv.EnvBuilder()
398+
builder.create(self.env_dir)
399+
400+
envpy = os.path.join(os.path.realpath(self.env_dir),
401+
self.bindir, self.exe)
402+
out, err = check_output([envpy, '-c',
403+
'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
404+
self.assertEqual(out.strip(), 'False'.encode())
405+
394406
@requireVenvCreate
395407
class EnsurePipTest(BaseTest):
396408
"""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
@@ -1139,6 +1139,17 @@ config_init_program_name(PyConfig *config)
11391139
if (_PyStatus_EXCEPTION(status)) {
11401140
return status;
11411141
}
1142+
1143+
/*
1144+
* This environment variable is used to communicate between
1145+
* the stub launcher and the real interpreter and isn't needed
1146+
* beyond this point.
1147+
*
1148+
* Clean up to avoid problems when launching other programs
1149+
* later on.
1150+
*/
1151+
(void)unsetenv("__PYVENV_LAUNCHER__");
1152+
11421153
return _PyStatus_OK();
11431154
}
11441155
}

0 commit comments

Comments
 (0)