1
+ import contextlib
1
2
from distutils .version import StrictVersion
2
3
import functools
3
4
import inspect
8
9
import unittest
9
10
import warnings
10
11
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
-
16
12
import matplotlib as mpl
17
13
import matplotlib .style
18
14
import matplotlib .units
19
15
import matplotlib .testing
20
16
from matplotlib import cbook
21
- from matplotlib import ticker
22
- from matplotlib import pyplot as plt
23
17
from 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
26
20
from . import is_called_from_pytest
21
+ from .compare import comparable_formats , compare_images , make_test_filename
27
22
from .exceptions import ImageComparisonFailure
28
23
29
24
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" )
54
35
55
36
56
37
class CleanupTestCase (unittest .TestCase ):
57
- ''' A wrapper for unittest.TestCase that includes cleanup operations'''
38
+ """ A wrapper for unittest.TestCase that includes cleanup operations."""
58
39
@classmethod
59
40
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__ ()
63
42
64
43
@classmethod
65
44
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 ()
68
55
69
56
70
57
def cleanup (style = None ):
@@ -78,34 +65,23 @@ def cleanup(style=None):
78
65
The name of the style to apply.
79
66
"""
80
67
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.
87
73
88
74
def make_cleanup (func ):
89
75
if inspect .isgeneratorfunction (func ):
90
76
@functools .wraps (func )
91
77
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 ):
96
79
yield from func (* args , ** kwargs )
97
- finally :
98
- _do_cleanup (original_units_registry , original_settings )
99
80
else :
100
81
@functools .wraps (func )
101
82
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 ):
106
84
func (* args , ** kwargs )
107
- finally :
108
- _do_cleanup (original_units_registry , original_settings )
109
85
110
86
return wrapped_callable
111
87
0 commit comments