Get a context manager to safely set environment variables All changes will be undone on close, hence environment variables set within this contextmanager will neither persist nor change global state.
()
| 57 | |
| 58 | @contextmanager |
| 59 | def ensure_safe_environment_variables(): |
| 60 | """ |
| 61 | Get a context manager to safely set environment variables |
| 62 | All changes will be undone on close, hence environment variables set |
| 63 | within this contextmanager will neither persist nor change global state. |
| 64 | """ |
| 65 | saved_environ = dict(os.environ) |
| 66 | try: |
| 67 | yield |
| 68 | finally: |
| 69 | os.environ.clear() |
| 70 | os.environ.update(saved_environ) |
| 71 | |
| 72 | |
| 73 | @pytest.fixture |