A context manager for manipulating a default stack.

def get_context_manager(self, default):
        """A context manager for manipulating a default stack."""
        try:
            self.stack.append(default)
            yield default
        finally:
            if self.enforce_nesting:
                if self.stack[-1] is not default:
                    raise AssertionError(
                        "Nesting violated for default stack of %s objects"
                        % type(default))
                self.stack.pop()
            else:
                self.stack.remove(default)