diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2016-05-10 11:56:46 +0300 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-05-10 12:01:24 +0300 |
commit | 71ad069f79a1a2fddcd89606c9236249d9b5e2e1 (patch) | |
tree | 50774e35403a6581abe65efe5277bf9bf7debfc6 /astroid/modutils.py | |
parent | 1a757073de2d0ca31af41c1470dcf09ad7d7f334 (diff) | |
download | astroid-git-71ad069f79a1a2fddcd89606c9236249d9b5e2e1.tar.gz |
Don't look for namespace packages implicitly into functions doing concrete imports.
Diffstat (limited to 'astroid/modutils.py')
-rw-r--r-- | astroid/modutils.py | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/astroid/modutils.py b/astroid/modutils.py index 31d2fdd3..6a25f4dc 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -296,7 +296,7 @@ def load_module_from_file(filepath, path=None, use_sys=True, extrapath=None): return load_module_from_modpath(modpath, path, use_sys) -def _check_init(path, mod_path): +def check_modpath_has_init(path, mod_path): """check there are some __init__.py all along the way""" modpath = [] for part in mod_path: @@ -304,11 +304,34 @@ def _check_init(path, mod_path): path = os.path.join(path, part) if not _has_init(path): old_namespace = _is_namespace('.'.join(modpath)) - if not (old_namespace or sys.version_info[:2] > (3, 3)): + if not old_namespace: return False return True +def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None): + filename = _path_from_filename(filename) + filename = os.path.abspath(filename) + base = os.path.splitext(filename)[0] + if extrapath is not None: + for path_ in extrapath: + path = os.path.abspath(path_) + if path and os.path.normcase(base[:len(path)]) == os.path.normcase(path): + submodpath = [pkg for pkg in base[len(path):].split(os.sep) + if pkg] + if is_package_cb(path, submodpath[:-1]): + return extrapath[path_].split('.') + submodpath + for path in sys.path: + path = _cache_normalize_path(path) + if path and os.path.normcase(base).startswith(path): + modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg] + if is_package_cb(path, modpath[:-1]): + return modpath + raise ImportError('Unable to find module for %s in %s' % ( + filename, ', \n'.join(sys.path))) + + + def modpath_from_file(filename, extrapath=None): """given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.') @@ -329,25 +352,7 @@ def modpath_from_file(filename, extrapath=None): :rtype: list(str) :return: the corresponding splitted module's name """ - filename = _path_from_filename(filename) - filename = os.path.abspath(filename) - base = os.path.splitext(filename)[0] - if extrapath is not None: - for path_ in extrapath: - path = os.path.abspath(path_) - if path and os.path.normcase(base[:len(path)]) == os.path.normcase(path): - submodpath = [pkg for pkg in base[len(path):].split(os.sep) - if pkg] - if _check_init(path, submodpath[:-1]): - return extrapath[path_].split('.') + submodpath - for path in sys.path: - path = _cache_normalize_path(path) - if path and os.path.normcase(base).startswith(path): - modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg] - if _check_init(path, modpath[:-1]): - return modpath - raise ImportError('Unable to find module for %s in %s' % ( - filename, ', \n'.join(sys.path))) + return modpath_from_file_with_callback(filename, extrapath, check_modpath_has_init) def file_from_modpath(modpath, path=None, context_file=None): |