summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWASE Yusuke <iwase.yusuke0@gmail.com>2016-10-24 17:18:40 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-10-25 22:40:28 +0900
commita7721fcce43465df6f05c8bf8c0367825c531ffb (patch)
tree85d582b9fb72c2630c5364554229b9fa54fc9cae
parent6792d6df2a28aac436eb9e2afb1fe07bd30a1831 (diff)
utils: Backward compatibility for 'imp.load_source'
This patch adds a function for providing the backward compatibility for 'imp.load_source' in Python 2 and fixes bgp/application.py to use this wrapper. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/services/protocols/bgp/application.py6
-rw-r--r--ryu/utils.py19
2 files changed, 22 insertions, 3 deletions
diff --git a/ryu/services/protocols/bgp/application.py b/ryu/services/protocols/bgp/application.py
index a1f4291b..14279c4e 100644
--- a/ryu/services/protocols/bgp/application.py
+++ b/ryu/services/protocols/bgp/application.py
@@ -15,14 +15,14 @@
"""
Defines bases classes to create a BGP application.
"""
-import imp
import logging
import traceback
+
from oslo_config import cfg
from ryu.lib import hub
+from ryu.utils import load_source
from ryu.base.app_manager import RyuApp
-
from ryu.services.protocols.bgp.api.base import call
from ryu.services.protocols.bgp.base import add_bgp_error_metadata
from ryu.services.protocols.bgp.base import BGPSException
@@ -138,7 +138,7 @@ class RyuBGPSpeaker(RyuApp):
# Check if file can be read
try:
- return imp.load_source('settings', config_file)
+ return load_source('settings', config_file)
except Exception as e:
raise ApplicationException(desc=str(e))
diff --git a/ryu/utils.py b/ryu/utils.py
index 3f6260ef..57eb9b0a 100644
--- a/ryu/utils.py
+++ b/ryu/utils.py
@@ -36,9 +36,28 @@ import os
import sys
import re
+import six
+
LOG = logging.getLogger('ryu.utils')
+def load_source(name, pathname):
+ """
+ This function provides the backward compatibility for 'imp.load_source'
+ in Python 2.
+
+ :param name: Name used to create or access a module object.
+ :param pathname: Path pointing to the source file.
+ :return: Loaded and initialized module.
+ """
+ if six.PY2:
+ import imp
+ return imp.load_source(name, pathname)
+ else:
+ loader = importlib.machinery.SourceFileLoader(name, pathname)
+ return loader.load_module(name)
+
+
def chop_py_suffix(p):
for suf in ['.py', '.pyc', '.pyo']:
if p.endswith(suf):