From 2696d95d7034897af02a1813476848fee8101266 Mon Sep 17 00:00:00 2001 From: Akinori Hattori Date: Sat, 19 Jul 2014 10:33:08 +0900 Subject: [PATCH] fix dotted_from_fs_path for Windows --- jedi/evaluate/compiled/__init__.py | 37 ++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/jedi/evaluate/compiled/__init__.py b/jedi/evaluate/compiled/__init__.py index 062f14a8..ed3d01ae 100644 --- a/jedi/evaluate/compiled/__init__.py +++ b/jedi/evaluate/compiled/__init__.py @@ -15,6 +15,13 @@ from jedi.evaluate.helpers import FakeName 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): # comply with the parser 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 path is not in the sys.path, just returns None. """ - sep = os.path.sep - shortest = None if sys_path is None: 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: - if fs_path.startswith(s): - path = fs_path[len(s):].strip(sep) - path = re.sub('\.[^%s]*|%s__init__.py$' % (sep, sep), '', path) - dotted = path.split(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 + if (fs_path.startswith(s) and + len(path) < len(s)): + path = s + return _path_re.sub('', fs_path[len(path):].lstrip(os.path.sep)).replace(os.path.sep, '.') def load_module(path, name):