Skip to content

Commit

Permalink
Merge pull request matplotlib#5809 from anntzer/cleanup-generative-tests
Browse files Browse the repository at this point in the history
Support generative tests in @cleanup.
  • Loading branch information
mdboom authored and bearstrong committed Apr 1, 2016
1 parent 7e209a2 commit 1382cee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
36 changes: 25 additions & 11 deletions lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import functools
import gc
import inspect
import os
import sys
import shutil
Expand Down Expand Up @@ -129,18 +130,31 @@ def cleanup(style=None):
# writing a decorator with optional arguments.

def make_cleanup(func):
@functools.wraps(func)
def wrapped_function(*args, **kwargs):
original_units_registry = matplotlib.units.registry.copy()
original_settings = mpl.rcParams.copy()
matplotlib.style.use(style)
try:
func(*args, **kwargs)
finally:
_do_cleanup(original_units_registry,
original_settings)
if inspect.isgenerator(func):
@functools.wraps(func)
def wrapped_callable(*args, **kwargs):
original_units_registry = matplotlib.units.registry.copy()
original_settings = mpl.rcParams.copy()
matplotlib.style.use(style)
try:
for yielded in func(*args, **kwargs):
yield yielded
finally:
_do_cleanup(original_units_registry,
original_settings)
else:
@functools.wraps(func)
def wrapped_callable(*args, **kwargs):
original_units_registry = matplotlib.units.registry.copy()
original_settings = mpl.rcParams.copy()
matplotlib.style.use(style)
try:
func(*args, **kwargs)
finally:
_do_cleanup(original_units_registry,
original_settings)

return wrapped_function
return wrapped_callable

if isinstance(style, six.string_types):
return make_cleanup
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4282,7 +4282,7 @@ def _helper_y(ax):
orig_xlim = ax_lst[0][1].get_xlim()
ax.remove()
ax.set_xlim(0, 5)
assert assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)
assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)


@cleanup
Expand Down

0 comments on commit 1382cee

Please sign in to comment.