forked from VimPlug/jedi
Trying to use the import machinery to import jedi/parso in python3.4+
The problem was that adding stuff to sys.path is simply very risky, because it already caused import issues (because enum was installed in 2.7). It was bound to cause other issues
This commit is contained in:
@@ -1,18 +1,37 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Get the path to jedi.
|
||||
_d = os.path.dirname
|
||||
_jedi_path = _d(_d(_d(_d(_d(__file__)))))
|
||||
_parso_path = sys.argv[1]
|
||||
# Remove the first entry, because it's simply a directory entry to this
|
||||
# directory.
|
||||
from importlib.machinery import FileFinder
|
||||
|
||||
|
||||
class ExactImporter(object):
|
||||
def __init__(self, path_dct):
|
||||
self._path_dct = path_dct
|
||||
|
||||
def find_module(self, fullname, path=None):
|
||||
if path is None and fullname in self._path_dct:
|
||||
loader, _ = FileFinder(self._path_dct[fullname]).find_loader(fullname)
|
||||
return loader
|
||||
return None
|
||||
|
||||
|
||||
def _create_importer():
|
||||
# Get the path to jedi.
|
||||
_d = os.path.dirname
|
||||
_jedi_path = _d(_d(_d(_d(_d(__file__)))))
|
||||
_parso_path = sys.argv[1]
|
||||
# The paths are the directory that jedi and parso lie in.
|
||||
return ExactImporter({'jedi': _jedi_path, 'parso': _parso_path})
|
||||
|
||||
|
||||
# Remove the first entry, because it's simply a directory entry that equals
|
||||
# this directory.
|
||||
del sys.path[0]
|
||||
|
||||
# This is kind of stupid. We actually don't want to modify the sys path but
|
||||
# simply import something from a specific location.
|
||||
sys.path[0:0] = [_jedi_path, _parso_path]
|
||||
from jedi.evaluate.compiled import subprocess
|
||||
sys.path[0:2] = []
|
||||
# Try to import jedi/parso.
|
||||
sys.meta_path.insert(0, _create_importer())
|
||||
from jedi.evaluate.compiled import subprocess # NOQA
|
||||
sys.meta_path.pop(0)
|
||||
|
||||
# And finally start the client.
|
||||
subprocess.Listener().listen()
|
||||
|
||||
Reference in New Issue
Block a user