mirror of
https://github.com/davidhalter/jedi.git
synced 2026-02-12 09:51:39 +08:00
Generalize the use of smart import paths
Now a lot more parts of the current scripts path are used as a sys path.
This commit is contained in:
@@ -101,11 +101,13 @@ class Script(object):
|
||||
sys_path = list(map(force_unicode, sys_path))
|
||||
|
||||
# Load the Python grammar of the current interpreter.
|
||||
project = get_default_project(path or os.getcwd())
|
||||
project = get_default_project(self.path or os.getcwd())
|
||||
# TODO deprecate and remove sys_path from the Script API.
|
||||
if sys_path is not None:
|
||||
project._sys_path = sys_path
|
||||
self._evaluator = Evaluator(project, environment=environment, script_path=path)
|
||||
self._evaluator = Evaluator(
|
||||
project, environment=environment, script_path=self.path
|
||||
)
|
||||
self._project = project
|
||||
debug.speed('init')
|
||||
self._module_node, source = self._evaluator.parse_and_get_code(
|
||||
|
||||
@@ -16,6 +16,15 @@ _CONTAINS_POTENTIAL_PROJECT = 'setup.py', '.git', '.hg', 'MANIFEST.in'
|
||||
_SERIALIZER_VERSION = 1
|
||||
|
||||
|
||||
def _remove_duplicates_from_path(path):
|
||||
used = set()
|
||||
for p in path:
|
||||
if p in used:
|
||||
continue
|
||||
used.add(p)
|
||||
yield p
|
||||
|
||||
|
||||
def _force_unicode_list(lst):
|
||||
return list(map(force_unicode, lst))
|
||||
|
||||
@@ -98,15 +107,28 @@ class Project(object):
|
||||
|
||||
sys_path = list(self._get_base_sys_path(environment))
|
||||
if self._smart_sys_path:
|
||||
suffixed.append(self._path)
|
||||
|
||||
if evaluator.script_path is not None:
|
||||
suffixed += detect_additional_paths(evaluator, evaluator.script_path)
|
||||
|
||||
suffixed.append(self._path)
|
||||
traversed = []
|
||||
for parent in traverse_parents(evaluator.script_path):
|
||||
traversed.append(parent)
|
||||
if parent == self._path:
|
||||
# Don't go futher than the project path.
|
||||
break
|
||||
|
||||
# AFAIK some libraries have imports like `foo.foo.bar`, which
|
||||
# leads to the conclusion to by default prefer longer paths
|
||||
# rather than shorter ones by default.
|
||||
suffixed += reversed(traversed)
|
||||
|
||||
if self._django:
|
||||
prefixed.append(self._path)
|
||||
|
||||
return _force_unicode_list(prefixed) + sys_path + _force_unicode_list(suffixed)
|
||||
path = _force_unicode_list(prefixed) + sys_path + _force_unicode_list(suffixed)
|
||||
return list(_remove_duplicates_from_path(path))
|
||||
|
||||
def save(self):
|
||||
data = dict(self.__dict__)
|
||||
|
||||
Reference in New Issue
Block a user