diff --git a/imports.py b/imports.py index 89da04fa..02beb65e 100644 --- a/imports.py +++ b/imports.py @@ -1,6 +1,7 @@ import os import pkgutil import imp +import sys import builtin import modules @@ -48,6 +49,10 @@ class ImportPath(object): and len(self.import_stmt.namespace.names) > 1 def get_nested_import(self, parent): + """ + See documentation of `self.is_nested_import`. + Generates an Import statement, that can be used to fake nested imports. + """ i = self.import_stmt # This is not an existing Import statement. Therefore, set position to # None. @@ -79,6 +84,7 @@ class ImportPath(object): def follow(self): """ + Returns the imported modules. """ if self.import_path: scope, rest = self.follow_file_system() @@ -109,16 +115,22 @@ class ImportPath(object): def follow_str(ns, string): debug.dbg('follow_module', ns, string) if ns: - path = [ns[1]] + try: + return imp.find_module(string, [ns[1]]) + except ImportError: + return imp.find_module(string, builtin.module_find_path) else: path = None debug.dbg('search_module', string, path, self.file_path) - try: - i = imp.find_module(string, path) - except ImportError: - # find builtins (ommit path): - i = imp.find_module(string, builtin.module_find_path) - return i + #sys.path, temp = builtin.module_find_path, sys.path + try: + i = imp.find_module(string, builtin.module_find_path) + except ImportError: + # find builtins (ommit path): + #i = imp.find_module(string) + raise + #sys.path = temp + return i # TODO handle relative paths - they are included in the import object current_namespace = None @@ -176,7 +188,6 @@ def strip_imports(scopes): result.append(s) return result - def remove_star_imports(scope): """ """ diff --git a/test/completion/import_tree/pkg/__init__.py b/test/completion/import_tree/pkg/__init__.py index e8e57d1c..480f2221 100644 --- a/test/completion/import_tree/pkg/__init__.py +++ b/test/completion/import_tree/pkg/__init__.py @@ -1 +1,3 @@ a = list + +from math import * diff --git a/test/completion/imports.py b/test/completion/imports.py index 845587bc..6a0aebb4 100644 --- a/test/completion/imports.py +++ b/test/completion/imports.py @@ -43,12 +43,19 @@ def scope_nested(): #? list import_tree.pkg.a + #? ['sqrt'] + import_tree.pkg.sqrt + #? ['a', 'pkg'] import_tree. #? float() import_tree.pkg.mod1.a + import import_tree.random + #? set + import_tree.random.a + # ----------------- # std lib modules # ----------------- diff --git a/test/completion/thirdparty/pylab.py b/test/completion/thirdparty/pylab.py index 45e121cf..10149fee 100644 --- a/test/completion/thirdparty/pylab.py +++ b/test/completion/thirdparty/pylab.py @@ -22,3 +22,5 @@ numpy.random_integers #? ['random_integers'] numpy.random.random_integers +#? ['sample'] +numpy.random.sample