10
10
import textwrap
11
11
12
12
from contextlib import contextmanager
13
+ from copy import deepcopy
13
14
from typing import Any
14
15
from typing import Dict
15
16
from typing import List
@@ -1077,15 +1078,13 @@ def _run(self, cmd, **kwargs):
1077
1078
1078
1079
def execute (self , bin , * args , ** kwargs ):
1079
1080
bin = self ._bin (bin )
1081
+ env = kwargs .pop ("env" , {k : v for k , v in os .environ .items ()})
1080
1082
1081
1083
if not self ._is_windows :
1082
1084
args = [bin ] + list (args )
1083
- if "env" in kwargs :
1084
- return os .execvpe (bin , args , kwargs ["env" ])
1085
- else :
1086
- return os .execvp (bin , args )
1085
+ return os .execvpe (bin , args , env = env )
1087
1086
else :
1088
- exe = subprocess .Popen ([bin ] + list (args ), ** kwargs )
1087
+ exe = subprocess .Popen ([bin ] + list (args ), env = env , ** kwargs )
1089
1088
exe .communicate ()
1090
1089
return exe .returncode
1091
1090
@@ -1322,24 +1321,32 @@ def is_sane(self):
1322
1321
return os .path .exists (self .python ) and os .path .exists (self ._bin ("pip" ))
1323
1322
1324
1323
def _run (self , cmd , ** kwargs ):
1325
- with self .temp_environ ():
1326
- os .environ ["PATH" ] = self ._updated_path ()
1327
- os .environ ["VIRTUAL_ENV" ] = str (self ._path )
1328
-
1329
- self .unset_env ("PYTHONHOME" )
1330
- self .unset_env ("__PYVENV_LAUNCHER__" )
1324
+ kwargs ["env" ] = self .get_temp_environ (environ = kwargs .get ("env" ))
1325
+ return super (VirtualEnv , self )._run (cmd , ** kwargs )
1326
+
1327
+ def get_temp_environ (
1328
+ self , environ = None , exclude = None , ** kwargs
1329
+ ): # type: (Optional[Dict[str, str]], Optional[List[str]], **str) -> Dict[str, str]
1330
+ exclude = exclude or []
1331
+ exclude .extend (["PYTHONHOME" , "__PYVENV_LAUNCHER__" ])
1332
+
1333
+ if environ :
1334
+ environ = deepcopy (environ )
1335
+ for key in exclude :
1336
+ environ .pop (key , None )
1337
+ else :
1338
+ environ = {k : v for k , v in os .environ .items () if k not in exclude }
1331
1339
1332
- return super ( VirtualEnv , self ). _run ( cmd , ** kwargs )
1340
+ environ . update ( kwargs )
1333
1341
1334
- def execute (self , bin , * args , ** kwargs ):
1335
- with self .temp_environ ():
1336
- os .environ ["PATH" ] = self ._updated_path ()
1337
- os .environ ["VIRTUAL_ENV" ] = str (self ._path )
1342
+ environ ["PATH" ] = self ._updated_path ()
1343
+ environ ["VIRTUAL_ENV" ] = str (self ._path )
1338
1344
1339
- self .unset_env ("PYTHONHOME" )
1340
- self .unset_env ("__PYVENV_LAUNCHER__" )
1345
+ return environ
1341
1346
1342
- return super (VirtualEnv , self ).execute (bin , * args , ** kwargs )
1347
+ def execute (self , bin , * args , ** kwargs ):
1348
+ kwargs ["env" ] = self .get_temp_environ (environ = kwargs .get ("env" ))
1349
+ return super (VirtualEnv , self ).execute (bin , * args , ** kwargs )
1343
1350
1344
1351
@contextmanager
1345
1352
def temp_environ (self ):
@@ -1350,10 +1357,6 @@ def temp_environ(self):
1350
1357
os .environ .clear ()
1351
1358
os .environ .update (environ )
1352
1359
1353
- def unset_env (self , key ):
1354
- if key in os .environ :
1355
- del os .environ [key ]
1356
-
1357
1360
def _updated_path (self ):
1358
1361
return os .pathsep .join ([str (self ._bin_dir ), os .environ .get ("PATH" , "" )])
1359
1362
0 commit comments