@@ -0,0 +1,1794 @@
+import contextlib%0Aimport os%0A%0A@contextlib.contextmanager%0Adef temporary_env(newenv):%0A    %22%22%22Completely replace the environment variables with the specified dict.%0A    %0A    Use as a context manager::%0A    %0A        with temporary_env(%7B'PATH': my_path%7D):%0A            ...%0A    %22%22%22%0A    orig_env = os.environ.copy()%0A    os.environ.clear()%0A    os.environ.update(newenv)%0A    try:%0A        yield%0A    finally:%0A        os.environ.clear()%0A        os.environ.update(orig_env)    %0A%0A@contextlib.contextmanager%0Adef modified_env(changes, snapshot=True):%0A    %22%22%22Temporarily modify environment variables.%0A    %0A    Specify the changes as a dictionary mapping names to new values, using%0A    None as the value for names that should be deleted.%0A    %0A    Example use::%0A    %0A        with modified_env(%7B'SHELL': 'bash', 'PYTHONPATH': None%7D):%0A            ...%0A    %0A    When the context exits, there are two possible ways to restore the%0A    environment. If *snapshot* is True, the default, it will reset the whole%0A    environment to its state when the context was entered. If *snapshot* is%0A    False, it will restore only the specific variables it modified, leaving%0A    any changes made to other environment variables in the context.%0A    %22%22%22%0A    def update_del(changes):%0A        for k, v in changes:%0A            if v is None:%0A                os.environ.pop(k)%0A            else:%0A                os.environ%5Bk%5D = v%0A    %0A    if snapshot:%0A        saved_variables = os.environ.copy()%0A    else:%0A        saved_variables = %7B%7D%0A        for k,v in changes.items():%0A            saved_variables%5Bk%5D = os.environ.get(k, None)%0A%0A    update_del(changes)%0A    %0A    try:%0A        yield%0A    finally:%0A        if snapshot:%0A            os.environ.clear()%0A            os.environ.update(saved_variables)%0A        else:%0A            update_del(saved_variables)%0A

Add functions for manipulating environment