Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f8370d

Browse files
authored
Merge pull request #9300 from anntzer/simplify-copy-metadata
MNT: Simplify mpl.testing._copy_metadata.
2 parents ca04300 + 5ccea85 commit 2f8370d

File tree

1 file changed

+3
-26
lines changed

1 file changed

+3
-26
lines changed

lib/matplotlib/testing/__init__.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4+
import functools
45
import inspect
56
import warnings
67
from 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-
4124
def _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

Comments
 (0)