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