def ensure_context_manager(): before() with modified_environ(**kwargs): during() after()
import os from pollute import modified_environ assert 'HELLO' not in os.environ assert 'PATH' in os.environ with modified_environ(added={'HELLO': 'WORLD'}, absent=['PATH']): assert os.environ['HELLO'] == 'WORLD' assert 'PATH' not in os.environ assert 'HELLO' not in os.environ assert 'PATH' in os.environ
def environment(added=None, absent=()): """Context manager that modifies the environments variables. Path objects as values are replaced with strings. """ added = stringify_dict(added or {}) return pollute.modified_environ(added, absent)