From c66b4b34e5d6fd71d999276a9413411682317044 Mon Sep 17 00:00:00 2001 From: Minoru TAKAHASHI Date: Tue, 10 May 2016 13:58:03 +0900 Subject: utils: fix for temporarily storing the value of sys.path Currentry, the *reference* of sys.path is temporarily stored before appending a path to the user modules. However, the *value* of sys.path should be stored because the type of sys.path is list (mutable object) type. Reported-by: Xandaros Signed-off-by: Minoru TAKAHASHI Signed-off-by: IWASE Yusuke Signed-off-by: FUJITA Tomonori --- ryu/utils.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/ryu/utils.py b/ryu/utils.py index a8eb5094..3f6260ef 100644 --- a/ryu/utils.py +++ b/ryu/utils.py @@ -30,7 +30,7 @@ # under the License. -import inspect +import importlib import logging import os import sys @@ -77,21 +77,27 @@ def _find_loaded_module(modpath): def import_module(modname): try: - __import__(modname) - except: + # Import module with python module path + # e.g.) modname = 'module.path.module_name' + return importlib.import_module(modname) + except (ImportError, TypeError): + # In this block, we retry to import module when modname is filename + # e.g.) modname = 'module/path/module_name.py' abspath = os.path.abspath(modname) + # Check if specified modname is already imported mod = _find_loaded_module(abspath) if mod: return mod - opath = sys.path + # Backup original sys.path before appending path to file + original_path = list(sys.path) sys.path.append(os.path.dirname(abspath)) - name = os.path.basename(modname) - if name.endswith('.py'): - name = name[:-3] - __import__(name) - sys.path = opath - return sys.modules[name] - return sys.modules[modname] + # Remove python suffix + name = chop_py_suffix(os.path.basename(modname)) + # Retry to import + mod = importlib.import_module(name) + # Restore sys.path + sys.path = original_path + return mod def round_up(x, y): -- cgit v1.2.3