Skip to content

Commit

Permalink
If the VIRTUAL_ENV variable changes, need to reload the default envir…
Browse files Browse the repository at this point in the history
…onment, fixes #1201, #1200
  • Loading branch information
davidhalter committed Sep 30, 2018
1 parent f9cbc65 commit 862f611
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion jedi/api/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,16 @@ def get_default_environment():
return SameEnvironment()


@time_cache(seconds=10 * 60) # 10 Minutes
def get_cached_default_environment():
environment = _get_cached_default_environment()
if environment.path != os.environ.get('VIRTUAL_ENV'):
_get_cached_default_environment.clear_cache()
return _get_cached_default_environment()
return environment


@time_cache(seconds=10 * 60) # 10 Minutes
def _get_cached_default_environment():
return get_default_environment()


Expand Down
1 change: 1 addition & 0 deletions jedi/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def wrapper(*args, **kwargs):

wrapper.clear_cache = lambda: cache.clear()
return wrapper

return decorator


Expand Down
9 changes: 8 additions & 1 deletion test/test_api/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from jedi._compatibility import py_version
from jedi.api.environment import get_default_environment, find_virtualenvs, \
InvalidPythonEnvironment, find_system_environments, \
get_system_environment, create_environment
get_system_environment, create_environment, get_cached_default_environment


def test_sys_path():
Expand Down Expand Up @@ -129,3 +129,10 @@ def _get_subprocess(self):
monkeypatch.setenv('VIRTUAL_ENV', fake_python)
env = get_default_environment()
assert env.path == 'fake'


def test_changing_venv(venv_path, monkeypatch):
monkeypatch.setitem(os.environ, 'VIRTUAL_ENV', venv_path)
get_cached_default_environment()
monkeypatch.setitem(os.environ, 'VIRTUAL_ENV', sys.executable)
assert get_cached_default_environment().executable == sys.executable

0 comments on commit 862f611

Please sign in to comment.