Fixing a import logic issue. We don't want to trigger if __name__ ... during _prep_function() calls. So we'll import the module with a specific namespace containing the .py which shouldn't be able to happen when normal programmers do normal imports as .py gets removed normally.

This commit is contained in:
Anton Hvornum 2020-09-30 09:38:46 +00:00
parent 1bff423195
commit 63d774b7d7
1 changed files with 3 additions and 2 deletions

View File

@ -85,10 +85,11 @@ class Profile():
# def py_exec_mock(self):
# spec.loader.exec_module(imported)
def load_instructions(self):
def load_instructions(self, namespace=None):
if (absolute_path := self.path):
if os.path.splitext(absolute_path)[1] == '.py':
namespace = os.path.splitext(os.path.basename(absolute_path))[0]
if not namespace:
namespace = os.path.splitext(os.path.basename(absolute_path))[0]
spec = importlib.util.spec_from_file_location(namespace, absolute_path)
imported = importlib.util.module_from_spec(spec)
sys.modules[namespace] = imported