From 3756e7adbecd98b43525403914688fe5f409e590 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 12 Dec 2014 15:59:44 -0600 Subject: [PATCH 1/3] Do not use importlib in tifffile_local to avoid warnings --- skimage/external/tifffile/tifffile_local.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/skimage/external/tifffile/tifffile_local.py b/skimage/external/tifffile/tifffile_local.py index b5a67ccf..1468abd6 100644 --- a/skimage/external/tifffile/tifffile_local.py +++ b/skimage/external/tifffile/tifffile_local.py @@ -3082,19 +3082,14 @@ 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 - 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: From 8f89852e054e5a47539f75664e42ddfb829b4776 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 12 Dec 2014 19:44:40 -0600 Subject: [PATCH 2/3] Improved docstring --- skimage/external/tifffile/tifffile_local.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/skimage/external/tifffile/tifffile_local.py b/skimage/external/tifffile/tifffile_local.py index 1468abd6..35c826ad 100644 --- a/skimage/external/tifffile/tifffile_local.py +++ b/skimage/external/tifffile/tifffile_local.py @@ -3081,7 +3081,11 @@ def imagej_description(description): def _replace_by(module_function, package=None, warn=False): - """Try replace decorated function by module.function.""" + """Try replace decorated function by module.function. + + This is used to replace local functions with functions from another + (usually compiled) module, if available. + """ def decorate(func, module_function=module_function, warn=warn): try: modname, function = module_function.split('.') From 1b38c172800b45c04fcf958c4e8712ae7374ea8a Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 12 Dec 2014 21:18:48 -0600 Subject: [PATCH 3/3] Beef up docstring --- skimage/external/tifffile/tifffile_local.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/skimage/external/tifffile/tifffile_local.py b/skimage/external/tifffile/tifffile_local.py index 35c826ad..4ddd6763 100644 --- a/skimage/external/tifffile/tifffile_local.py +++ b/skimage/external/tifffile/tifffile_local.py @@ -3085,6 +3085,27 @@ def _replace_by(module_function, package=None, warn=False): 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: