Skip to content

Commit

Permalink
Merge pull request #2070 from QuLogic/more-exitstack
Browse files Browse the repository at this point in the history
Use contextlib.ExitStack in more places
  • Loading branch information
dopplershift authored Aug 22, 2022
2 parents 79947f2 + 33a1062 commit 324325a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
24 changes: 12 additions & 12 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,19 +459,19 @@ def hold_limits(self, hold=True):
context manager exits. Defaults to True.
"""
data_lim = self.dataLim.frozen().get_points()
view_lim = self.viewLim.frozen().get_points()
other = (self.ignore_existing_data_limits,
self.get_autoscalex_on(), self.get_autoscaley_on())
try:
yield
finally:
with contextlib.ExitStack() as stack:
if hold:
self.dataLim.set_points(data_lim)
self.viewLim.set_points(view_lim)
self.ignore_existing_data_limits = other[0]
self.set_autoscalex_on(other[1])
self.set_autoscaley_on(other[2])
stack.callback(self.dataLim.set_points,
self.dataLim.frozen().get_points())
stack.callback(self.viewLim.set_points,
self.viewLim.frozen().get_points())
stack.callback(setattr, self, 'ignore_existing_data_limits',
self.ignore_existing_data_limits)
stack.callback(self.set_autoscalex_on,
self.get_autoscalex_on())
stack.callback(self.set_autoscaley_on,
self.get_autoscaley_on())
yield

def _draw_preprocess(self, renderer):
"""
Expand Down
26 changes: 14 additions & 12 deletions lib/cartopy/tests/io/test_downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ def config_replace(replacement_dict):
dict with the given dictionary. Great for testing purposes!
"""
downloads_orig = cartopy.config['downloaders']
cartopy.config['downloaders'] = replacement_dict
yield
cartopy.config['downloaders'] = downloads_orig
with contextlib.ExitStack() as stack:
stack.callback(cartopy.config.__setitem__, 'downloaders',
cartopy.config['downloaders'])
cartopy.config['downloaders'] = replacement_dict

yield


@pytest.fixture
Expand All @@ -54,16 +56,16 @@ def download_to_temp(tmp_path_factory):
which is automatically cleaned up on exit.
"""
old_downloads_dict = cartopy.config['downloaders'].copy()
old_dir = cartopy.config['data_dir']
with contextlib.ExitStack() as stack:
stack.callback(cartopy.config.__setitem__, 'downloaders',
cartopy.config['downloaders'].copy())
stack.callback(cartopy.config.__setitem__, 'data_dir',
cartopy.config['data_dir'])

tmp_dir = tmp_path_factory.mktemp('cartopy_data')
cartopy.config['data_dir'] = str(tmp_dir)

tmp_dir = tmp_path_factory.mktemp('cartopy_data')
cartopy.config['data_dir'] = str(tmp_dir)
try:
yield tmp_dir
finally:
cartopy.config['downloaders'] = old_downloads_dict
cartopy.config['data_dir'] = old_dir


def test_from_config():
Expand Down
17 changes: 9 additions & 8 deletions lib/cartopy/tests/mpl/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
# This file is part of Cartopy and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
from contextlib import ExitStack

import matplotlib.pyplot as plt
import pytest


@pytest.fixture(autouse=True)
def mpl_test_cleanup(request):
"""Runs tests in a context manager and closes figures after each test"""
try:
"""Run tests in a context manager and close figures after each test."""
with ExitStack() as stack:
# At exit, close all open figures and switch backend back to original.
stack.callback(plt.switch_backend, plt.get_backend())

# Run each test in a context manager so that state does not leak out
orig_backend = plt.get_backend()
plt.switch_backend('Agg')
with plt.rc_context():
yield
finally:
# Closes all open figures and switches backend back to original
plt.switch_backend(orig_backend)
stack.enter_context(plt.rc_context())
yield


def pytest_itemcollected(item):
Expand Down

0 comments on commit 324325a

Please sign in to comment.