Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created December 21, 2022 22:58
Show Gist options
  • Select an option

  • Save tsibley/a562078a9bf45fb9d927f00c8769c538 to your computer and use it in GitHub Desktop.

Select an option

Save tsibley/a562078a9bf45fb9d927f00c8769c538 to your computer and use it in GitHub Desktop.
@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