Extend create_environment to accept an executable path

Assume environments specified by the user are safe.
This commit is contained in:
micbou
2018-04-18 16:46:20 +02:00
committed by Dave Halter
parent aa6857d22d
commit 5f37d08761
2 changed files with 25 additions and 4 deletions

View File

@@ -287,11 +287,15 @@ def get_system_environment(version):
def create_environment(path, safe=True): 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` :raises: :exc:`.InvalidPythonEnvironment`
:returns: :class:`Environment` :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)) 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): if not os.path.exists(python):
raise InvalidPythonEnvironment("%s seems to be missing." % python) raise InvalidPythonEnvironment("%s seems to be missing." % python)
if safe and not _is_safe(python): _assert_safe(python, safe)
raise InvalidPythonEnvironment("The python binary is potentially unsafe.")
return python return python
@@ -339,6 +342,12 @@ def _get_executables_from_windows_registry(version):
pass 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): def _is_safe(executable_path):
# Resolve sym links. A venv typically is a symlink to a known Python # Resolve sym links. A venv typically is a symlink to a known Python
# binary. Only virtualenvs copy symlinks around. # binary. Only virtualenvs copy symlinks around.

View File

@@ -1,4 +1,5 @@
import os import os
import sys
from contextlib import contextmanager from contextlib import contextmanager
import pytest import pytest
@@ -6,7 +7,8 @@ import pytest
import jedi import jedi
from jedi._compatibility import py_version from jedi._compatibility import py_version
from jedi.api.environment import get_default_environment, find_virtualenvs, \ 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(): def test_sys_path():
@@ -111,3 +113,13 @@ def test_working_venv(venv_path):
def test_scanning_venvs(venv_path): def test_scanning_venvs(venv_path):
parent_dir = os.path.dirname(venv_path) parent_dir = os.path.dirname(venv_path)
assert any(venv.path == venv_path for venv in find_virtualenvs([parent_dir])) 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