Skip to content

Simplify mpl.testing._copy_metadata. #9300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions lib/matplotlib/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import functools
import inspect
import warnings
from contextlib import contextmanager
Expand All @@ -20,34 +21,10 @@ def is_called_from_pytest():
return getattr(matplotlib, '_called_from_pytest', False)


# stolen from pytest
def _getrawcode(obj, trycall=True):
"""Return code object for given function."""
try:
return obj.__code__
except AttributeError:
obj = getattr(obj, 'im_func', obj)
obj = getattr(obj, 'func_code', obj)
obj = getattr(obj, 'f_code', obj)
obj = getattr(obj, '__code__', obj)
if trycall and not hasattr(obj, 'co_firstlineno'):
if hasattr(obj, '__call__') and not inspect.isclass(obj):
x = getrawcode(obj.__call__, trycall=False)
if hasattr(x, 'co_firstlineno'):
return x
return obj


def _copy_metadata(src_func, tgt_func):
"""Replicates metadata of the function. Returns target function."""
tgt_func.__dict__.update(src_func.__dict__)
tgt_func.__doc__ = src_func.__doc__
tgt_func.__module__ = src_func.__module__
tgt_func.__name__ = src_func.__name__
if hasattr(src_func, '__qualname__'):
tgt_func.__qualname__ = src_func.__qualname__
if not hasattr(tgt_func, 'compat_co_firstlineno'):
tgt_func.compat_co_firstlineno = _getrawcode(src_func).co_firstlineno
functools.update_wrapper(tgt_func, src_func)
tgt_func.__wrapped__ = src_func # Python2 compatibility.
return tgt_func


Expand Down