1
1
from distutils .version import StrictVersion
2
2
import functools
3
- import inspect
4
3
import os
5
4
from pathlib import Path
6
5
import shutil
23
22
from matplotlib import ft2font
24
23
from matplotlib .testing .compare import (
25
24
comparable_formats , compare_images , make_test_filename )
26
- from . import _copy_metadata , is_called_from_pytest
25
+ from . import is_called_from_pytest
27
26
from .exceptions import ImageComparisonFailure
28
27
29
28
@@ -250,9 +249,9 @@ def copy_baseline(self, baseline, extension):
250
249
if os .path .exists (orig_expected_fname ):
251
250
shutil .copyfile (orig_expected_fname , expected_fname )
252
251
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 ))
256
255
raise ImageComparisonFailure (reason )
257
256
return expected_fname
258
257
@@ -327,11 +326,12 @@ def __call__(self, func):
327
326
self .delayed_init (func )
328
327
import nose .tools
329
328
329
+ @functools .wraps (func )
330
330
@nose .tools .with_setup (self .setup , self .teardown )
331
331
def runner_wrapper ():
332
332
yield from self .nose_runner ()
333
333
334
- return _copy_metadata ( func , runner_wrapper )
334
+ return runner_wrapper
335
335
336
336
337
337
def _pytest_image_comparison (baseline_images , extensions , tol ,
@@ -350,6 +350,7 @@ def _pytest_image_comparison(baseline_images, extensions, tol,
350
350
extensions = map (_mark_xfail_if_format_is_uncomparable , extensions )
351
351
352
352
def decorator (func ):
353
+ @functools .wraps (func )
353
354
# Parameter indirection; see docstring above and comment below.
354
355
@pytest .mark .usefixtures ('mpl_image_comparison_parameters' )
355
356
@pytest .mark .parametrize ('extension' , extensions )
@@ -379,8 +380,7 @@ def wrapper(*args, **kwargs):
379
380
for idx , baseline in enumerate (baseline_images ):
380
381
img .compare (idx , baseline , extension )
381
382
382
- wrapper .__wrapped__ = func # For Python 2.7.
383
- return _copy_metadata (func , wrapper )
383
+ return wrapper
384
384
385
385
return decorator
386
386
@@ -408,7 +408,7 @@ def image_comparison(baseline_images, extensions=None, tol=0,
408
408
extensions : [ None | list ]
409
409
410
410
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']`` .
412
412
413
413
tol : float, optional, default: 0
414
414
The RMS threshold above which the test is considered failed.
@@ -464,10 +464,10 @@ def _image_directories(func):
464
464
# FIXME: this won't work for nested packages in matplotlib.tests
465
465
warnings .warn (
466
466
'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
470
469
else :
470
+ module_path = Path (sys .modules [func .__module__ ].__file__ )
471
471
mods = module_name .split ('.' )
472
472
if len (mods ) >= 3 :
473
473
mods .pop (0 )
@@ -486,50 +486,29 @@ def _image_directories(func):
486
486
"file (can be empty)." .format (module_name ))
487
487
subdir = os .path .join (* mods )
488
488
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 )
509
492
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 )
515
494
516
495
517
496
def 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
+
520
498
def switch_backend_decorator (func ):
499
+
521
500
@functools .wraps (func )
522
501
def backend_switcher (* args , ** kwargs ):
523
502
try :
524
503
prev_backend = mpl .get_backend ()
525
504
matplotlib .testing .setup ()
526
505
plt .switch_backend (backend )
527
- result = func (* args , ** kwargs )
506
+ return func (* args , ** kwargs )
528
507
finally :
529
508
plt .switch_backend (prev_backend )
530
- return result
531
509
532
- return _copy_metadata (func , backend_switcher )
510
+ return backend_switcher
511
+
533
512
return switch_backend_decorator
534
513
535
514
0 commit comments