fix: pass __file__ to exec() in addon Init.py/InitGui.py loading
Some checks failed
Build and Test / build (pull_request) Has been cancelled
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:
@@ -1204,7 +1204,7 @@ class DirMod(Mod):
|
|||||||
try:
|
try:
|
||||||
source = init_py.read_text(encoding="utf-8")
|
source = init_py.read_text(encoding="utf-8")
|
||||||
code = compile(source, init_py, 'exec')
|
code = compile(source, init_py, 'exec')
|
||||||
exec(code)
|
exec(code, {"__file__": str(init_py)})
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
Log(f"Init: Initializing {self.path!s}... failed")
|
Log(f"Init: Initializing {self.path!s}... failed")
|
||||||
Log(utils.HLine)
|
Log(utils.HLine)
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ class DirModGui(ModGui):
|
|||||||
try:
|
try:
|
||||||
source = init_gui_py.read_text(encoding="utf-8")
|
source = init_gui_py.read_text(encoding="utf-8")
|
||||||
code = compile(source, init_gui_py, "exec")
|
code = compile(source, init_gui_py, "exec")
|
||||||
exec(code)
|
exec(code, {"__file__": str(init_gui_py)})
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
sep = "-" * 100 + "\n"
|
sep = "-" * 100 + "\n"
|
||||||
Log(f"Init: Initializing {target!s}... failed\n")
|
Log(f"Init: Initializing {target!s}... failed\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user