utils.import_module: Prefer filepath than Python module
Currently, ryu.utils.import_module() prefers the Python module path than the user specified file path, and if the user app name is the same with the Python module, this function returns the Python module instead of the user app. This patch fixes to try to load "file" before finding the loaded Python module, and fixes this problem. Note: With this patch, import_module() will reload (override) a module if the specified module file name is the same. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: Brad Cowie <brad@gizmoguy.net.nz> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
parent
077eef801e
commit
a197d87375
|
@ -44,9 +44,8 @@ class Test_import_module(unittest.TestCase):
|
||||||
eq_("this is ccc", ccc.name)
|
eq_("this is ccc", ccc.name)
|
||||||
ddd = import_module('./lib/test_mod/ddd/mod.py')
|
ddd = import_module('./lib/test_mod/ddd/mod.py')
|
||||||
# Note: When importing a module by filename, if module file name
|
# Note: When importing a module by filename, if module file name
|
||||||
# is duplicated, import_module returns a module instance which is
|
# is duplicated, import_module reload (override) a module instance.
|
||||||
# imported before.
|
eq_("this is ddd", ddd.name)
|
||||||
eq_("this is ccc", ddd.name)
|
|
||||||
|
|
||||||
def test_import_same_module1(self):
|
def test_import_same_module1(self):
|
||||||
from ryu.tests.unit.lib.test_mod import eee as eee1
|
from ryu.tests.unit.lib.test_mod import eee as eee1
|
||||||
|
|
43
ryu/utils.py
43
ryu/utils.py
|
@ -80,29 +80,34 @@ def _find_loaded_module(modpath):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def import_module(modname):
|
def _import_module_file(path):
|
||||||
try:
|
abspath = os.path.abspath(path)
|
||||||
# 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
|
|
||||||
# Backup original sys.path before appending path to file
|
# Backup original sys.path before appending path to file
|
||||||
original_path = list(sys.path)
|
original_path = list(sys.path)
|
||||||
sys.path.append(os.path.dirname(abspath))
|
sys.path.append(os.path.dirname(abspath))
|
||||||
# Remove python suffix
|
modname = chop_py_suffix(os.path.basename(abspath))
|
||||||
name = chop_py_suffix(os.path.basename(modname))
|
try:
|
||||||
# Retry to import
|
return load_source(modname, abspath)
|
||||||
mod = importlib.import_module(name)
|
finally:
|
||||||
# Restore sys.path
|
# Restore original sys.path
|
||||||
sys.path = original_path
|
sys.path = original_path
|
||||||
return mod
|
|
||||||
|
|
||||||
|
def import_module(modname):
|
||||||
|
if os.path.exists(modname):
|
||||||
|
try:
|
||||||
|
# Try to import module since 'modname' is a valid path to a file
|
||||||
|
# e.g.) modname = './path/to/module/name.py'
|
||||||
|
return _import_module_file(modname)
|
||||||
|
except SyntaxError:
|
||||||
|
# The file didn't parse as valid Python code, try
|
||||||
|
# importing module assuming 'modname' is a Python module name
|
||||||
|
# e.g.) modname = 'path.to.module.name'
|
||||||
|
return importlib.import_module(modname)
|
||||||
|
else:
|
||||||
|
# Import module assuming 'modname' is a Python module name
|
||||||
|
# e.g.) modname = 'path.to.module.name'
|
||||||
|
return importlib.import_module(modname)
|
||||||
|
|
||||||
|
|
||||||
def round_up(x, y):
|
def round_up(x, y):
|
||||||
|
|
Loading…
Reference in New Issue