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 <[email protected]>
---
 ryu/services/protocols/bgp/application.py |  6 +++---
 ryu/utils.py                              | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/ryu/services/protocols/bgp/application.py 
b/ryu/services/protocols/bgp/application.py
index a1f4291..14279c4 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 3f6260e..57eb9b0 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):
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to