Skip to content

Commit 7a412cd

Browse files
committed
MNT: consolidate the no-op logic into a RenderBase method
Be forgiving about renderer instances that do not inherit from RendereBase.
1 parent 5324ada commit 7a412cd

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

lib/matplotlib/backend_bases.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
The base class for the Toolbar class of each interactive backend.
2626
"""
2727

28-
from contextlib import contextmanager
28+
from contextlib import contextmanager, suppress
2929
from enum import Enum, IntEnum
3030
import functools
3131
import importlib
@@ -709,6 +709,23 @@ def stop_filter(self, filter_func):
709709
Currently only supported by the agg renderer.
710710
"""
711711

712+
def _draw_disabled(self):
713+
"""
714+
Context manager to temporary disable drawing.
715+
716+
This is used for getting the drawn size of Artists. This lets us
717+
run the draw process to update any Python state but does not pay the
718+
cost of the draw_XYZ calls on the canvas.
719+
"""
720+
no_ops = {
721+
meth_name: lambda *args, **kwargs: None
722+
for meth_name in dir(RendererBase)
723+
if (meth_name.startswith("draw_")
724+
or meth_name in ["open_group", "close_group"])
725+
}
726+
727+
return _setattr_cm(self, **no_ops)
728+
712729

713730
class GraphicsContextBase:
714731
"""An abstract base class that provides color, line styles, etc."""
@@ -2089,14 +2106,10 @@ def print_figure(
20892106
functools.partial(
20902107
print_method, orientation=orientation)
20912108
)
2092-
no_ops = {
2093-
meth_name: lambda *args, **kwargs: None
2094-
for meth_name in dir(RendererBase)
2095-
if (meth_name.startswith("draw_")
2096-
or meth_name in ["open_group", "close_group"])
2097-
}
2098-
2099-
with _setattr_cm(renderer, **no_ops):
2109+
ctx = (renderer._draw_disabled()
2110+
if hasattr(renderer, '_draw_disabled')
2111+
else suppress())
2112+
with ctx:
21002113
self.figure.draw(renderer)
21012114

21022115
bbox_inches = self.figure.get_tightbbox(

lib/matplotlib/figure.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -2392,9 +2392,7 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
23922392

23932393
from .tight_layout import (
23942394
get_renderer, get_subplotspec_list, get_tight_layout_figure)
2395-
from .cbook import _setattr_cm
2396-
from .backend_bases import RendererBase
2397-
2395+
from contextlib import suppress
23982396
subplotspec_list = get_subplotspec_list(self.axes)
23992397
if None in subplotspec_list:
24002398
cbook._warn_external("This figure includes Axes that are not "
@@ -2403,15 +2401,10 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
24032401

24042402
if renderer is None:
24052403
renderer = get_renderer(self)
2406-
2407-
no_ops = {
2408-
meth_name: lambda *args, **kwargs: None
2409-
for meth_name in dir(RendererBase)
2410-
if (meth_name.startswith("draw_")
2411-
or meth_name in ["open_group", "close_group"])
2412-
}
2413-
2414-
with _setattr_cm(renderer, **no_ops):
2404+
ctx = (renderer._draw_disabled()
2405+
if hasattr(renderer, '_draw_disabled')
2406+
else suppress())
2407+
with ctx:
24152408
kwargs = get_tight_layout_figure(
24162409
self, self.axes, subplotspec_list, renderer,
24172410
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)

0 commit comments

Comments
 (0)