1+ import contextlib
12from distutils .version import StrictVersion
23import functools
34import inspect
89import unittest
910import warnings
1011
11- # Note - don't import nose up here - import it only as needed in functions.
12- # This allows other functions here to be used by pytest-based testing suites
13- # without requiring nose to be installed.
14-
15-
1612import matplotlib as mpl
1713import matplotlib .style
1814import matplotlib .units
1915import matplotlib .testing
2016from matplotlib import cbook
21- from matplotlib import ticker
22- from matplotlib import pyplot as plt
2317from matplotlib import ft2font
24- from matplotlib . testing . compare import (
25- comparable_formats , compare_images , make_test_filename )
18+ from matplotlib import pyplot as plt
19+ from matplotlib import ticker
2620from . import is_called_from_pytest
21+ from .compare import comparable_formats , compare_images , make_test_filename
2722from .exceptions import ImageComparisonFailure
2823
2924
30- def _do_cleanup (original_units_registry , original_settings ):
31- plt .close ('all' )
32-
33- mpl .rcParams .clear ()
34- mpl .rcParams .update (original_settings )
35- matplotlib .units .registry .clear ()
36- matplotlib .units .registry .update (original_units_registry )
37- warnings .resetwarnings () # reset any warning filters set in tests
38-
39-
40- class CleanupTest (object ):
41- @classmethod
42- def setup_class (cls ):
43- cls .original_units_registry = matplotlib .units .registry .copy ()
44- cls .original_settings = mpl .rcParams .copy ()
45- matplotlib .testing .setup ()
46-
47- @classmethod
48- def teardown_class (cls ):
49- _do_cleanup (cls .original_units_registry ,
50- cls .original_settings )
51-
52- def test (self ):
53- self ._func ()
25+ @contextlib .contextmanager
26+ def _cleanup_cm ():
27+ orig_units_registry = matplotlib .units .registry .copy ()
28+ try :
29+ with warnings .catch_warnings (), matplotlib .rc_context ():
30+ yield
31+ finally :
32+ matplotlib .units .registry .clear ()
33+ matplotlib .units .registry .update (orig_units_registry )
34+ plt .close ("all" )
5435
5536
5637class CleanupTestCase (unittest .TestCase ):
57- ''' A wrapper for unittest.TestCase that includes cleanup operations'''
38+ """ A wrapper for unittest.TestCase that includes cleanup operations."""
5839 @classmethod
5940 def setUpClass (cls ):
60- import matplotlib .units
61- cls .original_units_registry = matplotlib .units .registry .copy ()
62- cls .original_settings = mpl .rcParams .copy ()
41+ cls ._cm = _cleanup_cm ().__enter__ ()
6342
6443 @classmethod
6544 def tearDownClass (cls ):
66- _do_cleanup (cls .original_units_registry ,
67- cls .original_settings )
45+ cls ._cm .__exit__ (None , None , None )
46+
47+
48+ @cbook .deprecated ("3.0" )
49+ class CleanupTest (object ):
50+ setup_class = classmethod (CleanupTestCase .setUpClass .__func__ )
51+ teardown_class = classmethod (CleanupTestCase .tearDownClass .__func__ )
52+
53+ def test (self ):
54+ self ._func ()
6855
6956
7057def cleanup (style = None ):
@@ -78,34 +65,23 @@ def cleanup(style=None):
7865 The name of the style to apply.
7966 """
8067
81- # If cleanup is used without arguments, `style` will be a
82- # callable, and we pass it directly to the wrapper generator. If
83- # cleanup if called with an argument, it is a string naming a
84- # style, and the function will be passed as an argument to what we
85- # return. This is a confusing, but somewhat standard, pattern for
86- # writing a decorator with optional arguments.
68+ # If cleanup is used without arguments, `style` will be a callable, and we
69+ # pass it directly to the wrapper generator. If cleanup if called with an
70+ # argument, it is a string naming a style, and the function will be passed
71+ # as an argument to what we return. This is a confusing, but somewhat
72+ # standard, pattern for writing a decorator with optional arguments.
8773
8874 def make_cleanup (func ):
8975 if inspect .isgeneratorfunction (func ):
9076 @functools .wraps (func )
9177 def wrapped_callable (* args , ** kwargs ):
92- original_units_registry = matplotlib .units .registry .copy ()
93- original_settings = mpl .rcParams .copy ()
94- matplotlib .style .use (style )
95- try :
78+ with _cleanup_cm (), matplotlib .style .context (style ):
9679 yield from func (* args , ** kwargs )
97- finally :
98- _do_cleanup (original_units_registry , original_settings )
9980 else :
10081 @functools .wraps (func )
10182 def wrapped_callable (* args , ** kwargs ):
102- original_units_registry = matplotlib .units .registry .copy ()
103- original_settings = mpl .rcParams .copy ()
104- matplotlib .style .use (style )
105- try :
83+ with _cleanup_cm (), matplotlib .style .context (style ):
10684 func (* args , ** kwargs )
107- finally :
108- _do_cleanup (original_units_registry , original_settings )
10985
11086 return wrapped_callable
11187
0 commit comments