From 9b77c395a4f189b89c5c96aaa05c3c710f481402 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 14 Oct 2022 11:46:55 +0100 Subject: [PATCH 1/2] gh-98251: Allow venv to pass along PYTHON* variables to pip when they do not impact path resolution --- Lib/test/test_venv.py | 8 +++--- Lib/venv/__init__.py | 28 +++++++++++++------ ...2-10-14-11-46-31.gh-issue-98251.Uxc9al.rst | 2 ++ 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 4359a4e3ebe861..b355b0050cd55b 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -216,7 +216,7 @@ def test_upgrade_dependencies(self): if sys.platform == 'win32': expect_exe = os.path.normcase(os.path.realpath(expect_exe)) - def pip_cmd_checker(cmd): + def pip_cmd_checker(cmd, **kwargs): cmd[0] = os.path.normcase(cmd[0]) self.assertEqual( cmd, @@ -232,7 +232,7 @@ def pip_cmd_checker(cmd): ) fake_context = builder.ensure_directories(fake_env_dir) - with patch('venv.subprocess.check_call', pip_cmd_checker): + with patch('venv.subprocess.check_output', pip_cmd_checker): builder.upgrade_dependencies(fake_context) @requireVenvCreate @@ -659,8 +659,8 @@ def nicer_error(self): try: yield except subprocess.CalledProcessError as exc: - out = exc.output.decode(errors="replace") - err = exc.stderr.decode(errors="replace") + out = (exc.output or b'').decode(errors="replace") + err = (exc.stderr or b'').decode(errors="replace") self.fail( f"{exc}\n\n" f"**Subprocess Output**\n{out}\n\n" diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 034e3d42017721..5e695c75ff496f 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -339,14 +339,25 @@ def setup_python(self, context): shutil.copyfile(src, dst) break + def _call_new_python(self, context, *py_args, **kwargs): + """Executes the newly created Python using safe-ish options""" + # gh-98251: We do not want to just use '-I' because that masks + # legitimate user preferences (such as not writing bytecode). All we + # really need is to ensure that the path variables do not overrule + # normal venv handling. + args = [context.env_exec_cmd, *py_args] + kwargs['env'] = env = os.environ.copy() + env['VIRTUAL_ENV'] = context.env_dir + env.pop('PYTHONHOME', None) + env.pop('PYTHONPATH', None) + kwargs['cwd'] = context.env_dir + kwargs['executable'] = context.env_exec_cmd + subprocess.check_output(args, **kwargs) + def _setup_pip(self, context): """Installs or upgrades pip in a virtual environment""" - # We run ensurepip in isolated mode to avoid side effects from - # environment vars, the current directory and anything else - # intended for the global Python environment - cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade', - '--default-pip'] - subprocess.check_output(cmd, stderr=subprocess.STDOUT) + self._call_new_python(context, '-m', 'ensurepip', '--upgrade', + '--default-pip', stderr=subprocess.STDOUT) def setup_scripts(self, context): """ @@ -445,9 +456,8 @@ def upgrade_dependencies(self, context): logger.debug( f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}' ) - cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade'] - cmd.extend(CORE_VENV_DEPS) - subprocess.check_call(cmd) + self._call_new_python(context, '-m', 'pip', 'install', '--upgrade', + *CORE_VENV_DEPS) def create(env_dir, system_site_packages=False, clear=False, diff --git a/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst b/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst new file mode 100644 index 00000000000000..89787c5d6b1fa3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst @@ -0,0 +1,2 @@ +Allow :mod:`venv` to pass along :envvar:`PYTHON*` variables to ``pip`` when +they do not impact path resolution From b5eece85eb572e8530d2ceaf0051be88b2966075 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 14 Oct 2022 16:19:11 +0100 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst Co-authored-by: Vinay Sajip --- .../next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst b/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst index 89787c5d6b1fa3..1a2b6a2537b996 100644 --- a/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst +++ b/Misc/NEWS.d/next/Library/2022-10-14-11-46-31.gh-issue-98251.Uxc9al.rst @@ -1,2 +1,2 @@ -Allow :mod:`venv` to pass along :envvar:`PYTHON*` variables to ``pip`` when +Allow :mod:`venv` to pass along :envvar:`PYTHON*` variables to ``ensurepip`` and ``pip`` when they do not impact path resolution