Skip to content

Commit

Permalink
Merge pull request #164 from charles-cooper/fix/import-errors
Browse files Browse the repository at this point in the history
fix: import error messages
  • Loading branch information
charles-cooper authored Feb 16, 2024
2 parents 51da344 + 2852746 commit bec847b
Showing 1 changed file with 24 additions and 15 deletions.
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

0 comments on commit bec847b

Please sign in to comment.