Skip to content

Commit d7ea249

Browse files
committed
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.)
1 parent 679ca17 commit d7ea249

File tree

4 files changed

+6
-22
lines changed

4 files changed

+6
-22
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"""
2727

2828
from collections import namedtuple
29-
from contextlib import contextmanager, suppress
29+
from contextlib import contextmanager, nullcontext
3030
from enum import Enum, IntEnum
3131
import functools
3232
import importlib
@@ -2286,10 +2286,7 @@ def print_figure(
22862286
functools.partial(
22872287
print_method, orientation=orientation)
22882288
)
2289-
ctx = (renderer._draw_disabled()
2290-
if hasattr(renderer, '_draw_disabled')
2291-
else suppress())
2292-
with ctx:
2289+
with getattr(renderer, "_draw_disabled", nullcontext)():
22932290
self.figure.draw(renderer)
22942291

22952292
if bbox_inches:

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,10 @@ def to_filehandle(fname, flag='r', return_opened=False, encoding=None):
475475
return fh
476476

477477

478-
@contextlib.contextmanager
479478
def open_file_cm(path_or_file, mode="r", encoding=None):
480479
r"""Pass through file objects and context-manage path-likes."""
481480
fh, opened = to_filehandle(path_or_file, mode, True, encoding)
482-
if opened:
483-
with fh:
484-
yield fh
485-
else:
486-
yield fh
481+
return fh if opened else contextlib.nullcontext(fh)
487482

488483

489484
def is_scalar_or_string(val):

lib/matplotlib/figure.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,21 +3089,16 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
30893089
.Figure.set_tight_layout
30903090
.pyplot.tight_layout
30913091
"""
3092-
3092+
from contextlib import nullcontext
30933093
from .tight_layout import (
30943094
get_subplotspec_list, get_tight_layout_figure)
3095-
from contextlib import suppress
30963095
subplotspec_list = get_subplotspec_list(self.axes)
30973096
if None in subplotspec_list:
30983097
_api.warn_external("This figure includes Axes that are not "
30993098
"compatible with tight_layout, so results "
31003099
"might be incorrect.")
3101-
31023100
renderer = _get_renderer(self)
3103-
ctx = (renderer._draw_disabled()
3104-
if hasattr(renderer, '_draw_disabled')
3105-
else suppress())
3106-
with ctx:
3101+
with getattr(renderer, "_draw_disabled", nullcontext)():
31073102
kwargs = get_tight_layout_figure(
31083103
self, self.axes, subplotspec_list, renderer,
31093104
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
from collections import namedtuple
2+
from contextlib import nullcontext
23
import datetime
34
from decimal import Decimal
45
import io
56
from itertools import product
67
import platform
78
from types import SimpleNamespace
8-
try:
9-
from contextlib import nullcontext
10-
except ImportError:
11-
from contextlib import ExitStack as nullcontext # Py3.6.
129

1310
import dateutil.tz
1411

0 commit comments

Comments
 (0)