diff --git a/skimage/_shared/tests/test_utils.py b/skimage/_shared/tests/test_utils.py new file mode 100644 index 00000000..92d0adac --- /dev/null +++ b/skimage/_shared/tests/test_utils.py @@ -0,0 +1,21 @@ +from skimage._shared.utils import copy_func +import numpy.testing as npt + + +def test_copyfunc(): + def foo(a): + return a + + bar = copy_func(foo, name='bar') + other = copy_func(foo) + + npt.assert_equal(bar.__name__, 'bar') + npt.assert_equal(other.__name__, 'foo') + + other.__name__ = 'other' + + npt.assert_equal(foo.__name__, 'foo') + + +if __name__ == "__main__": + npt.run_module_suite() diff --git a/skimage/_shared/utils.py b/skimage/_shared/utils.py index 6b0607e9..cc688fd7 100644 --- a/skimage/_shared/utils.py +++ b/skimage/_shared/utils.py @@ -2,6 +2,7 @@ import warnings import functools import sys import numpy as np +import types import six @@ -174,3 +175,19 @@ def _mode_deprecations(mode): "removed in a future release.")) mode = 'edge' return mode + + +def copy_func(f, name=None): + """Create a copy of a function. + + Parameters + ---------- + f : function + Function to copy. + name : str, optional + Name of new function. + + """ + return types.FunctionType(six.get_function_code(f), + six.get_function_globals(f), name or f.__name__, + six.get_function_defaults(f), six.get_function_closure(f))