11from __future__ import (absolute_import , division , print_function ,
22 unicode_literals )
33
4+ import functools
45import inspect
56import warnings
67from contextlib import contextmanager
@@ -20,34 +21,10 @@ def is_called_from_pytest():
2021 return getattr (matplotlib , '_called_from_pytest' , False )
2122
2223
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-
4124def _copy_metadata (src_func , tgt_func ):
4225 """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.
5128 return tgt_func
5229
5330
0 commit comments