From 9211537b2f8ea0bcf66ab30e872a59e3a39c738e Mon Sep 17 00:00:00 2001 From: MarcoGorelli Date: Sat, 2 Nov 2019 11:09:58 +0000 Subject: [PATCH 1/3] Make sure that figures are closed when check_figures_equal finishes --- lib/matplotlib/testing/decorators.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index 7e9621cb3b1d..c258574dca11 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -389,18 +389,22 @@ def decorator(func): @pytest.mark.parametrize("ext", extensions) def wrapper(*args, ext, **kwargs): - fig_test = plt.figure("test") - fig_ref = plt.figure("reference") - func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) - test_image_path = result_dir / (func.__name__ + "." + ext) - ref_image_path = result_dir / ( - func.__name__ + "-expected." + ext - ) - fig_test.savefig(test_image_path) - fig_ref.savefig(ref_image_path) - _raise_on_image_difference( - ref_image_path, test_image_path, tol=tol - ) + try: + fig_test = plt.figure("test") + fig_ref = plt.figure("reference") + func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) + test_image_path = result_dir / (func.__name__ + "." + ext) + ref_image_path = result_dir / ( + func.__name__ + "-expected." + ext + ) + fig_test.savefig(test_image_path) + fig_ref.savefig(ref_image_path) + _raise_on_image_difference( + ref_image_path, test_image_path, tol=tol + ) + finally: + plt.close(fig_test) + plt.close(fig_ref) sig = inspect.signature(func) new_sig = sig.replace( From 3d9b7dc0d529a440b6dc93455d55c4a92a95ce33 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 16 Mar 2020 18:43:19 -0400 Subject: [PATCH 2/3] Merge pull request #16693 from tacaswell/tst_better_compare_names TST: use pytest name in naming files for check_figures_equal --- lib/matplotlib/testing/decorators.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index c258574dca11..0a9e21bebcd9 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -5,6 +5,7 @@ import os from pathlib import Path import shutil +import string import sys import unittest import warnings @@ -17,7 +18,7 @@ from matplotlib import ft2font from matplotlib import pyplot as plt from matplotlib import ticker -from . import is_called_from_pytest + from .compare import comparable_formats, compare_images, make_test_filename from .exceptions import ImageComparisonFailure @@ -381,22 +382,23 @@ def test_plot(fig_test, fig_ref): fig_test.subplots().plot([1, 3, 5]) fig_ref.subplots().plot([0, 1, 2], [1, 3, 5]) """ - POSITIONAL_OR_KEYWORD = inspect.Parameter.POSITIONAL_OR_KEYWORD + ALLOWED_CHARS = set(string.digits + string.ascii_letters + '_-[]()') + KEYWORD_ONLY = inspect.Parameter.KEYWORD_ONLY def decorator(func): import pytest _, result_dir = _image_directories(func) @pytest.mark.parametrize("ext", extensions) - def wrapper(*args, ext, **kwargs): + def wrapper(*args, ext, request, **kwargs): + file_name = "".join(c for c in request.node.name + if c in ALLOWED_CHARS) try: fig_test = plt.figure("test") fig_ref = plt.figure("reference") func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) - test_image_path = result_dir / (func.__name__ + "." + ext) - ref_image_path = result_dir / ( - func.__name__ + "-expected." + ext - ) + test_image_path = result_dir / (file_name + "." + ext) + ref_image_path = result_dir / (file_name + "-expected." + ext) fig_test.savefig(test_image_path) fig_ref.savefig(ref_image_path) _raise_on_image_difference( @@ -411,7 +413,10 @@ def wrapper(*args, ext, **kwargs): parameters=([param for param in sig.parameters.values() if param.name not in {"fig_test", "fig_ref"}] - + [inspect.Parameter("ext", POSITIONAL_OR_KEYWORD)]) + + [ + inspect.Parameter("ext", KEYWORD_ONLY), + inspect.Parameter("request", KEYWORD_ONLY), + ]) ) wrapper.__signature__ = new_sig From 5d69b1efd3351d40ee387303b995c23b25d5baa7 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 16 Mar 2020 21:44:02 -0400 Subject: [PATCH 3/3] Fix check_figures_equal for tests that use its fixtures. --- lib/matplotlib/testing/decorators.py | 30 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index 0a9e21bebcd9..7d0941233f80 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -388,9 +388,17 @@ def decorator(func): import pytest _, result_dir = _image_directories(func) + old_sig = inspect.signature(func) @pytest.mark.parametrize("ext", extensions) - def wrapper(*args, ext, request, **kwargs): + def wrapper(*args, **kwargs): + ext = kwargs['ext'] + if 'ext' not in old_sig.parameters: + kwargs.pop('ext') + request = kwargs['request'] + if 'request' not in old_sig.parameters: + kwargs.pop('request') + file_name = "".join(c for c in request.node.name if c in ALLOWED_CHARS) try: @@ -408,16 +416,16 @@ def wrapper(*args, ext, request, **kwargs): plt.close(fig_test) plt.close(fig_ref) - sig = inspect.signature(func) - new_sig = sig.replace( - parameters=([param - for param in sig.parameters.values() - if param.name not in {"fig_test", "fig_ref"}] - + [ - inspect.Parameter("ext", KEYWORD_ONLY), - inspect.Parameter("request", KEYWORD_ONLY), - ]) - ) + parameters = [ + param + for param in old_sig.parameters.values() + if param.name not in {"fig_test", "fig_ref"} + ] + if 'ext' not in old_sig.parameters: + parameters += [inspect.Parameter("ext", KEYWORD_ONLY)] + if 'request' not in old_sig.parameters: + parameters += [inspect.Parameter("request", KEYWORD_ONLY)] + new_sig = old_sig.replace(parameters=parameters) wrapper.__signature__ = new_sig # reach a bit into pytest internals to hoist the marks from pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy