Created
December 21, 2022 22:58
-
-
Save tsibley/a562078a9bf45fb9d927f00c8769c538 to your computer and use it in GitHub Desktop.
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
| @contextmanager | |
| def modified_environ(**overlay): | |
| """ | |
| XXX FIXME | |
| >>> ... | |
| """ | |
| def _update_environ(d): | |
| for k, v in d.items(): | |
| yield k, os.environ.pop(k, None) | |
| if v is not None: | |
| os.environ[k] = v | |
| # Modify os.environ in place, saving original values for restore later | |
| original = dict(_update_environ(overlay)) | |
| try: | |
| yield | |
| finally: | |
| # Restore os.environ to as it was | |
| _update_environ(original) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment