misc: Snapshot the environment when a switchenv is entered - #3019
Open
MaxFreedomPollard wants to merge 1 commit into
Open
misc: Snapshot the environment when a switchenv is entered#3019MaxFreedomPollard wants to merge 1 commit into
MaxFreedomPollard wants to merge 1 commit into
Conversation
mloubout
approved these changes
Sep 7, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3019 +/- ##
==========================================
- Coverage 83.77% 79.40% -4.38%
==========================================
Files 257 257
Lines 55007 55025 +18
Branches 4708 4708
==========================================
- Hits 46083 43692 -2391
- Misses 8109 10467 +2358
- Partials 815 866 +51
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
|
Needs a rebase, but then it's good to go |
`switchenv.__init__` took the environment snapshot used by `__exit__` to restore the environment, so the snapshot dated from construction rather than from entry (devito/parameters.py, `switchenv.__init__`). `SwitchDecorator.__call__` wraps the decorated function in `with self`, so a `switchenv` used as a decorator is constructed once and entered on every call. Because `__exit__` does `os.environ.clear()` followed by `os.environ.update(self.previous)`, the first call reverted the environment to how it looked when the decorator was applied, silently dropping every variable set since then. The same happened for a `switchenv` object stored and reused as a context manager. The sibling `switchconfig` already rebuilds `self.previous` inside `__enter__`. This does the same for `switchenv`, taking the snapshot at the top of `__enter__` before the device vars are popped, so the restored environment is unchanged for a single use.
MaxFreedomPollard
force-pushed
the
fix-switchenv-env-snapshot
branch
from
September 10, 2026 03:04
e5e28e4 to
6c439c3
Compare
Author
|
Rebased on main. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
switchenv.__exit__restores the environment withos.environ.clear()followed byos.environ.update(self.previous), butself.previousis captured inswitchenv.__init__(devito/parameters.py), so it dates from construction rather than from entry.SwitchDecorator.__call__wraps the decorated function inwith self, so aswitchenvused as a decorator is built once, when the decorator is applied, and entered on every call. The first call therefore rewinds the environment to how it looked at decoration time, deleting every variable set since. Same story for aswitchenvobject kept around and reused as a context manager.On main:
The sibling
switchconfigalready rebuildsself.previousat the top of__enter__. This does the same inswitchenv, taking the snapshot before the device vars are popped so that a single use restores exactly what it restored before. Every existing call site builds the object inline in awithstatement, where construction and entry are adjacent, so none of them change behaviour.Verified on macOS with
DEVITO_ARCH=clang. The newtests/test_tools.py::test_switchenv_reusefails on unmodified main withKeyError: 'TEST_VAR_LATE'and passes with the fix.pytest tests/test_tools.pygives 32 passed andpytest tests/test_environment.pygives 1 passed.isort --check-only .,ruff check --preview,flake8 --builtins=ArgumentError .andtyposare all clean.