forked from VimPlug/jedi
An empty path given to Jedi should not raise errors. Fixes #577.
This commit is contained in:
@@ -100,9 +100,11 @@ class Script(object):
|
|||||||
encoding = source_encoding
|
encoding = source_encoding
|
||||||
|
|
||||||
self._orig_path = path
|
self._orig_path = path
|
||||||
self.path = None if path is None else os.path.abspath(path)
|
# An empty path (also empty string) should always result in no path.
|
||||||
|
self.path = os.path.abspath(path) if path else None
|
||||||
|
|
||||||
if source is None:
|
if source is None:
|
||||||
|
# TODO add a better warning than the traceback!
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
source = f.read()
|
source = f.read()
|
||||||
|
|
||||||
@@ -131,8 +133,8 @@ class Script(object):
|
|||||||
|
|
||||||
def _get_module(self):
|
def _get_module(self):
|
||||||
cache.invalidate_star_import_cache(self._path)
|
cache.invalidate_star_import_cache(self._path)
|
||||||
parser = FastParser(self._grammar, self._source, self._path)
|
parser = FastParser(self._grammar, self._source, self.path)
|
||||||
save_parser(self._path, parser, pickling=False)
|
save_parser(self.path, parser, pickling=False)
|
||||||
|
|
||||||
module = self._evaluator.wrap(parser.module)
|
module = self._evaluator.wrap(parser.module)
|
||||||
imports.add_module(self._evaluator, unicode(module.name), module)
|
imports.add_module(self._evaluator, unicode(module.name), module)
|
||||||
|
|||||||
@@ -201,20 +201,21 @@ class Importer(object):
|
|||||||
base = []
|
base = []
|
||||||
if level > len(base):
|
if level > len(base):
|
||||||
path = module.py__file__()
|
path = module.py__file__()
|
||||||
import_path = list(import_path)
|
if path is not None:
|
||||||
for i in range(level):
|
import_path = list(import_path)
|
||||||
path = os.path.dirname(path)
|
for i in range(level):
|
||||||
dir_name = os.path.basename(path)
|
path = os.path.dirname(path)
|
||||||
# This is not the proper way to do relative imports. However, since
|
dir_name = os.path.basename(path)
|
||||||
# Jedi cannot be sure about the entry point, we just calculate an
|
# This is not the proper way to do relative imports. However, since
|
||||||
# absolute path here.
|
# Jedi cannot be sure about the entry point, we just calculate an
|
||||||
if dir_name:
|
# absolute path here.
|
||||||
import_path.insert(0, dir_name)
|
if dir_name:
|
||||||
else:
|
import_path.insert(0, dir_name)
|
||||||
_add_error(self._evaluator, import_path[-1])
|
else:
|
||||||
import_path = []
|
_add_error(self._evaluator, import_path[-1])
|
||||||
# TODO add import error.
|
import_path = []
|
||||||
debug.warning('Attempted relative import beyond top-level package.')
|
# TODO add import error.
|
||||||
|
debug.warning('Attempted relative import beyond top-level package.')
|
||||||
else:
|
else:
|
||||||
# Here we basically rewrite the level to 0.
|
# Here we basically rewrite the level to 0.
|
||||||
import_path = tuple(base) + import_path
|
import_path = tuple(base) + import_path
|
||||||
|
|||||||
@@ -852,7 +852,8 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, tree.Module, Wrapper)):
|
|||||||
def _get_init_directory(self):
|
def _get_init_directory(self):
|
||||||
for suffix, _, _ in imp.get_suffixes():
|
for suffix, _, _ in imp.get_suffixes():
|
||||||
ending = '__init__' + suffix
|
ending = '__init__' + suffix
|
||||||
if self.py__file__().endswith(ending):
|
py__file__ = self.py__file__()
|
||||||
|
if py__file__ is not None and py__file__.endswith(ending):
|
||||||
# Remove the ending, including the separator.
|
# Remove the ending, including the separator.
|
||||||
return self.py__file__()[:-len(ending) - 1]
|
return self.py__file__()[:-len(ending) - 1]
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -18,17 +18,17 @@ def test_goto_definition_on_import():
|
|||||||
def test_complete_on_empty_import():
|
def test_complete_on_empty_import():
|
||||||
assert Script("from datetime import").completions()[0].name == 'import'
|
assert Script("from datetime import").completions()[0].name == 'import'
|
||||||
# should just list the files in the directory
|
# should just list the files in the directory
|
||||||
assert 10 < len(Script("from .", path='').completions()) < 30
|
assert 10 < len(Script("from .", path='whatever.py').completions()) < 30
|
||||||
|
|
||||||
# Global import
|
# Global import
|
||||||
assert len(Script("from . import", 1, 5, '').completions()) > 30
|
assert len(Script("from . import", 1, 5, 'whatever.py').completions()) > 30
|
||||||
# relative import
|
# relative import
|
||||||
assert 10 < len(Script("from . import", 1, 6, '').completions()) < 30
|
assert 10 < len(Script("from . import", 1, 6, 'whatever.py').completions()) < 30
|
||||||
|
|
||||||
# Global import
|
# Global import
|
||||||
assert len(Script("from . import classes", 1, 5, '').completions()) > 30
|
assert len(Script("from . import classes", 1, 5, 'whatever.py').completions()) > 30
|
||||||
# relative import
|
# relative import
|
||||||
assert 10 < len(Script("from . import classes", 1, 6, '').completions()) < 30
|
assert 10 < len(Script("from . import classes", 1, 6, 'whatever.py').completions()) < 30
|
||||||
|
|
||||||
wanted = set(['ImportError', 'import', 'ImportWarning'])
|
wanted = set(['ImportError', 'import', 'ImportWarning'])
|
||||||
assert set([c.name for c in Script("import").completions()]) == wanted
|
assert set([c.name for c in Script("import").completions()]) == wanted
|
||||||
|
|||||||
Reference in New Issue
Block a user