@contextmanager
def replace_module_value(module, name, value=None):
    has_prev = hasattr(module, name)
    prev = getattr(module, name, None)
    if value:
        setattr(module, name, value)
    else:
        try:
            delattr(module, name)
        except __HOLE__:
            pass
    yield
    if prev is not None:
        setattr(sys, name, prev)
    if not has_prev:
        try:
            delattr(module, name)
        except AttributeError:
            pass
dataset/ETHPy150Open celery/cell/cell/tests/utils.py/replace_module_value