Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,13 @@ def test_devnull(self):

def do_test_with_pip(self, system_site_packages):
rmtree(self.env_dir)
os.mkdir(self.env_dir)
# Both ensurepip and its pip subprocess run with the venv as cwd.
for name in ('ensurepip.py', 'subprocess.py'):
pathlib.Path(self.env_dir, name).write_text(
'raise AssertionError(__file__)\n', encoding='utf-8')
with EnvironmentVarGuard() as envvars:
envvars["PYTHONSAFEPATH"] = ""
# pip's cross-version compatibility may trigger deprecation
# warnings in current versions of Python. Ensure related
# environment settings don't cause venv to fail.
Expand Down
7 changes: 4 additions & 3 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,15 @@ def setup_python(self, context):
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.
# legitimate user preferences (such as not writing bytecode).
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)
# Keep the working directory off sys.path, including in the
# subprocess that ensurepip starts to run pip.
env['PYTHONSAFEPATH'] = '1'
kwargs['cwd'] = context.env_dir
kwargs['executable'] = context.env_exec_cmd
subprocess.check_output(args, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prevent :mod:`venv` from importing modules from the root of an existing
virtual environment when installing or upgrading pip.
Loading