|
5 | 5 |
|
6 | 6 | import functools
|
7 | 7 | import gc
|
| 8 | +import inspect |
8 | 9 | import os
|
9 | 10 | import sys
|
10 | 11 | import shutil
|
@@ -129,18 +130,31 @@ def cleanup(style=None):
|
129 | 130 | # writing a decorator with optional arguments.
|
130 | 131 |
|
131 | 132 | def make_cleanup(func):
|
132 |
| - @functools.wraps(func) |
133 |
| - def wrapped_function(*args, **kwargs): |
134 |
| - original_units_registry = matplotlib.units.registry.copy() |
135 |
| - original_settings = mpl.rcParams.copy() |
136 |
| - matplotlib.style.use(style) |
137 |
| - try: |
138 |
| - func(*args, **kwargs) |
139 |
| - finally: |
140 |
| - _do_cleanup(original_units_registry, |
141 |
| - original_settings) |
| 133 | + if inspect.isgenerator(func): |
| 134 | + @functools.wraps(func) |
| 135 | + def wrapped_callable(*args, **kwargs): |
| 136 | + original_units_registry = matplotlib.units.registry.copy() |
| 137 | + original_settings = mpl.rcParams.copy() |
| 138 | + matplotlib.style.use(style) |
| 139 | + try: |
| 140 | + for yielded in func(*args, **kwargs): |
| 141 | + yield yielded |
| 142 | + finally: |
| 143 | + _do_cleanup(original_units_registry, |
| 144 | + original_settings) |
| 145 | + else: |
| 146 | + @functools.wraps(func) |
| 147 | + def wrapped_callable(*args, **kwargs): |
| 148 | + original_units_registry = matplotlib.units.registry.copy() |
| 149 | + original_settings = mpl.rcParams.copy() |
| 150 | + matplotlib.style.use(style) |
| 151 | + try: |
| 152 | + func(*args, **kwargs) |
| 153 | + finally: |
| 154 | + _do_cleanup(original_units_registry, |
| 155 | + original_settings) |
142 | 156 |
|
143 |
| - return wrapped_function |
| 157 | + return wrapped_callable |
144 | 158 |
|
145 | 159 | if isinstance(style, six.string_types):
|
146 | 160 | return make_cleanup
|
|
0 commit comments