'Context manager that clears the environments (caches and fields to
recompute) upon exception.'

@contextmanager
def clear_upon_failure(self):

    try:
        (yield)
    except Exception:
        self.clear()
        raise
