summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMinoru TAKAHASHI <takahashi.minoru7@gmail.com>2016-05-10 13:58:03 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-05-13 22:17:23 +0900
commitc66b4b34e5d6fd71d999276a9413411682317044 (patch)
treeb4d15d5df0328e372d34d4fb70d6a4e434339ca9
parent2d05aedf145f725abf8abb0da9ee04c05104c903 (diff)
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 <mz.bremerhaven@gmail.com> Signed-off-by: Minoru TAKAHASHI <takahashi.minoru7@gmail.com> Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/utils.py28
1 files 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):