-
Notifications
You must be signed in to change notification settings - Fork 22
add Context manager and default context under threading #38
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
thisiscam
wants to merge
12
commits into
inducer:main
Choose a base branch
from
thisiscam:default_context_managing
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.
Open
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f06584
add Context manager and default context under threading
thisiscam b0be767
fix flake8
thisiscam cc95c56
Update islpy/__init__.py
thisiscam d496980
Merge remote-tracking branch 'inducer/master' into default_context_ma…
thisiscam 345142f
add documentation and fixes comments
thisiscam f9083b7
Update doc/ref_fundamental.rst
thisiscam 113df5f
Update doc/reference.rst
thisiscam 74a0da7
Update islpy/__init__.py
thisiscam ac4f449
Update islpy/__init__.py
thisiscam 96a5af2
update docs
thisiscam 2ae695f
Merge branch 'default_context_managing' of https://github.com/thisisc…
thisiscam 71da6cd
update docs
thisiscam 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 |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |
| THE SOFTWARE. | ||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| import islpy._isl as _isl | ||
| from islpy.version import VERSION, VERSION_TEXT # noqa | ||
| import six | ||
|
|
@@ -145,14 +147,67 @@ | |
| EXPR_CLASSES = tuple(cls for cls in ALL_CLASSES | ||
| if "Aff" in cls.__name__ or "Polynomial" in cls.__name__) | ||
|
|
||
| DEFAULT_CONTEXT = Context() | ||
|
|
||
| def _module_property(func): | ||
| """Decorator to turn module functions into properties. | ||
| Function names must be prefixed with an underscore.""" | ||
| module = sys.modules[func.__module__] | ||
|
|
||
| def _get_default_context(): | ||
| """A callable to get the default context for the benefit of Python's | ||
| ``__reduce__`` protocol. | ||
| """ | ||
| return DEFAULT_CONTEXT | ||
| def base_getattr(name): | ||
| raise AttributeError( | ||
| f"module '{module.__name__}' has no attribute '{name}'") | ||
|
|
||
| old_getattr = getattr(module, "__getattr__", base_getattr) | ||
|
|
||
| def new_getattr(name): | ||
| if f"_{name}" == func.__name__: | ||
| return func() | ||
| else: | ||
| return old_getattr(name) | ||
|
|
||
| module.__getattr__ = new_getattr | ||
| return func | ||
|
|
||
|
|
||
| import threading | ||
|
|
||
|
|
||
| _thread_local_storage = threading.local() | ||
|
|
||
|
|
||
| def _check_init_default_context(): | ||
| if not hasattr(_thread_local_storage, "islpy_default_contexts"): | ||
| _thread_local_storage.islpy_default_contexts = [Context()] | ||
|
|
||
|
|
||
| def get_default_context(): | ||
inducer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Get or create the default context under current thread.""" | ||
| _check_init_default_context() | ||
| return _thread_local_storage.islpy_default_contexts[-1] | ||
|
|
||
|
|
||
| import contextlib | ||
|
||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def push_context(ctx=None): | ||
| if ctx is None: | ||
| ctx = Context() | ||
| _check_init_default_context() | ||
| _thread_local_storage.islpy_default_contexts.append(ctx) | ||
| yield ctx | ||
| _thread_local_storage.islpy_default_contexts.pop() | ||
|
|
||
|
|
||
| @_module_property | ||
| def _DEFAULT_CONTEXT(): # noqa: N802 | ||
| from warnings import warn | ||
| warn("Use of islpy.DEFAULT_CONTEXT is deprecated " | ||
| "and will be removed in the future." | ||
thisiscam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| " Please use `islpy.get_default_context()` instead. ", | ||
| FutureWarning, | ||
| stacklevel=3) | ||
| return get_default_context() | ||
|
|
||
|
|
||
| def _read_from_str_wrapper(cls, context, s): | ||
|
|
@@ -168,10 +223,14 @@ def _add_functionality(): | |
| # {{{ Context | ||
|
|
||
| def context_reduce(self): | ||
| if self._wraps_same_instance_as(DEFAULT_CONTEXT): | ||
| return (_get_default_context, ()) | ||
| else: | ||
| return (Context, ()) | ||
| return (get_default_context, ()) | ||
inducer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def context_copy(self): | ||
| return self | ||
|
|
||
| def context_deepcopy(self, memo): | ||
| del memo | ||
| return self | ||
inducer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def context_eq(self, other): | ||
| return isinstance(other, Context) and self._wraps_same_instance_as(other) | ||
|
|
@@ -180,9 +239,10 @@ def context_ne(self, other): | |
| return not self.__eq__(other) | ||
|
|
||
| Context.__reduce__ = context_reduce | ||
| Context.__copy__ = context_copy | ||
| Context.__deepcopy__ = context_deepcopy | ||
| Context.__eq__ = context_eq | ||
| Context.__ne__ = context_ne | ||
|
|
||
| # }}} | ||
|
|
||
| # {{{ generic initialization, pickling | ||
|
|
@@ -197,7 +257,7 @@ def obj_new(cls, s=None, context=None): | |
| return cls._prev_new(cls) | ||
|
|
||
| if context is None: | ||
| context = DEFAULT_CONTEXT | ||
| context = get_default_context() | ||
|
|
||
| result = cls.read_from_str(context, s) | ||
| return result | ||
|
|
@@ -473,7 +533,7 @@ def obj_get_coefficients_by_name(self, dimtype=None, dim_to_name=None): | |
|
|
||
| def id_new(cls, name, user=None, context=None): | ||
| if context is None: | ||
| context = DEFAULT_CONTEXT | ||
| context = get_default_context() | ||
|
|
||
| result = cls.alloc(context, name, user) | ||
| result._made_from_python = True | ||
|
|
@@ -777,7 +837,7 @@ def expr_like_floordiv(self, other): | |
|
|
||
| def val_new(cls, src, context=None): | ||
| if context is None: | ||
| context = DEFAULT_CONTEXT | ||
| context = get_default_context() | ||
|
|
||
| if isinstance(src, six.string_types): | ||
| result = cls.read_from_str(context, src) | ||
|
|
@@ -1274,7 +1334,7 @@ def make_zero_and_vars(set_vars, params=[], ctx=None): | |
| ) | ||
| """ | ||
| if ctx is None: | ||
| ctx = DEFAULT_CONTEXT | ||
| ctx = get_default_context() | ||
|
|
||
| if isinstance(set_vars, str): | ||
| set_vars = [s.strip() for s in set_vars.split(",")] | ||
|
|
||
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
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.