Skip to content

Fix for #791 #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use importlib for Python 3 (fixes #791)
  • Loading branch information
sebastinas committed Jan 29, 2020
commit 596d5f1725e3e30d526723c938c152b1da6f6306
25 changes: 19 additions & 6 deletions bpython/importcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
current_from_import_import,
)

import imp
import os
import sys
import warnings
Expand Down Expand Up @@ -143,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
Expand All @@ -163,12 +166,22 @@ def find_modules(path):
is_package = False
with warnings.catch_warnings():
warnings.simplefilter("ignore", ImportWarning)
fo, pathname, _ = imp.find_module(name, [path])
if fo is not None:
fo.close()
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:
# Yay, package
is_package = True
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:
Expand Down