Created
July 12, 2019 20:58
-
-
Save hngkr/8bb63bed26b2047c8dbfb75d554e0add to your computer and use it in GitHub Desktop.
setenviron contextmanager source code
This file contains 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
import contextlib | |
import os | |
@contextlib.contextmanager | |
def setenviron(envdict=None, **mapping): | |
"""``with`` context to temporarily modify the environment variables""" | |
_environ = os.environ.copy() | |
if envdict: | |
for key, value in envdict.items(): | |
os.environ[key] = str(value) | |
for key, value in mapping.items(): | |
os.environ[key] = str(value) | |
try: | |
yield | |
finally: | |
os.environ.clear() | |
os.environ.update(_environ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment