Commit 6c439c3
committed
misc: Snapshot the environment when a switchenv is entered
`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.1 parent 33c3b12 commit 6c439c3
2 files changed
Lines changed: 35 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
298 | | - | |
299 | 298 | | |
| 299 | + | |
300 | 300 | | |
301 | 301 | | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
302 | 306 | | |
303 | 307 | | |
304 | 308 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
344 | 344 | | |
345 | 345 | | |
346 | 346 | | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
0 commit comments