def METHOD_NAME(self, config):
    # Avoid an unnecessary file write if possible
    if config != self.config:
        try:
            self.write_config(config)
            yield
        finally:
            self.write_config()
    else:
        yield