Enabling load_instructions() to set a temporary namespace, and then reverting it after the instructions are loaded. This is to temporarly override the namespace during import - enabling avoidance of triggering __name__ checks, and at the same time reverting back the namespace automatically to enable .execute() on the script (reusability of classes)

This commit is contained in:
Anton Hvornum 2021-03-21 15:07:45 +01:00
parent 9ee6479701
commit 28606cde61
No known key found for this signature in database
GPG Key ID: F1234C5BA67C59DF
1 changed files with 5 additions and 2 deletions

View File

@ -135,11 +135,14 @@ class Script():
imported = importlib.util.module_from_spec(self.spec)
sys.modules[self.namespace] = imported
print(f"Imported {self} into sys.modules with namespace {self.namespace}.")
print(f"Imported {self} into sys.modules with namespace {self.namespace}")
if '.py' not in self.namespace:
raise KeyError(f"Debugging: {self.namespace}, {reset_namespace}, {self}")
if reset_namespace:
self.namespace = original_namespace
return self
def execute(self):