fix dotted_from_fs_path for Windows

This commit is contained in:
Akinori Hattori
2014-07-19 10:33:08 +09:00
parent 2616143d10
commit 2696d95d70
+25 -12
View File
@@ -15,6 +15,13 @@ from jedi.evaluate.helpers import FakeName
from . import fake from . import fake
_sep = os.path.sep
if os.path.altsep is not None:
_sep += os.path.altsep
_path_re = re.compile('(?:\.[^{0}]+|[{0}]__init__\.py)$'.format(re.escape(_sep)))
del _sep
class CompiledObject(Base): class CompiledObject(Base):
# comply with the parser # comply with the parser
start_pos = 0, 0 start_pos = 0, 0
@@ -220,21 +227,27 @@ def dotted_from_fs_path(fs_path, sys_path=None):
compares the path with sys.path and then returns the dotted_path. If the compares the path with sys.path and then returns the dotted_path. If the
path is not in the sys.path, just returns None. path is not in the sys.path, just returns None.
""" """
sep = os.path.sep
shortest = None
if sys_path is None: if sys_path is None:
sys_path = get_sys_path() sys_path = get_sys_path()
# prefer
# - UNIX
# /path/to/pythonX.Y/lib-dynload
# /path/to/pythonX.Y/site-packages
# - Windows
# C:\path\to\DLLs
# C:\path\to\Lib\site-packages
# over
# - UNIX
# /path/to/pythonX.Y
# - Windows
# C:\path\to\Lib
path = ''
for s in sys_path: for s in sys_path:
if fs_path.startswith(s): if (fs_path.startswith(s) and
path = fs_path[len(s):].strip(sep) len(path) < len(s)):
path = re.sub('\.[^%s]*|%s__init__.py$' % (sep, sep), '', path) path = s
dotted = path.split(sep) return _path_re.sub('', fs_path[len(path):].lstrip(os.path.sep)).replace(os.path.sep, '.')
dotted = '.'.join(dotted)
# At this point dotted could be both `lib-dynload.datetime` and
# `datetime`. The shorter one is typically the module we want.
if shortest is None or len(shortest) > len(dotted):
shortest = dotted
return shortest
def load_module(path, name): def load_module(path, name):