aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2023-01-25 16:55:59 +0000
committerMax Audron <audron@cocaine.farm>2023-01-27 13:44:34 +0000
commit14c0884fd0948c478db165989cca7aaffc9a0504 (patch)
treea30ac2cdc1c297e2db4b41a035f45919d099d189
parent5eee2ac39863f9e44591b50d0710dd2615416a13 (diff)
downloadstable-diffusion-webui-gfx803-14c0884fd0948c478db165989cca7aaffc9a0504.tar.gz
stable-diffusion-webui-gfx803-14c0884fd0948c478db165989cca7aaffc9a0504.tar.bz2
stable-diffusion-webui-gfx803-14c0884fd0948c478db165989cca7aaffc9a0504.zip
use python importlib to load and execute extension modules
previously module attributes like __file__ where not set correctly, leading to scripts getting the directory of the stable-diffusion repo location instead of their own script. This causes problem when loading user data from an external location using the --data-dir flag, as extensions would look for their own code in the stable-diffusion repo location instead of the data dir location. Using pythons importlib functions sets the modules specs correctly and executes them. But this will break extensions if they build paths based on the previously incorrect __file__ attribute.
-rw-r--r--modules/script_loading.py10
1 files changed, 4 insertions, 6 deletions
diff --git a/modules/script_loading.py b/modules/script_loading.py
index f93f0951..a7d2203f 100644
--- a/modules/script_loading.py
+++ b/modules/script_loading.py
@@ -1,16 +1,14 @@
import os
import sys
import traceback
+import importlib.util
from types import ModuleType
def load_module(path):
- with open(path, "r", encoding="utf8") as file:
- text = file.read()
-
- compiled = compile(text, path, 'exec')
- module = ModuleType(os.path.basename(path))
- exec(compiled, module.__dict__)
+ module_spec = importlib.util.spec_from_file_location(os.path.basename(path), path)
+ module = importlib.util.module_from_spec(module_spec)
+ module_spec.loader.exec_module(module)
return module