diff --git a/bpython/importcompletion.py b/bpython/importcompletion.py index 6dea46125..0fbc46d58 100644 --- a/bpython/importcompletion.py +++ b/bpython/importcompletion.py @@ -32,7 +32,6 @@ current_from_import_import, ) -import imp import os import sys import warnings @@ -43,6 +42,8 @@ SUFFIXES = importlib.machinery.all_suffixes() else: + import imp + SUFFIXES = [suffix for suffix, mode, type in imp.get_suffixes()] # The cached list of all known modules @@ -141,6 +142,10 @@ def find_modules(path): filenames = os.listdir(path) except EnvironmentError: filenames = [] + + if py3: + finder = importlib.machinery.FileFinder(path) + for name in filenames: if not any(name.endswith(suffix) for suffix in SUFFIXES): # Possibly a package @@ -158,9 +163,25 @@ def find_modules(path): # Workaround for issue #166 continue try: + is_package = False with warnings.catch_warnings(): warnings.simplefilter("ignore", ImportWarning) - fo, pathname, _ = imp.find_module(name, [path]) + if py3: + spec = finder.find_spec(name) + if spec is None: + continue + if spec.submodule_search_locations is not None: + pathname = spec.submodule_search_locations[0] + is_package = True + else: + pathname = spec.origin + else: + fo, pathname, _ = imp.find_module(name, [path]) + if fo is not None: + fo.close() + else: + # Yay, package + is_package = True except (ImportError, IOError, SyntaxError): continue except UnicodeEncodeError: @@ -168,10 +189,7 @@ def find_modules(path): # invalid encoding continue else: - if fo is not None: - fo.close() - else: - # Yay, package + if is_package: for subname in find_modules(pathname): if subname != "__init__": yield "%s.%s" % (name, subname)
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: