def stacked(stream):
    """Save and restore stream context when used with the ``with`` keyword."""
    stream.push_state()
    try:
        yield
    finally:
        stream.pop_state()
Save and restore stream context when used with the ``with`` keyword.
weasyprint/draw/stack.py