@contextlib.contextmanager<EOL>def modified_environ(*remove, **update):
env = os.environ<EOL>update = update or {}<EOL>remove = remove or []<EOL>stomped = (set(update) | set(remove)) & set(env)<EOL>update_after = {k: env[k] for k in stomped}<EOL>remove_after = frozenset(k for k in update if k not in env)<EOL>try:<EOL><INDENT>env.update(update)<EOL>[env.pop(k, None) for k in remove]<EOL>yield<EOL><DEDENT>finally:<EOL><INDENT>env.update(update_after)<EOL>[env.pop(k) for k in remove_after]<EOL><DEDENT>
Temporarily updates the ``os.environ`` dictionary in-place.

The ``os.environ`` dictionary is updated in-place so that the modification
is sure to work in all situations.

:param remove: Environment variables to remove.
:param update: Dictionary of environment variables
and values to add/update.