@contextlib.contextmanager
def restore_mapping_entry(mapping, key):
    missing = object()
    old_value = mapping.get(key, missing)
    try:
        yield
    finally:
        if old_value is missing:
            mapping.pop(key, None)
        else:
            mapping[key] = old_value
Context manager that restores a mapping entry to its previous
dataset/ETHPy150Open enthought/traits/traits/etsconfig/tests/test_etsconfig.py/restore_mapping_entry