Commit 15d7a263 by Arun Babu Neelicattu

env: do not modify os.environ

Replace updates of os.environ with explcit passing of `env` to
subprocess calls.

Relates-to: #3199
parent 8c5611c6
......@@ -11,6 +11,7 @@ import sysconfig
import textwrap
from contextlib import contextmanager
from copy import deepcopy
from pathlib import Path
from subprocess import CalledProcessError
from typing import Any
......@@ -1143,15 +1144,13 @@ class Env(object):
return self.run_pip(*args, **kwargs)
bin = self._bin(bin)
env = kwargs.pop("env", {k: v for k, v in os.environ.items()})
if not self._is_windows:
args = [bin] + list(args)
if "env" in kwargs:
return os.execvpe(bin, args, kwargs["env"])
return os.execvpe(bin, args, env=env)
else:
return os.execvp(bin, args)
else:
exe = subprocess.Popen([bin] + list(args), **kwargs)
exe = subprocess.Popen([bin] + list(args), env=env, **kwargs)
exe.communicate()
return exe.returncode
......@@ -1388,23 +1387,34 @@ class VirtualEnv(Env):
return os.path.exists(self.python)
def _run(self, cmd: List[str], **kwargs: Any) -> Optional[int]:
with self.temp_environ():
os.environ["PATH"] = self._updated_path()
os.environ["VIRTUAL_ENV"] = str(self._path)
kwargs["env"] = self.get_temp_environ(environ=kwargs.get("env"))
return super(VirtualEnv, self)._run(cmd, **kwargs)
self.unset_env("PYTHONHOME")
self.unset_env("__PYVENV_LAUNCHER__")
def get_temp_environ(
self,
environ: Optional[Dict[str, str]] = None,
exclude: Optional[List[str]] = None,
**kwargs: str,
) -> Dict[str, str]:
exclude = exclude or []
exclude.extend(["PYTHONHOME", "__PYVENV_LAUNCHER__"])
if environ:
environ = deepcopy(environ)
for key in exclude:
environ.pop(key, None)
else:
environ = {k: v for k, v in os.environ.items() if k not in exclude}
return super(VirtualEnv, self)._run(cmd, **kwargs)
environ.update(kwargs)
def execute(self, bin: str, *args: str, **kwargs: Any) -> Optional[int]:
with self.temp_environ():
os.environ["PATH"] = self._updated_path()
os.environ["VIRTUAL_ENV"] = str(self._path)
environ["PATH"] = self._updated_path()
environ["VIRTUAL_ENV"] = str(self._path)
self.unset_env("PYTHONHOME")
self.unset_env("__PYVENV_LAUNCHER__")
return environ
def execute(self, bin: str, *args: str, **kwargs: Any) -> Optional[int]:
kwargs["env"] = self.get_temp_environ(environ=kwargs.get("env"))
return super(VirtualEnv, self).execute(bin, *args, **kwargs)
@contextmanager
......@@ -1416,10 +1426,6 @@ class VirtualEnv(Env):
os.environ.clear()
os.environ.update(environ)
def unset_env(self, key: str) -> None:
if key in os.environ:
del os.environ[key]
def _updated_path(self) -> str:
return os.pathsep.join([str(self._bin_dir), os.environ.get("PATH", "")])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment