Cleanup SameEnvironment and use the same logic for creation in virtualenvs

This commit is contained in:
Dave Halter
2019-02-22 00:24:55 +01:00
parent 48b137a7f5
commit a79d386eba
2 changed files with 9 additions and 3 deletions

View File

@@ -153,9 +153,9 @@ def _get_virtual_env_from_var():
variable is considered to be safe / controlled by the user solely.
"""
var = os.environ.get('VIRTUAL_ENV')
if var is not None:
if var:
if var == sys.prefix:
return SameEnvironment()
return _try_get_same_env()
try:
return create_environment(var, safe=False)
@@ -184,6 +184,10 @@ def get_default_environment():
if virtual_env is not None:
return virtual_env
return _try_get_same_env()
def _try_get_same_env():
env = SameEnvironment()
if not os.path.basename(env.executable).lower().startswith('python'):
# This tries to counter issues with embedding. In some cases (e.g.

View File

@@ -131,11 +131,13 @@ def test_get_default_environment_from_env_does_not_use_safe(tmpdir, monkeypatch)
assert env.path == 'fake'
def test_get_default_environment_when_embedded(monkeypatch):
@pytest.mark.parametrize('virtualenv', ['', 'fufuuuuu', sys.prefix])
def test_get_default_environment_when_embedded(monkeypatch, virtualenv):
# When using Python embedded, sometimes the executable is not a Python
# executable.
executable_name = 'RANDOM_EXE'
monkeypatch.setattr(sys, 'executable', executable_name)
monkeypatch.setenv('VIRTUAL_ENV', virtualenv)
env = get_default_environment()
assert env.executable != executable_name