From f3f412e0b45bbdce663a2e1d6aae9423db3ce27b Mon Sep 17 00:00:00 2001 From: Muhammad Date: Thu, 10 Oct 2024 15:14:22 +0100 Subject: [PATCH] `loadModule` for `__init__.py` and `bs` `loadModule` loads the file directly instead of searching in a directory. --- __init__.py | 7 ++----- __main__.py | 15 ++++++--------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/__init__.py b/__init__.py index 514a3d5..c1196cb 100644 --- a/__init__.py +++ b/__init__.py @@ -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 diff --git a/__main__.py b/__main__.py index cfa6cca..cdf4eec 100755 --- a/__main__.py +++ b/__main__.py @@ -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()