Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env: do not modify os.environ #3253

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import textwrap

from contextlib import contextmanager
from copy import deepcopy
from typing import Any
from typing import Dict
from typing import List
Expand Down Expand Up @@ -1077,15 +1078,13 @@ def _run(self, cmd, **kwargs):

def execute(self, bin, *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"])
else:
return os.execvp(bin, args)
return os.execvpe(bin, args, env=env)
else:
exe = subprocess.Popen([bin] + list(args), **kwargs)
exe = subprocess.Popen([bin] + list(args), env=env, **kwargs)
exe.communicate()
return exe.returncode

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

def _run(self, cmd, **kwargs):
with self.temp_environ():
os.environ["PATH"] = self._updated_path()
os.environ["VIRTUAL_ENV"] = str(self._path)

self.unset_env("PYTHONHOME")
self.unset_env("__PYVENV_LAUNCHER__")
kwargs["env"] = self.get_temp_environ(environ=kwargs.get("env"))
return super(VirtualEnv, self)._run(cmd, **kwargs)

def get_temp_environ(
self, environ=None, exclude=None, **kwargs
): # type: (Optional[Dict[str, str]], Optional[List[str]], **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, *args, **kwargs):
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

return super(VirtualEnv, self).execute(bin, *args, **kwargs)
def execute(self, bin, *args, **kwargs):
kwargs["env"] = self.get_temp_environ(environ=kwargs.get("env"))
return super(VirtualEnv, self).execute(bin, *args, **kwargs)

@contextmanager
def temp_environ(self):
Expand All @@ -1350,10 +1357,6 @@ def temp_environ(self):
os.environ.clear()
os.environ.update(environ)

def unset_env(self, key):
if key in os.environ:
del os.environ[key]

def _updated_path(self):
return os.pathsep.join([str(self._bin_dir), os.environ.get("PATH", "")])

Expand Down