Skip to content
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

fix: import error messages #164

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
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
39 changes: 24 additions & 15 deletions boa/interpret.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,38 @@


class BoaImporter(importlib.abc.MetaPathFinder):
def __init__(self):
self._path_lookup = {}

def find_module(self, fullname, package_path, target=None):
# Return a loader
return self
# note: maybe instead of looping, append path to package_path
path = Path(fullname.replace(".", "/")).with_suffix(".vy")

for prefix in package_path:
# for fullname == "x.y.z"
# prefix looks something like "<...>/site-packages/x"
to_try = Path(prefix).parent / path

if to_try.exists():
self._path_lookup[fullname] = to_try
return self

return None

def load_module(self, fullname):
# Return a module
if fullname in sys.modules:
return sys.modules[fullname]

path = Path(fullname.replace(".", "/")).with_suffix(".vy")
for prefix in sys.path:
to_try = Path(prefix) / path
try:
ret = load_partial(to_try)
break
except (FileNotFoundError, NotADirectoryError):
pass
else:
raise ImportError(fullname)
# import system should guarantee this, but be paranoid
if fullname not in self._path_lookup:
raise ImportError(f"invariant violated: no lookup for {fullname}")

path = self._path_lookup[fullname]
ret = load_partial(path)

# comply with PEP-302:
ret.__name__ = to_try.name
ret.__file__ = str(to_try)
ret.__name__ = path.name
ret.__file__ = str(path)
sys.modules[fullname] = ret
return ret

Expand Down
Loading