11from distutils .version import StrictVersion
22import functools
3- import inspect
43import os
54from pathlib import Path
65import shutil
2322from matplotlib import ft2font
2423from matplotlib .testing .compare import (
2524 comparable_formats , compare_images , make_test_filename )
26- from . import _copy_metadata , is_called_from_pytest
25+ from . import is_called_from_pytest
2726from .exceptions import ImageComparisonFailure
2827
2928
@@ -250,9 +249,9 @@ def copy_baseline(self, baseline, extension):
250249 if os .path .exists (orig_expected_fname ):
251250 shutil .copyfile (orig_expected_fname , expected_fname )
252251 else :
253- reason = ("Do not have baseline image {0 } because this "
254- "file does not exist: {1 }" .format (expected_fname ,
255- orig_expected_fname ))
252+ reason = ("Do not have baseline image {} because this "
253+ "file does not exist: {}" .format (expected_fname ,
254+ orig_expected_fname ))
256255 raise ImageComparisonFailure (reason )
257256 return expected_fname
258257
@@ -327,11 +326,12 @@ def __call__(self, func):
327326 self .delayed_init (func )
328327 import nose .tools
329328
329+ @functools .wraps (func )
330330 @nose .tools .with_setup (self .setup , self .teardown )
331331 def runner_wrapper ():
332332 yield from self .nose_runner ()
333333
334- return _copy_metadata ( func , runner_wrapper )
334+ return runner_wrapper
335335
336336
337337def _pytest_image_comparison (baseline_images , extensions , tol ,
@@ -350,6 +350,7 @@ def _pytest_image_comparison(baseline_images, extensions, tol,
350350 extensions = map (_mark_xfail_if_format_is_uncomparable , extensions )
351351
352352 def decorator (func ):
353+ @functools .wraps (func )
353354 # Parameter indirection; see docstring above and comment below.
354355 @pytest .mark .usefixtures ('mpl_image_comparison_parameters' )
355356 @pytest .mark .parametrize ('extension' , extensions )
@@ -379,8 +380,7 @@ def wrapper(*args, **kwargs):
379380 for idx , baseline in enumerate (baseline_images ):
380381 img .compare (idx , baseline , extension )
381382
382- wrapper .__wrapped__ = func # For Python 2.7.
383- return _copy_metadata (func , wrapper )
383+ return wrapper
384384
385385 return decorator
386386
@@ -408,7 +408,7 @@ def image_comparison(baseline_images, extensions=None, tol=0,
408408 extensions : [ None | list ]
409409
410410 If None, defaults to all supported extensions.
411- Otherwise, a list of extensions to test. For example ['png','pdf'].
411+ Otherwise, a list of extensions to test, e.g. `` ['png', 'pdf']`` .
412412
413413 tol : float, optional, default: 0
414414 The RMS threshold above which the test is considered failed.
@@ -464,10 +464,10 @@ def _image_directories(func):
464464 # FIXME: this won't work for nested packages in matplotlib.tests
465465 warnings .warn (
466466 'Test module run as script. Guessing baseline image locations.' )
467- script_name = sys .argv [0 ]
468- basedir = os .path .abspath (os .path .dirname (script_name ))
469- subdir = os .path .splitext (os .path .split (script_name )[1 ])[0 ]
467+ module_path = Path (sys .argv [0 ]).resolve ()
468+ subdir = module_path .stem
470469 else :
470+ module_path = Path (sys .modules [func .__module__ ].__file__ )
471471 mods = module_name .split ('.' )
472472 if len (mods ) >= 3 :
473473 mods .pop (0 )
@@ -486,50 +486,29 @@ def _image_directories(func):
486486 "file (can be empty)." .format (module_name ))
487487 subdir = os .path .join (* mods )
488488
489- import imp
490- def find_dotted_module (module_name , path = None ):
491- """A version of imp which can handle dots in the module name.
492- As for imp.find_module(), the return value is a 3-element
493- tuple (file, pathname, description)."""
494- res = None
495- for sub_mod in module_name .split ('.' ):
496- try :
497- res = file , path , _ = imp .find_module (sub_mod , path )
498- path = [path ]
499- if file is not None :
500- file .close ()
501- except ImportError :
502- # assume namespace package
503- path = list (sys .modules [sub_mod ].__path__ )
504- res = None , path , None
505- return res
506-
507- mod_file = find_dotted_module (func .__module__ )[1 ]
508- basedir = os .path .dirname (mod_file )
489+ baseline_dir = module_path .parent / 'baseline_images' / subdir
490+ result_dir = Path ().resolve () / 'result_images' / subdir
491+ result_dir .mkdir (parents = True , exist_ok = True )
509492
510- baseline_dir = os .path .join (basedir , 'baseline_images' , subdir )
511- result_dir = os .path .abspath (os .path .join ('result_images' , subdir ))
512- Path (result_dir ).mkdir (parents = True , exist_ok = True )
513-
514- return baseline_dir , result_dir
493+ return str (baseline_dir ), str (result_dir )
515494
516495
517496def switch_backend (backend ):
518- # Local import to avoid a hard nose dependency and only incur the
519- # import time overhead at actual test-time.
497+
520498 def switch_backend_decorator (func ):
499+
521500 @functools .wraps (func )
522501 def backend_switcher (* args , ** kwargs ):
523502 try :
524503 prev_backend = mpl .get_backend ()
525504 matplotlib .testing .setup ()
526505 plt .switch_backend (backend )
527- result = func (* args , ** kwargs )
506+ return func (* args , ** kwargs )
528507 finally :
529508 plt .switch_backend (prev_backend )
530- return result
531509
532- return _copy_metadata (func , backend_switcher )
510+ return backend_switcher
511+
533512 return switch_backend_decorator
534513
535514
0 commit comments