-
-
Notifications
You must be signed in to change notification settings - Fork 19
Support reloading namespaces #1035
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
07a6366
Support reloading namespaces
chrisrink10 d8571d9
More
chrisrink10 83f535d
Documentation
chrisrink10 794cc11
No println
chrisrink10 d13855f
docstring
chrisrink10 913d188
MyPy being insane
chrisrink10 fc69701
Simplify
chrisrink10 9313c19
Why isn't this working
chrisrink10 081b892
Add namespace full names as aliases, which they were always intended …
chrisrink10 f856d0c
Changelog
chrisrink10 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
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
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
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
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 |
|---|---|---|
|
|
@@ -71,6 +71,11 @@ | |
| from basilisp.lang.util import OBJECT_DUNDER_METHODS, demunge, is_abstract, munge | ||
| from basilisp.util import Maybe | ||
|
|
||
| if sys.version_info >= (3, 9): | ||
| import graphlib | ||
| else: | ||
| import graphlib # type: ignore | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Public constants | ||
|
|
@@ -670,6 +675,49 @@ def __repr__(self): | |
| def __hash__(self): | ||
| return hash(self._name) | ||
|
|
||
| def _get_required_namespaces(self) -> vec.PersistentVector["Namespace"]: | ||
| """Return a vector of all required namespaces (loaded via `require`, `use`, | ||
| or `refer`). | ||
|
|
||
| This vector will include `basilisp.core` unless the namespace was created | ||
| manually without requiring it.""" | ||
| ts: graphlib.TopologicalSorter = graphlib.TopologicalSorter() | ||
|
|
||
| def add_nodes(ns: Namespace) -> None: | ||
| # basilisp.core does actually create a cycle by requiring namespaces | ||
| # that require it, so we cannot add it to the topological sorter here, | ||
| # or it will throw a cycle error | ||
| if ns.name == CORE_NS: | ||
| return | ||
|
|
||
| for aliased_ns in ns.aliases.values(): | ||
| ts.add(ns, aliased_ns) | ||
| add_nodes(aliased_ns) | ||
|
|
||
| for referred_var in ns.refers.values(): | ||
| referred_ns = referred_var.ns | ||
| ts.add(ns, referred_ns) | ||
| add_nodes(referred_ns) | ||
|
|
||
| add_nodes(self) | ||
| return vec.vector(ts.static_order()) | ||
|
|
||
| def reload_all(self) -> "Namespace": | ||
| """Reload all dependency namespaces as by `Namespace.reload()`.""" | ||
| sorted_reload_order = self._get_required_namespaces() | ||
| logger.debug(f"Computed :reload-all order: {sorted_reload_order}") | ||
|
|
||
| for ns in sorted_reload_order: | ||
| ns.reload() | ||
|
|
||
| return self | ||
|
|
||
| def reload(self) -> "Namespace": | ||
| """Reload code in this namespace by reloading the underlying Python module.""" | ||
| with self._lock: | ||
| importlib.reload(self.module) | ||
| return self | ||
|
|
||
| def require(self, ns_name: str, *aliases: sym.Symbol) -> BasilispModule: | ||
| """Require the Basilisp Namespace named by `ns_name` and add any aliases given | ||
| to this Namespace. | ||
|
|
@@ -686,6 +734,7 @@ def require(self, ns_name: str, *aliases: sym.Symbol) -> BasilispModule: | |
| ns_sym = sym.symbol(ns_name) | ||
| ns = self.get(ns_sym) | ||
| assert ns is not None, "Namespace must exist after being required" | ||
| self.add_alias(ns, ns_sym) | ||
|
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. I think it was always my intent to add the fully-qualified name as an alias to the namespace, but it never mattered before now. |
||
| if aliases: | ||
| self.add_alias(ns, *aliases) | ||
| return ns_module | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt this ever happens except in testing, but I was finding that calling
monkeypatch.syspath_prependthat monkeypatch callsimportlib.invalidate_caches()which was causing issues trying to access values from the cache without any guard. Now we just reconstruct the cache from the module if that does happen.