fix: pass __file__ to exec() in addon Init.py/InitGui.py loading
Some checks failed
Build and Test / build (pull_request) Has been cancelled

DirMod.run_init() and DirModGui.run_init_gui() use exec(code) to run
addon Init.py and InitGui.py files, but don't pass __file__ in the
globals dict. Addons that reference __file__ (e.g. Silo) crash with
NameError: name '__file__' is not defined.

Pass {"__file__": str(init_py)} as the globals argument so __file__
is available in the executed code, matching the behavior of normal
Python module loading.
This commit is contained in:
forbes
2026-02-14 21:27:25 -06:00
parent cecdb2e68c
commit e10841a6c8
2 changed files with 2 additions and 2 deletions

View File

@@ -1204,7 +1204,7 @@ class DirMod(Mod):
try:
source = init_py.read_text(encoding="utf-8")
code = compile(source, init_py, 'exec')
exec(code)
exec(code, {"__file__": str(init_py)})
except Exception as ex:
Log(f"Init: Initializing {self.path!s}... failed")
Log(utils.HLine)

View File

@@ -295,7 +295,7 @@ class DirModGui(ModGui):
try:
source = init_gui_py.read_text(encoding="utf-8")
code = compile(source, init_gui_py, "exec")
exec(code)
exec(code, {"__file__": str(init_gui_py)})
except Exception as ex:
sep = "-" * 100 + "\n"
Log(f"Init: Initializing {target!s}... failed\n")