@@ -339,14 +339,25 @@ def setup_python(self, context):
339
339
shutil .copyfile (src , dst )
340
340
break
341
341
342
+ def _call_new_python (self , context , * py_args , ** kwargs ):
343
+ """Executes the newly created Python using safe-ish options"""
344
+ # gh-98251: We do not want to just use '-I' because that masks
345
+ # legitimate user preferences (such as not writing bytecode). All we
346
+ # really need is to ensure that the path variables do not overrule
347
+ # normal venv handling.
348
+ args = [context .env_exec_cmd , * py_args ]
349
+ kwargs ['env' ] = env = os .environ .copy ()
350
+ env ['VIRTUAL_ENV' ] = context .env_dir
351
+ env .pop ('PYTHONHOME' , None )
352
+ env .pop ('PYTHONPATH' , None )
353
+ kwargs ['cwd' ] = context .env_dir
354
+ kwargs ['executable' ] = context .env_exec_cmd
355
+ subprocess .check_output (args , ** kwargs )
356
+
342
357
def _setup_pip (self , context ):
343
358
"""Installs or upgrades pip in a virtual environment"""
344
- # We run ensurepip in isolated mode to avoid side effects from
345
- # environment vars, the current directory and anything else
346
- # intended for the global Python environment
347
- cmd = [context .env_exec_cmd , '-Im' , 'ensurepip' , '--upgrade' ,
348
- '--default-pip' ]
349
- subprocess .check_output (cmd , stderr = subprocess .STDOUT )
359
+ self ._call_new_python (context , '-m' , 'ensurepip' , '--upgrade' ,
360
+ '--default-pip' , stderr = subprocess .STDOUT )
350
361
351
362
def setup_scripts (self , context ):
352
363
"""
@@ -445,9 +456,8 @@ def upgrade_dependencies(self, context):
445
456
logger .debug (
446
457
f'Upgrading { CORE_VENV_DEPS } packages in { context .bin_path } '
447
458
)
448
- cmd = [context .env_exec_cmd , '-m' , 'pip' , 'install' , '--upgrade' ]
449
- cmd .extend (CORE_VENV_DEPS )
450
- subprocess .check_call (cmd )
459
+ self ._call_new_python (context , '-m' , 'pip' , 'install' , '--upgrade' ,
460
+ * CORE_VENV_DEPS )
451
461
452
462
453
463
def create (env_dir , system_site_packages = False , clear = False ,
0 commit comments