-
Notifications
You must be signed in to change notification settings - Fork 677
fix(moduleloader): isolate module namespaces and cache loaded modules #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Marshall-Hallenbeck
wants to merge
1
commit into
main
Choose a base branch
from
fix/moduleloader-isolated-namespaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import sys | ||
|
|
||
| from os import listdir | ||
| from os.path import dirname | ||
| from os.path import basename, dirname | ||
| from os.path import join as path_join | ||
|
|
||
| from nxc.context import Context | ||
|
|
@@ -46,11 +46,29 @@ def module_is_sane(self, module, module_path): | |
|
|
||
| return not module_error | ||
|
|
||
| module_cache = {} | ||
|
|
||
| @classmethod | ||
| def load_module_file(cls, module_path): | ||
| """Load a module file and return the raw module object with isolated namespace. | ||
|
|
||
| Each module gets a unique name to avoid sys.modules collisions that | ||
| caused NXCModule class references to be overwritten when loading | ||
| multiple modules. Results are cached so each file is only parsed | ||
| and executed once regardless of how many targets are scanned. | ||
| """ | ||
| if module_path not in cls.module_cache: | ||
| module_name = f"nxc_module_{basename(module_path)[:-3]}" | ||
| spec = importlib.util.spec_from_file_location(module_name, module_path) | ||
Marshall-Hallenbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
| cls.module_cache[module_path] = module | ||
| return cls.module_cache[module_path] | ||
Marshall-Hallenbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def load_module(self, module_path): | ||
| """Load a module, initializing it and checking that it has the proper attributes""" | ||
| try: | ||
| spec = importlib.util.spec_from_file_location("NXCModule", module_path) | ||
| module = spec.loader.load_module().NXCModule() | ||
| module = self.load_module_file(module_path).NXCModule() | ||
|
|
||
| if self.module_is_sane(module, module_path): | ||
| return module | ||
|
|
@@ -87,8 +105,7 @@ def init_module(self, module_path): | |
| def get_module_info(self, module_path): | ||
| """Get the path, description, and options from a module""" | ||
| try: | ||
| spec = importlib.util.spec_from_file_location("NXCModule", module_path) | ||
| module_spec = spec.loader.load_module().NXCModule | ||
| module_spec = self.load_module_file(module_path).NXCModule | ||
|
|
||
| module = { | ||
| f"{module_spec.name}": { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.