From d7ea249789024072385551e0c5d235b12e33ee05 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 28 Apr 2021 13:59:33 +0200 Subject: [PATCH] Use nullcontext more as do-nothing context manager. (The implementation of open_file_cm is very close to the second example in the stdlib docs for nullcontext.) --- lib/matplotlib/backend_bases.py | 7 ++----- lib/matplotlib/cbook/__init__.py | 7 +------ lib/matplotlib/figure.py | 9 ++------- lib/matplotlib/tests/test_axes.py | 5 +---- 4 files changed, 6 insertions(+), 22 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index c7cb22b09f6c..477c3e9ebd03 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -26,7 +26,7 @@ """ from collections import namedtuple -from contextlib import contextmanager, suppress +from contextlib import contextmanager, nullcontext from enum import Enum, IntEnum import functools import importlib @@ -2286,10 +2286,7 @@ def print_figure( functools.partial( print_method, orientation=orientation) ) - ctx = (renderer._draw_disabled() - if hasattr(renderer, '_draw_disabled') - else suppress()) - with ctx: + with getattr(renderer, "_draw_disabled", nullcontext)(): self.figure.draw(renderer) if bbox_inches: diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index f69c766be2cd..034c6ceb99ca 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -475,15 +475,10 @@ def to_filehandle(fname, flag='r', return_opened=False, encoding=None): return fh -@contextlib.contextmanager def open_file_cm(path_or_file, mode="r", encoding=None): r"""Pass through file objects and context-manage path-likes.""" fh, opened = to_filehandle(path_or_file, mode, True, encoding) - if opened: - with fh: - yield fh - else: - yield fh + return fh if opened else contextlib.nullcontext(fh) def is_scalar_or_string(val): diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index a0c7c4e44fe7..fef0740305f8 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -3089,21 +3089,16 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None): .Figure.set_tight_layout .pyplot.tight_layout """ - + from contextlib import nullcontext from .tight_layout import ( get_subplotspec_list, get_tight_layout_figure) - from contextlib import suppress subplotspec_list = get_subplotspec_list(self.axes) if None in subplotspec_list: _api.warn_external("This figure includes Axes that are not " "compatible with tight_layout, so results " "might be incorrect.") - renderer = _get_renderer(self) - ctx = (renderer._draw_disabled() - if hasattr(renderer, '_draw_disabled') - else suppress()) - with ctx: + with getattr(renderer, "_draw_disabled", nullcontext)(): kwargs = get_tight_layout_figure( self, self.axes, subplotspec_list, renderer, pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index af4f50117694..e6e2b6008c18 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1,14 +1,11 @@ from collections import namedtuple +from contextlib import nullcontext import datetime from decimal import Decimal import io from itertools import product import platform from types import SimpleNamespace -try: - from contextlib import nullcontext -except ImportError: - from contextlib import ExitStack as nullcontext # Py3.6. import dateutil.tz