Add a py__path__ method to the ModuleWrapper, that behaves very similar to a package's __path__ attribute.

This commit is contained in:
Dave Halter
2015-04-20 16:21:00 +02:00
parent df9452f210
commit 77a37be83a
2 changed files with 34 additions and 7 deletions

View File

@@ -448,14 +448,18 @@ class _Importer(object):
# This is a recursive way of importing that works great with
# the module cache.
base = self._do_import(import_path[:-1], sys_path)
path = base.py__file__()
debug.dbg('search_module %s in pkg %s', module_name, path)
module_file, module_path, is_pkg = \
find_module(import_parts[-1], [path])
raise NotImplementedError
try:
paths = base.py__path__()
except AttributeError:
# The module is not a package.
raise NotImplementedError
else:
debug.dbg('search_module %s in paths %s', module_name, paths)
for path in paths:
module_file, module_path, is_pkg = \
find_module(import_parts[-1], [path])
else:
debug.dbg('search_module %s in %s', module_name, self.file_path)
debug.dbg('search_module %s in %s', import_parts[-1], self.file_path)
# Override the sys.path. It works only good that way.
# Injecting the path directly into `find_module` did not work.
sys.path, temp = sys_path, sys.path

View File

@@ -30,6 +30,7 @@ __
"""
import os
import pkgutil
import imp
from itertools import chain
from jedi._compatibility import use_metaclass, unicode, Python3Method
@@ -762,6 +763,28 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module, Wrapper)):
def py__name__(self):
return self._evaluator.module_name_cache[self]
def py__file__(self):
return self._module.path
@property
def py__path__(self):
"""
In case of a package, this returns Python's __path__ attribute, which
is a list of paths (strings).
Raises an AttributeError if the module is not a package.
"""
def return_value():
return [path]
for suffix, _, _ in imp.get_suffixes():
ending = '__init__' + suffix
if self.py__file__().endswith(ending):
# Remove the ending, including the separator.
path = self.py__file__()[:-len(ending) - 1]
return return_value
else:
raise AttributeError('Only packages have __path__ attributes.')
@memoize_default()
def _sub_modules_dict(self):
"""