Skip to content

Commit

Permalink
loadModule for __init__.py and bs
Browse files Browse the repository at this point in the history
`loadModule` loads the file directly instead of searching in
a directory.
  • Loading branch information
nnym committed Oct 10, 2024
1 parent e5333d2 commit f3f412e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
7 changes: 2 additions & 5 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,9 @@ def write(s):
with open(CACHE, "bw") as file:
pickle.dump(cache, file)

def main():
def main(loadModule):
if entry := first(entry for entry in ["bs", "bs.py"] if path.exists(entry)):
entry = path.abspath(entry)
with open(entry) as source: script = compile(source.read(), entry, "exec")

try: exec(script, exports)
try: loadModule("bs", entry)
except Exception as e:
tb = e.__traceback__
while tb and tb.tb_frame.f_code.co_filename != entry: tb = tb.tb_next
Expand Down
15 changes: 6 additions & 9 deletions __main__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#!/usr/bin/env python
import os.path as path
import os.path as osp
import sys
from importlib import util as ilu, machinery as ilm
from importlib import machinery as ilm

def main():
if not (bt := sys.modules.get("bt", None)):
spec = ilm.PathFinder().find_spec("bt", [path.dirname(path.dirname(path.realpath(__file__)))])
bt = ilu.module_from_spec(spec)
sys.modules[bt.__name__] = bt
spec.loader.exec_module(bt)
def loadModule(name, path):
return ilm.SourceFileLoader(name, path).load_module()

bt.main()
def main():
(sys.modules.get("bt", None) or loadModule("bt", osp.join(osp.dirname(osp.realpath(__file__)), "__init__.py"))).main(loadModule)

if __name__ == "__main__": main()

0 comments on commit f3f412e

Please sign in to comment.