Merge pull request #1272 from blink1073/fix-tifffile-warning

Do not use importlib in `tifffile_local` to avoid warnings
This commit is contained in:
Juan Nunez-Iglesias
2014-12-13 23:44:52 +11:00
+30 -10
View File
@@ -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: