Different __init__ file searching.

This commit is contained in:
Dave Halter
2015-04-28 01:58:49 +02:00
parent b8bb258677
commit 0d406d27fd
+14 -14
View File
@@ -159,6 +159,18 @@ def _add_error(evaluator, name, message=None):
analysis.add(evaluator, 'import-error', name, message) analysis.add(evaluator, 'import-error', name, message)
def get_init_path(directory_path):
"""
The __init__ file can be searched in a directory. If found return it, else
None.
"""
for suffix, _, _ in imp.get_suffixes():
path = os.path.join(directory_path, '__init__' + suffix)
if os.path.exists(path):
return path
return None
class Importer(object): class Importer(object):
def __init__(self, evaluator, import_path, module, level=0): def __init__(self, evaluator, import_path, module, level=0):
""" """
@@ -171,7 +183,7 @@ class Importer(object):
directory of the module calling ``__import__()`` (see PEP 328 for the directory of the module calling ``__import__()`` (see PEP 328 for the
details). details).
:param import_path: List of namespaces (strings). :param import_path: List of namespaces (strings or Names).
""" """
debug.speed('import %s' % (import_path,)) debug.speed('import %s' % (import_path,))
self._evaluator = evaluator self._evaluator = evaluator
@@ -213,12 +225,6 @@ class Importer(object):
"""Returns the import path as pure strings instead of `Name`.""" """Returns the import path as pure strings instead of `Name`."""
return tuple(str(name) for name in self.import_path) return tuple(str(name) for name in self.import_path)
def get_relative_path(self):
path = self.file_path
for i in range(self.level - 1):
path = os.path.dirname(path)
return path
@memoize_default() @memoize_default()
def sys_path_with_modifications(self): def sys_path_with_modifications(self):
in_path = [] in_path = []
@@ -359,12 +365,7 @@ class Importer(object):
if is_pkg: if is_pkg:
# In this case, we don't have a file yet. Search for the # In this case, we don't have a file yet. Search for the
# __init__ file. # __init__ file.
for suffix, _, _ in imp.get_suffixes(): module_path = get_init_path(module_path)
path = os.path.join(module_path, '__init__' + suffix)
if os.path.exists(path):
if suffix == '.py':
module_path = path
break
elif module_file: elif module_file:
source = module_file.read() source = module_file.read()
module_file.close() module_file.close()
@@ -478,7 +479,6 @@ def _load_module(evaluator, path=None, source=None, sys_path=None, module_name=N
cached = cache.load_parser(path) cached = cache.load_parser(path)
module = load(source) if cached is None else cached.module module = load(source) if cached is None else cached.module
# TODO return mod instead of just something.
module = evaluator.wrap(module) module = evaluator.wrap(module)
return module return module