From f35177ebe6e956a028838bc83d97304b1bacf1a8 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 6 Jan 2016 19:10:37 -0800 Subject: [PATCH 1/4] Support generative tests in @cleanup. --- lib/matplotlib/testing/decorators.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index 06877c83bf8b..acf5b9c1a225 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -5,6 +5,7 @@ import functools import gc +import inspect import os import sys import shutil @@ -135,7 +136,11 @@ def wrapped_function(*args, **kwargs): original_settings = mpl.rcParams.copy() matplotlib.style.use(style) try: - func(*args, **kwargs) + if inspect.isgeneratorfunction(func): + for yielded in func(*args, **kwargs): + yield yielded + else: + func(*args, **kwargs) finally: _do_cleanup(original_units_registry, original_settings) From 608855b80f850aa067b4d9caffc6dfafcd5b1c54 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 7 Jan 2016 11:00:38 -0800 Subject: [PATCH 2/4] Temporary mark test_remove_shared_axes as failing. --- lib/matplotlib/tests/test_axes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index e3bfdaa8ce26..4fee24f76703 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -19,7 +19,8 @@ import warnings import matplotlib -from matplotlib.testing.decorators import image_comparison, cleanup +from matplotlib.testing.decorators import ( + cleaup, image_comparison, knownfailureif) import matplotlib.pyplot as plt import matplotlib.markers as mmarkers from numpy.testing import assert_array_equal @@ -4238,6 +4239,7 @@ def test_auto_numticks(): @cleanup +@knownfailureif(True) def test_remove_shared_axes(): def _helper_x(ax): ax2 = ax.twinx() From e8bf90a6a6324e3be7ee294e50d83df67105479e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 7 Jan 2016 21:16:54 -0500 Subject: [PATCH 3/4] TST: fix broken test One too many assertions Closes #1 --- lib/matplotlib/tests/test_axes.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 4fee24f76703..f89b889550bc 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -19,8 +19,7 @@ import warnings import matplotlib -from matplotlib.testing.decorators import ( - cleaup, image_comparison, knownfailureif) +from matplotlib.testing.decorators import image_comparison, cleanup import matplotlib.pyplot as plt import matplotlib.markers as mmarkers from numpy.testing import assert_array_equal @@ -4239,7 +4238,6 @@ def test_auto_numticks(): @cleanup -@knownfailureif(True) def test_remove_shared_axes(): def _helper_x(ax): ax2 = ax.twinx() @@ -4280,7 +4278,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 From b441be2a3ea454673a1c6adcef5fbf314e31623c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 8 Jan 2016 08:52:15 -0800 Subject: [PATCH 4/4] cleanup should not always create a generator. --- lib/matplotlib/testing/decorators.py | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index acf5b9c1a225..3f2f0ce7c391 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -130,22 +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: - if inspect.isgeneratorfunction(func): + 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 - else: + 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) + finally: + _do_cleanup(original_units_registry, + original_settings) - return wrapped_function + return wrapped_callable if isinstance(style, six.string_types): return make_cleanup