diff --git a/skimage/external/tifffile/tifffile_local.py b/skimage/external/tifffile/tifffile_local.py index b5a67ccf..4ddd6763 100644 --- a/skimage/external/tifffile/tifffile_local.py +++ b/skimage/external/tifffile/tifffile_local.py @@ -3081,20 +3081,40 @@ def imagej_description(description): def _replace_by(module_function, package=None, warn=False): - """Try replace decorated function by module.function.""" - try: - from importlib import import_module - except ImportError: - warnings.warn('could not import module importlib') - return lambda func: func + """Try replace decorated function by module.function. + This is used to replace local functions with functions from another + (usually compiled) module, if available. + + Parameters + ---------- + module_function : str + Module and function path string (e.g. numpy.ones) + package : str, optional + The parent package of the module + warn : bool, optional + Whether to warn when wrapping fails + + Returns + ------- + func : function + Wrapped function, hopefully calling a function in another module. + + Example + ------- + >>> @_replace_by('_tifffile.decodepackbits') + ... def decodepackbits(encoded): + ... raise NotImplementedError + + """ def decorate(func, module_function=module_function, warn=warn): try: - module, function = module_function.split('.') - if not package: - module = import_module(module) + modname, function = module_function.split('.') + if package is None: + full_name = modname else: - module = import_module('.' + module, package=package) + full_name = package + '.' + modname + module = __import__(full_name, romlist=[modname]) func, oldfunc = getattr(module, function), func globals()['__old_' + func.__name__] = oldfunc except Exception: