From 862f61182941a451c8e158091a328d63cb4ef43b Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 30 Sep 2018 19:06:40 +0200 Subject: [PATCH] If the VIRTUAL_ENV variable changes, need to reload the default environment, fixes #1201, #1200 --- jedi/api/environment.py | 10 +++++++++- jedi/cache.py | 1 + test/test_api/test_environment.py | 9 ++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/jedi/api/environment.py b/jedi/api/environment.py index 7aa16dc80..2bf6f68a4 100644 --- a/jedi/api/environment.py +++ b/jedi/api/environment.py @@ -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() diff --git a/jedi/cache.py b/jedi/cache.py index 6c0c2a830..93e2bd7fc 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -126,6 +126,7 @@ def wrapper(*args, **kwargs): wrapper.clear_cache = lambda: cache.clear() return wrapper + return decorator diff --git a/test/test_api/test_environment.py b/test/test_api/test_environment.py index 98230e575..6e2cf36b7 100644 --- a/test/test_api/test_environment.py +++ b/test/test_api/test_environment.py @@ -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(): @@ -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