1
1
from __future__ import (absolute_import , division , print_function ,
2
2
unicode_literals )
3
3
4
+ import functools
4
5
import inspect
5
6
import warnings
6
7
from contextlib import contextmanager
@@ -20,34 +21,10 @@ def is_called_from_pytest():
20
21
return getattr (matplotlib , '_called_from_pytest' , False )
21
22
22
23
23
- # stolen from pytest
24
- def _getrawcode (obj , trycall = True ):
25
- """Return code object for given function."""
26
- try :
27
- return obj .__code__
28
- except AttributeError :
29
- obj = getattr (obj , 'im_func' , obj )
30
- obj = getattr (obj , 'func_code' , obj )
31
- obj = getattr (obj , 'f_code' , obj )
32
- obj = getattr (obj , '__code__' , obj )
33
- if trycall and not hasattr (obj , 'co_firstlineno' ):
34
- if hasattr (obj , '__call__' ) and not inspect .isclass (obj ):
35
- x = getrawcode (obj .__call__ , trycall = False )
36
- if hasattr (x , 'co_firstlineno' ):
37
- return x
38
- return obj
39
-
40
-
41
24
def _copy_metadata (src_func , tgt_func ):
42
25
"""Replicates metadata of the function. Returns target function."""
43
- tgt_func .__dict__ .update (src_func .__dict__ )
44
- tgt_func .__doc__ = src_func .__doc__
45
- tgt_func .__module__ = src_func .__module__
46
- tgt_func .__name__ = src_func .__name__
47
- if hasattr (src_func , '__qualname__' ):
48
- tgt_func .__qualname__ = src_func .__qualname__
49
- if not hasattr (tgt_func , 'compat_co_firstlineno' ):
50
- tgt_func .compat_co_firstlineno = _getrawcode (src_func ).co_firstlineno
26
+ functools .update_wrapper (tgt_func , src_func )
27
+ tgt_func .__wrapped__ = src_func # Python2 compatibility.
51
28
return tgt_func
52
29
53
30
0 commit comments