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 ...@@ -11,6 +11,7 @@ import sysconfig
import textwrap import textwrap
from contextlib import contextmanager from contextlib import contextmanager
from copy import deepcopy
from pathlib import Path from pathlib import Path
from subprocess import CalledProcessError from subprocess import CalledProcessError
from typing import Any from typing import Any
...@@ -1143,15 +1144,13 @@ class Env(object): ...@@ -1143,15 +1144,13 @@ class Env(object):
return self.run_pip(*args, **kwargs) return self.run_pip(*args, **kwargs)
bin = self._bin(bin) bin = self._bin(bin)
env = kwargs.pop("env", {k: v for k, v in os.environ.items()})
if not self._is_windows: if not self._is_windows:
args = [bin] + list(args) args = [bin] + list(args)
if "env" in kwargs: return os.execvpe(bin, args, env=env)
return os.execvpe(bin, args, kwargs["env"])
else: else:
return os.execvp(bin, args) exe = subprocess.Popen([bin] + list(args), env=env, **kwargs)
else:
exe = subprocess.Popen([bin] + list(args), **kwargs)
exe.communicate() exe.communicate()
return exe.returncode return exe.returncode
...@@ -1388,23 +1387,34 @@ class VirtualEnv(Env): ...@@ -1388,23 +1387,34 @@ class VirtualEnv(Env):
return os.path.exists(self.python) return os.path.exists(self.python)
def _run(self, cmd: List[str], **kwargs: Any) -> Optional[int]: def _run(self, cmd: List[str], **kwargs: Any) -> Optional[int]:
with self.temp_environ(): kwargs["env"] = self.get_temp_environ(environ=kwargs.get("env"))
os.environ["PATH"] = self._updated_path() return super(VirtualEnv, self)._run(cmd, **kwargs)
os.environ["VIRTUAL_ENV"] = str(self._path)
self.unset_env("PYTHONHOME") def get_temp_environ(
self.unset_env("__PYVENV_LAUNCHER__") 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]: environ["PATH"] = self._updated_path()
with self.temp_environ(): environ["VIRTUAL_ENV"] = str(self._path)
os.environ["PATH"] = self._updated_path()
os.environ["VIRTUAL_ENV"] = str(self._path)
self.unset_env("PYTHONHOME") return environ
self.unset_env("__PYVENV_LAUNCHER__")
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) return super(VirtualEnv, self).execute(bin, *args, **kwargs)
@contextmanager @contextmanager
...@@ -1416,10 +1426,6 @@ class VirtualEnv(Env): ...@@ -1416,10 +1426,6 @@ class VirtualEnv(Env):
os.environ.clear() os.environ.clear()
os.environ.update(environ) 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: def _updated_path(self) -> str:
return os.pathsep.join([str(self._bin_dir), os.environ.get("PATH", "")]) 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