diff --git a/jedi/api/environment.py b/jedi/api/environment.py index 51b390f3..7e0a8507 100644 --- a/jedi/api/environment.py +++ b/jedi/api/environment.py @@ -287,11 +287,15 @@ def get_system_environment(version): def create_environment(path, safe=True): """ - Make it possible to create an environment by hand. + Make it possible to manually create an environment by specifying a + Virtualenv path or an executable path. :raises: :exc:`.InvalidPythonEnvironment` :returns: :class:`Environment` """ + if os.path.isfile(path): + _assert_safe(path, safe) + return Environment(_get_python_prefix(path), path) return Environment(path, _get_executable_path(path, safe=safe)) @@ -307,8 +311,7 @@ def _get_executable_path(path, safe=True): if not os.path.exists(python): raise InvalidPythonEnvironment("%s seems to be missing." % python) - if safe and not _is_safe(python): - raise InvalidPythonEnvironment("The python binary is potentially unsafe.") + _assert_safe(python, safe) return python @@ -339,6 +342,12 @@ def _get_executables_from_windows_registry(version): pass +def _assert_safe(executable_path, safe): + if safe and not _is_safe(executable_path): + raise InvalidPythonEnvironment( + "The python binary is potentially unsafe.") + + def _is_safe(executable_path): # Resolve sym links. A venv typically is a symlink to a known Python # binary. Only virtualenvs copy symlinks around. diff --git a/test/test_api/test_environment.py b/test/test_api/test_environment.py index 627336cc..395e2f5e 100644 --- a/test/test_api/test_environment.py +++ b/test/test_api/test_environment.py @@ -1,4 +1,5 @@ import os +import sys from contextlib import contextmanager import pytest @@ -6,7 +7,8 @@ import pytest import jedi from jedi._compatibility import py_version from jedi.api.environment import get_default_environment, find_virtualenvs, \ - InvalidPythonEnvironment, find_system_environments, get_system_environment + InvalidPythonEnvironment, find_system_environments, \ + get_system_environment, create_environment def test_sys_path(): @@ -111,3 +113,13 @@ def test_working_venv(venv_path): def test_scanning_venvs(venv_path): parent_dir = os.path.dirname(venv_path) assert any(venv.path == venv_path for venv in find_virtualenvs([parent_dir])) + + +def test_create_environment_venv_path(venv_path): + environment = create_environment(venv_path) + assert environment.path == venv_path + + +def test_create_environment_executable(): + environment = create_environment(sys.executable) + assert environment.executable == sys.executable