Skip to content

Commit 88888fc

Browse files
abnkasteph
authored andcommitted
env: do not modify os.environ
Replace updates of os.environ with explcit passing of `env` to subprocess calls. Relates-to: #3199
1 parent 175af5f commit 88888fc

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

poetry/utils/env.py

+26-23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import textwrap
1111

1212
from contextlib import contextmanager
13+
from copy import deepcopy
1314
from typing import Any
1415
from typing import Dict
1516
from typing import List
@@ -1077,15 +1078,13 @@ def _run(self, cmd, **kwargs):
10771078

10781079
def execute(self, bin, *args, **kwargs):
10791080
bin = self._bin(bin)
1081+
env = kwargs.pop("env", {k: v for k, v in os.environ.items()})
10801082

10811083
if not self._is_windows:
10821084
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)
10871086
else:
1088-
exe = subprocess.Popen([bin] + list(args), **kwargs)
1087+
exe = subprocess.Popen([bin] + list(args), env=env, **kwargs)
10891088
exe.communicate()
10901089
return exe.returncode
10911090

@@ -1322,24 +1321,32 @@ def is_sane(self):
13221321
return os.path.exists(self.python) and os.path.exists(self._bin("pip"))
13231322

13241323
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}
13311339

1332-
return super(VirtualEnv, self)._run(cmd, **kwargs)
1340+
environ.update(kwargs)
13331341

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)
13381344

1339-
self.unset_env("PYTHONHOME")
1340-
self.unset_env("__PYVENV_LAUNCHER__")
1345+
return environ
13411346

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)
13431350

13441351
@contextmanager
13451352
def temp_environ(self):
@@ -1350,10 +1357,6 @@ def temp_environ(self):
13501357
os.environ.clear()
13511358
os.environ.update(environ)
13521359

1353-
def unset_env(self, key):
1354-
if key in os.environ:
1355-
del os.environ[key]
1356-
13571360
def _updated_path(self):
13581361
return os.pathsep.join([str(self._bin_dir), os.environ.get("PATH", "")])
13591362

0 commit comments

Comments
 (0)