@contextmanager<EOL>def ensure_safe_environment_variables():
saved_environ = dict(os.environ)<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>os.environ.clear()<EOL>os.environ.update(saved_environ)<EOL><DEDENT>
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.